1 |
| package org.drools.semantics.java; |
2 |
| |
3 |
| import java.io.IOException; |
4 |
| import java.util.ArrayList; |
5 |
| import java.util.Iterator; |
6 |
| import java.util.List; |
7 |
| import java.util.Set; |
8 |
| import java.util.StringTokenizer; |
9 |
| |
10 |
| import org.codehaus.janino.DebuggingInformation; |
11 |
| import org.codehaus.janino.EvaluatorBase; |
12 |
| import org.codehaus.janino.Java; |
13 |
| import org.codehaus.janino.Location; |
14 |
| import org.codehaus.janino.Parser; |
15 |
| import org.codehaus.janino.Scanner; |
16 |
| import org.codehaus.janino.Java.CompileException; |
17 |
| import org.codehaus.janino.Parser.ParseException; |
18 |
| import org.codehaus.janino.Scanner.ScanException; |
19 |
| import org.drools.spi.Importer; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| public class ImporterClassBodyEvaluator extends EvaluatorBase |
29 |
| { |
30 |
| private final Class clazz; |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
1
| public ImporterClassBodyEvaluator(Importer importer,
|
49 |
| String className, |
50 |
| Scanner scanner, |
51 |
| ClassLoader classLoader) throws ScanException, |
52 |
| IOException, |
53 |
| CompileException, |
54 |
| ParseException, |
55 |
| ClassNotFoundException |
56 |
| { |
57 |
1
| super( classLoader );
|
58 |
| |
59 |
1
| Class optionalExtendedType = (Class) null;
|
60 |
1
| Class[] implementedTypes = new Class[0];
|
61 |
| |
62 |
1
| Java.CompilationUnit compilationUnit = new Java.CompilationUnit( scanner.peek( ).getLocation( ).getFileName( ) );
|
63 |
| |
64 |
| |
65 |
1
| Parser parser = new Parser( scanner );
|
66 |
1
| this.parseImportDeclarations( compilationUnit,
|
67 |
| scanner ); |
68 |
| |
69 |
| |
70 |
1
| Location loc = scanner.peek( ).getLocation( );
|
71 |
1
| Set imports = importer.getImports( );
|
72 |
1
| Iterator it = imports.iterator( );
|
73 |
1
| String type;
|
74 |
1
| List list;
|
75 |
1
| StringTokenizer st;
|
76 |
1
| String token;
|
77 |
1
| boolean importOnDemand;
|
78 |
1
| while ( it.hasNext( ) )
|
79 |
| { |
80 |
0
| importOnDemand = false;
|
81 |
0
| list = new ArrayList( );
|
82 |
0
| type = (String) it.next( );
|
83 |
0
| st = new StringTokenizer( type,
|
84 |
| "." ); |
85 |
0
| while ( st.hasMoreTokens( ) )
|
86 |
| { |
87 |
0
| token = st.nextToken( );
|
88 |
0
| if ( !token.equals( "*" ) )
|
89 |
| { |
90 |
0
| list.add( token );
|
91 |
| } |
92 |
| else |
93 |
| { |
94 |
0
| importOnDemand = true;
|
95 |
| } |
96 |
| } |
97 |
0
| if ( importOnDemand )
|
98 |
| { |
99 |
0
| compilationUnit.addImportDeclaration( new Java.TypeImportOnDemandDeclaration( loc,
|
100 |
| (String[]) list.toArray( new String[list.size( )] ) ) ); |
101 |
| } |
102 |
| else |
103 |
| { |
104 |
0
| compilationUnit.addImportDeclaration( new Java.SingleTypeImportDeclaration( loc,
|
105 |
| (String[]) list.toArray( new String[list.size( )] ) ) ); |
106 |
| } |
107 |
| } |
108 |
| |
109 |
| |
110 |
1
| Java.ClassDeclaration cd = this.addPackageMemberClassDeclaration( scanner.peek( ).getLocation( ),
|
111 |
| compilationUnit, |
112 |
| className, |
113 |
| optionalExtendedType, |
114 |
| implementedTypes ); |
115 |
| |
116 |
| |
117 |
1
| while ( !scanner.peek( ).isEOF( ) )
|
118 |
| { |
119 |
1
| parser.parseClassBodyDeclaration( cd );
|
120 |
| } |
121 |
| |
122 |
0
| this.clazz = this.compileAndLoad( compilationUnit,
|
123 |
| DebuggingInformation.SOURCE.add( DebuggingInformation.LINES ), |
124 |
| className ); |
125 |
| } |
126 |
| |
127 |
0
| public Class evaluate()
|
128 |
| { |
129 |
0
| return clazz;
|
130 |
| } |
131 |
| |
132 |
| } |