Java
JavaのReflectionAPIを使用して、ファイルを読み取り、実行する操作(オブジェクトの作成、クラスのメソッドの呼び出し)を計算するプログラムを作成しました。例えば:
2234:org.powertac.common.Rate::9::new
2234:org.powertac.common.Rate::9::withValue::-0.5
キーワードを見つけたらnew
、そのクラスの新しいオブジェクトを作成しようとします。withValue()
それ以外の場合は、引数を使用してメソッド(例では)を呼び出します-0.5
。ソースコードを以下に示します。
//
// program created a map between int and string
//
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
import java.lang.reflect.*;
import org.powertac.common.*;
import org.powertac.util.*;
public class Parser {
private String filename;
private Map< Integer, Object > map;
//
// just the default constructor is needed for now
//
public Parser(){
this.map = new HashMap< Integer, Object >();
this.filename = "/home/phillipe/powertac/powertac-server/" +
"server-distribution/log/powertac-boot.state";
}
//
// override the default constructor
//
public Parser( String filename ){
this.filename = filename;
}
public void parse() throws ClassNotFoundException{
try{
//
// assure file exists before parsing
//
FileReader fr = new FileReader( this.filename );
BufferedReader textReader = new BufferedReader( fr );
String line;
Integer id = 1;
while(( line = textReader.readLine()) != null ){
Pattern p = Pattern.compile("([^:]+):([^:]+)::([\\d]+)::([^:]+)::(.+)");
Matcher m = p.matcher( line );
if (m.find()) {
//String id = m.group(1);
String className = m.group(2);
int orderOfExecution = Integer.valueOf( m.group( 3 ));
String methodNameOrNew = m.group(4);
Object[] arguments = m.group(5).split("::");
if( methodNameOrNew.compareTo( "new" ) == 0 ){
System.out.println("Loading class: " + className);
Competition cc = null;
if( className.contains("Competition")){
this.map.put(id, cc);
}
else if( className.contains( "LocalBroker" )){
continue;
}
else {
Class<?> cl = Class.forName( className );
Constructor<?> cons = cl.getConstructor(String.class);
Object obj = cons.newInstance( arguments );
this.map.put( id , obj );
}
}
else{
Class<?> cl = Class.forName( className );
Method method = cl.getMethod( methodNameOrNew, String.class );
this.map.put(id, method);
}
id++;
System.out.printf("%s %s %d %s %d\n", id, className, orderOfExecution, methodNameOrNew, arguments.length);
}
}
textReader.close();
}
catch( IOException ex ){
ex.printStackTrace();
}
catch( ArrayIndexOutOfBoundsException ex){
ex.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void print(){
Iterator<?> iterator = this.map.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = this.map.get(key).toString();
System.out.println(key + " " + value);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Parser parse = new Parser();
try {
parse.parse();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit( 0 );
}
}
ただし、次のエラーが発生します。
Loading class: org.powertac.common.Competition
2 org.powertac.common.Competition 0 new 1
Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/Signature
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at Parser.parse(Parser.java:71)
at Parser.main(Parser.java:123)
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.Signature
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
.jar
クラスバイナリを含むファイルをロードしました。でこの種の仕事をするのは初めてなJava
ので、私はあまりなじみがありません....誰かが私を助けてくれませんか?それは行方不明の図書館か何かのようなものですか?ありがとう