1 package club.wpia.gigi.localisation;
4 import java.io.IOException;
5 import java.lang.reflect.Method;
6 import java.util.Stack;
8 import org.eclipse.jdt.internal.compiler.ASTVisitor;
9 import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
10 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
11 import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression;
12 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
13 import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
14 import org.eclipse.jdt.internal.compiler.ast.Expression;
15 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
16 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
17 import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
18 import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
19 import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
20 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
21 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
22 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
23 import org.eclipse.jdt.internal.compiler.util.Util;
25 public final class TranslationCollectingVisitor extends ASTVisitor {
29 private CompilationUnitDeclaration unit;
33 private TranslationCollector translationCollector;
35 Stack<QualifiedAllocationExpression> anonymousConstructorCall = new Stack<>();
37 public TranslationCollectingVisitor(CompilationUnitDeclaration unit, TaintSource[] target, TranslationCollector c) {
40 this.translationCollector = c;
44 public boolean visit(MethodDeclaration methodDeclaration, org.eclipse.jdt.internal.compiler.lookup.ClassScope scope) {
45 cm = methodDeclaration.binding;
50 public void endVisit(MethodDeclaration methodDeclaration, org.eclipse.jdt.internal.compiler.lookup.ClassScope scope) {
55 public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
56 cm = constructorDeclaration.binding;
57 return super.visit(constructorDeclaration, scope);
61 public void endVisit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
66 public boolean visit(AllocationExpression allocationExpression, BlockScope scope) {
67 TaintSource test = new TaintSource(allocationExpression.binding);
68 for (TaintSource taintSource : ts) {
69 if (taintSource.equals(test)) {
70 check(null, scope, allocationExpression.arguments[taintSource.getTgt()], allocationExpression.toString());
74 return super.visit(allocationExpression, scope);
78 public boolean visit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
79 anonymousConstructorCall.push(qualifiedAllocationExpression);
80 return super.visit(qualifiedAllocationExpression, scope);
84 public void endVisit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
85 if (anonymousConstructorCall.pop() != qualifiedAllocationExpression) {
86 throw new Error("stack illegally manipulated");
91 public boolean visit(ExplicitConstructorCall explicitConstructor, BlockScope scope) {
93 TaintSource t = new TaintSource(explicitConstructor.binding);
95 for (TaintSource t0 : ts) {
97 Expression[] ags = explicitConstructor.arguments;
98 if (anonymousConstructorCall.size() > 0) {
99 ags = anonymousConstructorCall.peek().arguments;
102 System.err.println(explicitConstructor);
105 Expression e = ags[t0.getTgt()];
106 check(null, scope, e, explicitConstructor.toString());
110 return super.visit(explicitConstructor, scope);
114 public boolean visit(org.eclipse.jdt.internal.compiler.ast.MessageSend call, org.eclipse.jdt.internal.compiler.lookup.BlockScope scope) {
115 if (call.binding == null) {
116 System.err.println("Unbound:" + call + " in " + call.sourceStart());
119 // System.out.println("Message");
120 TaintSource t = new TaintSource(call.binding);
122 for (TaintSource t0 : ts) {
124 Expression[] ags = call.arguments;
126 System.err.println(call);
129 Expression e = ags[t0.getTgt()];
130 check(call, scope, e, call.toString());
137 private void check(org.eclipse.jdt.internal.compiler.ast.MessageSend call, org.eclipse.jdt.internal.compiler.lookup.BlockScope scope, Expression e, String caller) {
138 if (e instanceof StringLiteral) {
139 int[] lineEnds = null;
140 int lineNumber = Util.getLineNumber(e.sourceStart, lineEnds = unit.compilationResult.getLineSeparatorPositions(), 0, lineEnds.length - 1);
142 String content = new String(((StringLiteral) e).source());
143 File f0 = new File(new String(unit.compilationResult.fileName)).getAbsoluteFile();
144 File f2 = translationCollector.base.getAbsoluteFile();
146 translationCollector.add(content, f0.getCanonicalPath().substring(f2.getCanonicalPath().length() + 1) + ":" + lineNumber);
147 } catch (IOException e1) {
148 e1.printStackTrace();
153 if (e instanceof NullLiteral) {
157 if (e instanceof MessageSend) {
158 MessageSend m2 = (MessageSend) e;
159 TaintSource ts = new TaintSource(m2.binding);
160 if (ts.equals(new TaintSource("club.wpia.gigi.pages", "Page", "getTitle()", 0))) {
163 if (m2.receiver.resolvedType.isCompatibleWith(scope.getJavaLangEnum())) {
164 testEnum(m2.receiver, m2.binding);
165 System.err.println("ENUM-SRC: !" + m2.receiver);
168 if (e.resolvedType.isCompatibleWith(scope.getJavaLangEnum())) {
170 System.err.println("ENUM-Not-Hanled");
173 TaintSource b = cm == null ? null : new TaintSource(cm);
174 for (TaintSource taintSource : ts) {
175 if (taintSource.equals(b) || (taintSource.getMaskOnly() != null && taintSource.getMaskOnly().equals(b))) {
179 if (e instanceof ConditionalExpression) {
180 check(call, scope, ((ConditionalExpression) e).valueIfFalse, caller);
181 check(call, scope, ((ConditionalExpression) e).valueIfTrue, caller);
185 System.err.println();
187 System.err.println(new String(scope.enclosingClassScope().referenceType().compilationResult.fileName));
188 System.err.println("Cannot Handle: " + e + " in " + (call == null ? "constructor" : call.sourceStart) + " => " + caller);
189 System.err.println(e.getClass());
190 System.err.println("To ignore: " + (b == null ? "don't know" : b.toConfLine()));
191 translationCollector.hadError();
194 private void testEnum(Expression e, MethodBinding binding) {
195 if (binding.parameters.length != 0) {
196 System.err.println("ERROR: meth");
199 System.err.println(e.resolvedType.getClass());
200 String s2 = new String(e.resolvedType.qualifiedPackageName()) + "." + (new String(e.resolvedType.qualifiedSourceName()).replace('.', '$'));
202 Class<?> c = Class.forName(s2);
203 Enum<?>[] e1 = (Enum[]) c.getMethod("values").invoke(null);
204 Method m = c.getMethod(new String(binding.selector));
205 for (int j = 0; j < e1.length; j++) {
206 System.err.println(m.invoke(e1[j]));
208 } catch (ClassNotFoundException e1) {
209 e1.printStackTrace();
210 } catch (ReflectiveOperationException e1) {
211 e1.printStackTrace();
213 System.err.println("ENUM-done: " + e + "!");