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 System.out.println(f0.getCanonicalPath());
133 System.out.println(f2.getCanonicalPath());
134 translationCollector.add(content, f0.getCanonicalPath()
135 .substring(f2.getCanonicalPath().length() + 1)
138 } catch (IOException e1) {
139 e1.printStackTrace();
144 if (e instanceof NullLiteral) {
148 if (e instanceof MessageSend) {
149 MessageSend m2 = (MessageSend) e;
150 TaintSource ts = new TaintSource(m2.binding);
151 if (ts.equals(new TaintSource("org.cacert.gigi.pages", "Page",
155 if (m2.receiver.resolvedType.isCompatibleWith(scope
156 .getJavaLangEnum())) {
157 testEnum(m2.receiver, m2.binding);
158 System.out.println("ENUM-SRC: !" + m2.receiver);
161 if (e.resolvedType.isCompatibleWith(scope.getJavaLangEnum())) {
163 System.out.println("ENUM-Not-Hanled");
166 TaintSource b = cm == null ? null : new TaintSource(cm);
167 for (TaintSource taintSource : ts) {
168 if (taintSource.equals(b)
169 || (taintSource.getMaskOnly() != null && taintSource
170 .getMaskOnly().equals(b))) {
174 if (e instanceof ConditionalExpression) {
175 check(call, scope, ((ConditionalExpression) e).valueIfFalse, caller);
176 check(call, scope, ((ConditionalExpression) e).valueIfTrue, caller);
180 System.out.println();
182 System.out.println(new String(scope.enclosingClassScope()
183 .referenceType().compilationResult.fileName));
184 System.out.println("Cannot Handle: " + e + " in "
185 + (call == null ? "constructor" : call.sourceStart) + " => "
187 System.out.println(e.getClass());
188 System.out.println("To ignore: " + b.toConfLine());
190 private void testEnum(Expression e, MethodBinding binding) {
191 if (binding.parameters.length != 0) {
192 System.out.println("ERROR: meth");
195 System.out.println(e.resolvedType.getClass());
196 String s2 = new String(e.resolvedType.qualifiedPackageName())
198 + (new String(e.resolvedType.qualifiedSourceName()).replace(
201 Class<?> c = Class.forName(s2);
202 Enum<?>[] e1 = (Enum[]) c.getMethod("values").invoke(null);
203 Method m = c.getMethod(new String(binding.selector));
204 for (int j = 0; j < e1.length; j++) {
205 System.out.println(m.invoke(e1[j]));
207 } catch (ClassNotFoundException e1) {
208 e1.printStackTrace();
209 } catch (ReflectiveOperationException e1) {
210 e1.printStackTrace();
212 System.out.println("ENUM-done: " + e + "!");