1 package org.cacert.gigi.localisation;
3 import java.io.IOException;
4 import java.lang.reflect.Method;
6 import org.eclipse.jdt.internal.compiler.ASTVisitor;
7 import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
8 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
9 import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression;
10 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
11 import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
12 import org.eclipse.jdt.internal.compiler.ast.Expression;
13 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
14 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
15 import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
16 import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
17 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
18 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
19 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
20 import org.eclipse.jdt.internal.compiler.util.Util;
22 public final class TranslationCollectingVisitor extends ASTVisitor {
24 private CompilationUnitDeclaration unit;
26 private TranslationCollector translationCollector;
28 public TranslationCollectingVisitor(CompilationUnitDeclaration unit,
29 TaintSource[] target, TranslationCollector c) {
32 this.translationCollector = c;
35 public boolean visit(MethodDeclaration methodDeclaration,
36 org.eclipse.jdt.internal.compiler.lookup.ClassScope scope) {
37 cm = methodDeclaration.binding;
41 public void endVisit(MethodDeclaration methodDeclaration,
42 org.eclipse.jdt.internal.compiler.lookup.ClassScope scope) {
46 public boolean visit(ConstructorDeclaration constructorDeclaration,
48 cm = constructorDeclaration.binding;
49 return super.visit(constructorDeclaration, scope);
52 public void endVisit(ConstructorDeclaration constructorDeclaration,
57 public boolean visit(AllocationExpression allocationExpression,
59 TaintSource test = new TaintSource(allocationExpression.binding);
60 for (TaintSource taintSource : ts) {
61 if (taintSource.equals(test)) {
63 allocationExpression.arguments[taintSource.getTgt()],
64 allocationExpression.toString());
68 return super.visit(allocationExpression, scope);
71 public boolean visit(ExplicitConstructorCall explicitConstructor,
74 TaintSource t = new TaintSource(explicitConstructor.binding);
76 for (TaintSource t0 : ts) {
78 Expression[] ags = explicitConstructor.arguments;
80 System.out.println(explicitConstructor);
83 Expression e = ags[t0.getTgt()];
84 check(null, scope, e, explicitConstructor.toString());
88 return super.visit(explicitConstructor, scope);
93 org.eclipse.jdt.internal.compiler.ast.MessageSend call,
94 org.eclipse.jdt.internal.compiler.lookup.BlockScope scope) {
95 if (call.binding == null) {
96 System.out.println("Unbound:" + call + " in " + call.sourceStart());
99 //System.out.println("Message");
100 TaintSource t = new TaintSource(call.binding);
102 for (TaintSource t0 : ts) {
104 Expression[] ags = call.arguments;
106 System.out.println(call);
109 Expression e = ags[t0.getTgt()];
110 check(call, scope, e, call.toString());
116 private void check(org.eclipse.jdt.internal.compiler.ast.MessageSend call,
117 org.eclipse.jdt.internal.compiler.lookup.BlockScope scope,
118 Expression e, String caller) {
119 if (e instanceof StringLiteral) {
120 int[] lineEnds = null;
121 int lineNumber = Util.getLineNumber(
123 lineEnds = unit.compilationResult
124 .getLineSeparatorPositions(), 0,
125 lineEnds.length - 1);
127 String content = new String(((StringLiteral) e).source());
128 File f0 = new File(new String(unit.compilationResult.fileName))
130 File f2 = translationCollector.base.getAbsoluteFile();
132 translationCollector.add(content, f0.getCanonicalPath()
133 .substring(f2.getCanonicalPath().length() + 1)
136 } catch (IOException e1) {
137 e1.printStackTrace();
142 if (e instanceof NullLiteral) {
146 if (e instanceof MessageSend) {
147 MessageSend m2 = (MessageSend) e;
148 TaintSource ts = new TaintSource(m2.binding);
149 if (ts.equals(new TaintSource("org.cacert.gigi.pages", "Page",
153 if (m2.receiver.resolvedType.isCompatibleWith(scope
154 .getJavaLangEnum())) {
155 testEnum(m2.receiver, m2.binding);
156 System.out.println("ENUM-SRC: !" + m2.receiver);
159 if (e.resolvedType.isCompatibleWith(scope.getJavaLangEnum())) {
161 System.out.println("ENUM-Not-Hanled");
164 TaintSource b = cm == null ? null : new TaintSource(cm);
165 for (TaintSource taintSource : ts) {
166 if (taintSource.equals(b)
167 || (taintSource.getMaskOnly() != null && taintSource
168 .getMaskOnly().equals(b))) {
172 if (e instanceof ConditionalExpression) {
173 check(call, scope, ((ConditionalExpression) e).valueIfFalse, caller);
174 check(call, scope, ((ConditionalExpression) e).valueIfTrue, caller);
178 System.out.println();
180 System.out.println(new String(scope.enclosingClassScope()
181 .referenceType().compilationResult.fileName));
182 System.out.println("Cannot Handle: " + e + " in "
183 + (call == null ? "constructor" : call.sourceStart) + " => "
185 System.out.println(e.getClass());
186 System.out.println("To ignore: " + b.toConfLine());
188 private void testEnum(Expression e, MethodBinding binding) {
189 if (binding.parameters.length != 0) {
190 System.out.println("ERROR: meth");
193 System.out.println(e.resolvedType.getClass());
194 String s2 = new String(e.resolvedType.qualifiedPackageName())
196 + (new String(e.resolvedType.qualifiedSourceName()).replace(
199 Class<?> c = Class.forName(s2);
200 Enum<?>[] e1 = (Enum[]) c.getMethod("values").invoke(null);
201 Method m = c.getMethod(new String(binding.selector));
202 for (int j = 0; j < e1.length; j++) {
203 System.out.println(m.invoke(e1[j]));
205 } catch (ClassNotFoundException e1) {
206 e1.printStackTrace();
207 } catch (ReflectiveOperationException e1) {
208 e1.printStackTrace();
210 System.out.println("ENUM-done: " + e + "!");