1 package org.cacert.gigi.localisation;
3 import java.io.IOException;
4 import java.lang.reflect.Method;
5 import java.util.Stack;
7 import org.eclipse.jdt.internal.compiler.ASTVisitor;
8 import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
9 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
10 import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression;
11 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
12 import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
13 import org.eclipse.jdt.internal.compiler.ast.Expression;
14 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
15 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
16 import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
17 import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
18 import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
19 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
20 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
21 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
22 import org.eclipse.jdt.internal.compiler.util.Util;
24 public final class TranslationCollectingVisitor extends ASTVisitor {
26 private CompilationUnitDeclaration unit;
28 private TranslationCollector translationCollector;
30 Stack<QualifiedAllocationExpression> anonymousConstructorCall = new Stack<>();
32 public TranslationCollectingVisitor(CompilationUnitDeclaration unit,
33 TaintSource[] target, TranslationCollector c) {
36 this.translationCollector = c;
39 public boolean visit(MethodDeclaration methodDeclaration,
40 org.eclipse.jdt.internal.compiler.lookup.ClassScope scope) {
41 cm = methodDeclaration.binding;
45 public void endVisit(MethodDeclaration methodDeclaration,
46 org.eclipse.jdt.internal.compiler.lookup.ClassScope scope) {
50 public boolean visit(ConstructorDeclaration constructorDeclaration,
52 cm = constructorDeclaration.binding;
53 return super.visit(constructorDeclaration, scope);
56 public void endVisit(ConstructorDeclaration constructorDeclaration,
61 public boolean visit(AllocationExpression allocationExpression,
63 TaintSource test = new TaintSource(allocationExpression.binding);
64 for (TaintSource taintSource : ts) {
65 if (taintSource.equals(test)) {
67 allocationExpression.arguments[taintSource.getTgt()],
68 allocationExpression.toString());
72 return super.visit(allocationExpression, scope);
76 public boolean visit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
77 anonymousConstructorCall.push(qualifiedAllocationExpression);
78 return super.visit(qualifiedAllocationExpression, scope);
82 public void endVisit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
83 if(anonymousConstructorCall.pop() != qualifiedAllocationExpression){
84 throw new Error("stack illegally manipulated");
89 public boolean visit(ExplicitConstructorCall explicitConstructor,
92 TaintSource t = new TaintSource(explicitConstructor.binding);
94 for (TaintSource t0 : ts) {
96 Expression[] ags = explicitConstructor.arguments;
97 if (anonymousConstructorCall.size() > 0) {
98 ags = anonymousConstructorCall.peek().arguments;
101 System.out.println(explicitConstructor);
104 Expression e = ags[t0.getTgt()];
105 check(null, scope, e, explicitConstructor.toString());
109 return super.visit(explicitConstructor, scope);
113 public boolean visit(
114 org.eclipse.jdt.internal.compiler.ast.MessageSend call,
115 org.eclipse.jdt.internal.compiler.lookup.BlockScope scope) {
116 if (call.binding == null) {
117 System.out.println("Unbound:" + call + " in " + call.sourceStart());
120 //System.out.println("Message");
121 TaintSource t = new TaintSource(call.binding);
123 for (TaintSource t0 : ts) {
125 Expression[] ags = call.arguments;
127 System.out.println(call);
130 Expression e = ags[t0.getTgt()];
131 check(call, scope, e, call.toString());
137 private void check(org.eclipse.jdt.internal.compiler.ast.MessageSend call,
138 org.eclipse.jdt.internal.compiler.lookup.BlockScope scope,
139 Expression e, String caller) {
140 if (e instanceof StringLiteral) {
141 int[] lineEnds = null;
142 int lineNumber = Util.getLineNumber(
144 lineEnds = unit.compilationResult
145 .getLineSeparatorPositions(), 0,
146 lineEnds.length - 1);
148 String content = new String(((StringLiteral) e).source());
149 File f0 = new File(new String(unit.compilationResult.fileName))
151 File f2 = translationCollector.base.getAbsoluteFile();
153 translationCollector.add(content, f0.getCanonicalPath()
154 .substring(f2.getCanonicalPath().length() + 1)
157 } catch (IOException e1) {
158 e1.printStackTrace();
163 if (e instanceof NullLiteral) {
167 if (e instanceof MessageSend) {
168 MessageSend m2 = (MessageSend) e;
169 TaintSource ts = new TaintSource(m2.binding);
170 if (ts.equals(new TaintSource("org.cacert.gigi.pages", "Page",
174 if (m2.receiver.resolvedType.isCompatibleWith(scope
175 .getJavaLangEnum())) {
176 testEnum(m2.receiver, m2.binding);
177 System.out.println("ENUM-SRC: !" + m2.receiver);
180 if (e.resolvedType.isCompatibleWith(scope.getJavaLangEnum())) {
182 System.out.println("ENUM-Not-Hanled");
185 TaintSource b = cm == null ? null : new TaintSource(cm);
186 for (TaintSource taintSource : ts) {
187 if (taintSource.equals(b)
188 || (taintSource.getMaskOnly() != null && taintSource
189 .getMaskOnly().equals(b))) {
193 if (e instanceof ConditionalExpression) {
194 check(call, scope, ((ConditionalExpression) e).valueIfFalse, caller);
195 check(call, scope, ((ConditionalExpression) e).valueIfTrue, caller);
199 System.out.println();
201 System.out.println(new String(scope.enclosingClassScope()
202 .referenceType().compilationResult.fileName));
203 System.out.println("Cannot Handle: " + e + " in "
204 + (call == null ? "constructor" : call.sourceStart) + " => "
206 System.out.println(e.getClass());
208 "To ignore: " + (b == null ? "don't know" : b.toConfLine()));
210 private void testEnum(Expression e, MethodBinding binding) {
211 if (binding.parameters.length != 0) {
212 System.out.println("ERROR: meth");
215 System.out.println(e.resolvedType.getClass());
216 String s2 = new String(e.resolvedType.qualifiedPackageName())
218 + (new String(e.resolvedType.qualifiedSourceName()).replace(
221 Class<?> c = Class.forName(s2);
222 Enum<?>[] e1 = (Enum[]) c.getMethod("values").invoke(null);
223 Method m = c.getMethod(new String(binding.selector));
224 for (int j = 0; j < e1.length; j++) {
225 System.out.println(m.invoke(e1[j]));
227 } catch (ClassNotFoundException e1) {
228 e1.printStackTrace();
229 } catch (ReflectiveOperationException e1) {
230 e1.printStackTrace();
232 System.out.println("ENUM-done: " + e + "!");