]> WPIA git - gigi.git/blobdiff - util-testing/org/cacert/gigi/localisation/TranslationCollectingVisitor.java
fix: collect translation strings from anonymous class' constructors
[gigi.git] / util-testing / org / cacert / gigi / localisation / TranslationCollectingVisitor.java
index 472789d8750e147e6dc15022e607cbc3f9ef963a..53e5c1cae0f9ff04bd258f1e234d93951bf3cc2b 100644 (file)
@@ -1,5 +1,8 @@
 package org.cacert.gigi.localisation;
+import java.io.File;
+import java.io.IOException;
 import java.lang.reflect.Method;
+import java.util.Stack;
 
 import org.eclipse.jdt.internal.compiler.ASTVisitor;
 import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
@@ -11,6 +14,7 @@ import org.eclipse.jdt.internal.compiler.ast.Expression;
 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
+import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
 import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
@@ -23,6 +27,8 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
        TaintSource[] ts;
        private TranslationCollector translationCollector;
 
+    Stack<QualifiedAllocationExpression> anonymousConstructorCall = new Stack<>();
+
        public TranslationCollectingVisitor(CompilationUnitDeclaration unit,
                        TaintSource[] target, TranslationCollector c) {
                this.unit = unit;
@@ -65,6 +71,20 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
                }
                return super.visit(allocationExpression, scope);
        }
+
+    @Override
+    public boolean visit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
+        anonymousConstructorCall.push(qualifiedAllocationExpression);
+        return super.visit(qualifiedAllocationExpression, scope);
+    }
+
+    @Override
+    public void endVisit(QualifiedAllocationExpression qualifiedAllocationExpression, BlockScope scope) {
+        if(anonymousConstructorCall.pop() != qualifiedAllocationExpression){
+            throw new Error("stack illegally manipulated");
+        }
+    }
+
        @Override
        public boolean visit(ExplicitConstructorCall explicitConstructor,
                        BlockScope scope) {
@@ -74,6 +94,9 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
                for (TaintSource t0 : ts) {
                        if (t0.equals(t)) {
                                Expression[] ags = explicitConstructor.arguments;
+                if (anonymousConstructorCall.size() > 0) {
+                    ags = anonymousConstructorCall.peek().arguments;
+                }
                                if (ags == null) {
                                        System.out.println(explicitConstructor);
                                        return true;
@@ -123,12 +146,17 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
                                        lineEnds.length - 1);
 
                        String content = new String(((StringLiteral) e).source());
-                       translationCollector.add(
-                                       content,
-                                       new String(unit.compilationResult.fileName)
-                                                       .substring(translationCollector.base
-                                                                       .getAbsolutePath().length() + 1)
-                                                       + ":" + lineNumber);
+                       File f0 = new File(new String(unit.compilationResult.fileName))
+                                       .getAbsoluteFile();
+                       File f2 = translationCollector.base.getAbsoluteFile();
+                       try {
+                               translationCollector.add(content, f0.getCanonicalPath()
+                                               .substring(f2.getCanonicalPath().length() + 1)
+                                               + ":"
+                                               + lineNumber);
+                       } catch (IOException e1) {
+                               e1.printStackTrace();
+                       }
                        return;
                }
 
@@ -176,7 +204,8 @@ public final class TranslationCollectingVisitor extends ASTVisitor {
                                + (call == null ? "constructor" : call.sourceStart) + " => "
                                + caller);
                System.out.println(e.getClass());
-               System.out.println("To ignore: " + b.toConfLine());
+               System.out.println(
+                               "To ignore: " + (b == null ? "don't know" : b.toConfLine()));
        }
        private void testEnum(Expression e, MethodBinding binding) {
                if (binding.parameters.length != 0) {