-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunFig.java
More file actions
112 lines (102 loc) · 3.98 KB
/
Copy pathRunFig.java
File metadata and controls
112 lines (102 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/***
* Excerpted from "Language Implementation Patterns",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/tpdsl for more book information.
***/
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.ANTLRFileStream;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.io.IOException;
public class RunFig {
// E.g., "$ java RunFig jguru.fig"
public static void main(String args[]) throws Exception {
Map<String,Object> config_objects = readFig(args[0]);
for ( String name : config_objects.keySet() ) {
System.out.println(name+":"+config_objects.get(name));
}
}
public static Map<String,Object> readFig(String fileName)
throws IOException, RecognitionException
{
// Create lexer attached to character stream from fileName
FigLexer lexer = new FigLexer(new ANTLRFileStream(fileName));
// Create a buffer of tokens feeding off of lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Create parser feeding off of token buffer
FigParser p = new FigParser(tokens);
return p.file(); // parse, return dictionary of config'd objects
}
public static void setObjectProperty(Object o, String propertyName, Object value) {
Class c = o.getClass();
// First see if name is a property ala javabeans
String methodSuffix = Character.toUpperCase(propertyName.charAt(0))+
propertyName.substring(1,propertyName.length());
Method m = getMethod(c,"set"+methodSuffix, new Class[] {value.getClass()});
if ( m != null ) {
try {
invokeMethod(m, o, value);
}
catch (Exception e) {
System.err.println("Can't set property "+propertyName+" using method set"+methodSuffix+
" from "+c.getName()+" instance: "+e);
}
}
else {
// try for a visible field
try {
Field f = c.getField(propertyName);
try {
f.set(o, value);
}
catch (IllegalAccessException iae) {
System.err.println("Can't access property "+propertyName+" using direct field access from "+
c.getName()+" instance: "+ iae);
}
}
catch (NoSuchFieldException nsfe) {
System.err.println("Class "+c.getName()+" has no such property/field: "+propertyName+
": "+nsfe);
}
}
}
protected static Object newInstance(String name) {
Object o = null;
try {
o = Class.forName(name).newInstance();
}
catch (Exception e) {
System.err.println("can't make instance of "+name);
}
return o;
}
protected static Method getMethod(Class c, String methodName, Class[] args) {
Method m;
try {
m = c.getMethod(methodName, args);
}
catch (NoSuchMethodException nsme) {
m = null;
}
return m;
}
protected static Object invokeMethod(Method m, Object o) throws IllegalAccessException, InvocationTargetException {
return invokeMethod(m, o, null);
}
protected static Object invokeMethod(Method m, Object o, Object value) throws IllegalAccessException, InvocationTargetException {
Object[] args = null;
if ( value!=null ) {
args = new Object[] {value};
}
value = m.invoke(o, args);
return value;
}
}