]> WPIA git - gigi.git/blob - util-testing/club/wpia/gigi/localisation/TranslationCollectingVisitor.java
add: code to statically verify SQL call patterns
[gigi.git] / util-testing / club / wpia / gigi / localisation / TranslationCollectingVisitor.java
1 package club.wpia.gigi.localisation;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.lang.reflect.Method;
6 import java.util.Stack;
7
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;
24
25 public final class TranslationCollectingVisitor extends ASTVisitor {
26
27     MethodBinding cm;
28
29     private CompilationUnitDeclaration unit;
30
31     TaintSource[] ts;
32
33     private TranslationCollector translationCollector;
34
35     Stack<QualifiedAllocationExpression> anonymousConstructorCall = new Stack<>();
36
37     public TranslationCollectingVisitor(CompilationUnitDeclaration unit, TaintSource[] target, TranslationCollector c) {
38         this.unit = unit;
39         ts = target;
40         this.translationCollector = c;
41     }
42
43     @Override
44     public boolean visit(MethodDeclaration methodDeclaration, org.eclipse.jdt.internal.compiler.lookup.ClassScope scope) {
45         cm = methodDeclaration.binding;
46         return true;
47     }
48
49     @Override
50     public void endVisit(MethodDeclaration methodDeclaration, org.eclipse.jdt.internal.compiler.lookup.ClassScope scope) {
51         cm = null;
52     }
53
54     @Override
55     public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
56         cm = constructorDeclaration.binding;
57         return super.visit(constructorDeclaration, scope);
58     }
59
60     @Override
61     public void endVisit(ConstructorDeclaration constructorDeclaration, ClassScope scope) {
62         cm = null;
63     }
64
65     @Override
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());
71                 return true;
72             }
73         }
74         return super.visit(allocationExpression, scope);
75     }
76
77     @Override
78     public boolean visit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
79         anonymousConstructorCall.push(qualifiedAllocationExpression);
80         return super.visit(qualifiedAllocationExpression, scope);
81     }
82
83     @Override
84     public void endVisit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
85         if (anonymousConstructorCall.pop() != qualifiedAllocationExpression) {
86             throw new Error("stack illegally manipulated");
87         }
88     }
89
90     @Override
91     public boolean visit(ExplicitConstructorCall explicitConstructor, BlockScope scope) {
92
93         TaintSource t = new TaintSource(explicitConstructor.binding);
94
95         for (TaintSource t0 : ts) {
96             if (t0.equals(t)) {
97                 Expression[] ags = explicitConstructor.arguments;
98                 if (anonymousConstructorCall.size() > 0) {
99                     ags = anonymousConstructorCall.peek().arguments;
100                 }
101                 if (ags == null) {
102                     System.err.println(explicitConstructor);
103                     return true;
104                 }
105                 Expression e = ags[t0.getTgt()];
106                 check(null, scope, e, explicitConstructor.toString());
107                 break;
108             }
109         }
110         return super.visit(explicitConstructor, scope);
111     }
112
113     @Override
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());
117             return true;
118         }
119         // System.out.println("Message");
120         TaintSource t = new TaintSource(call.binding);
121
122         for (TaintSource t0 : ts) {
123             if (t0.equals(t)) {
124                 Expression[] ags = call.arguments;
125                 if (ags == null) {
126                     System.err.println(call);
127                     return true;
128                 }
129                 Expression e = ags[t0.getTgt()];
130                 check(call, scope, e, call.toString());
131                 break;
132             }
133         }
134         return true;
135     }
136
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);
141
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();
145             try {
146                 translationCollector.add(content, f0.getCanonicalPath().substring(f2.getCanonicalPath().length() + 1) + ":" + lineNumber);
147             } catch (IOException e1) {
148                 e1.printStackTrace();
149             }
150             return;
151         }
152
153         if (e instanceof NullLiteral) {
154             return;
155         }
156
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))) {
161                 return;
162             }
163             if (m2.receiver.resolvedType.isCompatibleWith(scope.getJavaLangEnum())) {
164                 testEnum(m2.receiver, m2.binding);
165                 System.err.println("ENUM-SRC: !" + m2.receiver);
166             }
167         }
168         if (e.resolvedType.isCompatibleWith(scope.getJavaLangEnum())) {
169             // TODO ?
170             System.err.println("ENUM-Not-Hanled");
171         }
172
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))) {
176                 return;
177             }
178         }
179         if (e instanceof ConditionalExpression) {
180             check(call, scope, ((ConditionalExpression) e).valueIfFalse, caller);
181             check(call, scope, ((ConditionalExpression) e).valueIfTrue, caller);
182             return;
183         }
184
185         System.err.println();
186
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();
192     }
193
194     private void testEnum(Expression e, MethodBinding binding) {
195         if (binding.parameters.length != 0) {
196             System.err.println("ERROR: meth");
197             return;
198         }
199         System.err.println(e.resolvedType.getClass());
200         String s2 = new String(e.resolvedType.qualifiedPackageName()) + "." + (new String(e.resolvedType.qualifiedSourceName()).replace('.', '$'));
201         try {
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]));
207             }
208         } catch (ClassNotFoundException e1) {
209             e1.printStackTrace();
210         } catch (ReflectiveOperationException e1) {
211             e1.printStackTrace();
212         }
213         System.err.println("ENUM-done: " + e + "!");
214         return;
215     }
216 }