Added java support code for stx:libjava/libtool development
authorJan Vrany <jan.vrany@fit.cvut.cz>
Thu, 03 Oct 2013 17:36:36 +0100
branchdevelopment
changeset 2786 241e36a125a9
parent 2785 0216e0808608
child 2787 d4914ffbb0cf
Added java support code for stx:libjava/libtool
tools/java/.classpath
tools/java/src-tests/stx/libjava/tools/text/tests/HighlighterTests.java
tools/java/src/stx/libjava/tools/environment/Resolver.java
tools/java/src/stx/libjava/tools/text/Highlighter.java
tools/java/src/stx/libjava/tools/text/Indexer.java
tools/java/src/stx/libjava/tools/text/Marker.java
--- a/tools/java/.classpath	Thu Oct 03 15:30:51 2013 +0100
+++ b/tools/java/.classpath	Thu Oct 03 17:36:36 2013 +0100
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
 	<classpathentry kind="src" path="src"/>
+	<classpathentry kind="src" path="src-tests"/>
 	<classpathentry combineaccessrules="false" exported="true" kind="src" path="/stx.libjava"/>
 	<classpathentry combineaccessrules="false" exported="true" kind="src" path="/stx.libjava.libs"/>
 	<classpathentry exported="true" kind="lib" path="libs/org.eclipse.jdt.core_3.8.3.v20130121-145325.jar" sourcepath="libs-src/org.eclipse.jdt.core.source_3.8.3.v20130121-145325.jar"/>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/java/src-tests/stx/libjava/tools/text/tests/HighlighterTests.java	Thu Oct 03 17:36:36 2013 +0100
@@ -0,0 +1,76 @@
+package stx.libjava.tools.text.tests;
+
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+
+import org.eclipse.jdt.internal.compiler.ast.ASTNode;
+import org.junit.Test;
+import org.junit.Before;
+
+import stx.libjava.tools.Source;
+import stx.libjava.tools.text.Highlighter;
+import stx.libjava.tools.text.Indexer;
+import stx.libjava.tools.text.Marker;
+
+public class HighlighterTests {
+        
+    protected static class MarkerEntry {        
+        public int kind;
+        public int from;
+        public int to;
+        
+        public MarkerEntry(int kind, int from, int to) {
+            this.kind = kind;
+            this.from = from;
+            this.to = to;
+        }
+    }
+    
+    protected static class IndexEntry {
+        public ASTNode node;
+        public int from;
+        public int to;
+        
+        public IndexEntry(ASTNode node, int from, int to) {
+            this.node = node;
+            this.from = from;
+            this.to = to;
+        }
+
+    }
+    
+    protected ArrayList<MarkerEntry> markerLog;
+    protected ArrayList<IndexEntry> indexLog;
+    
+        
+    protected void parse(String code) {
+        Highlighter h = new Highlighter();
+        h.setMarker(new Marker() {
+           public void mark(int kind, int from, int to) {
+               markerLog.add(new MarkerEntry(kind, from,to));
+           }
+        });
+        h.setIndexer(new Indexer() {
+            public void index(ASTNode node, int from, int to) {
+                indexLog.add(new IndexEntry(node, from,to));
+            }
+        });
+        h.parse(new Source(code), false);
+    }
+    
+    
+    @Before
+    public void setup() {
+        markerLog = new ArrayList<MarkerEntry>();
+        indexLog = new ArrayList<IndexEntry>();
+    }
+
+    @Test
+    public void test_01() {
+        parse("public class X { public void foo() { bar().baz(); } }");
+        
+        assertEquals(2, indexLog.size());
+    }
+
+}
--- a/tools/java/src/stx/libjava/tools/environment/Resolver.java	Thu Oct 03 15:30:51 2013 +0100
+++ b/tools/java/src/stx/libjava/tools/environment/Resolver.java	Thu Oct 03 17:36:36 2013 +0100
@@ -1,7 +1,6 @@
 package stx.libjava.tools.environment;
 
 import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
-import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
 import org.eclipse.jdt.internal.compiler.env.AccessRestriction;
@@ -11,7 +10,6 @@
 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
 import org.eclipse.jdt.internal.compiler.impl.ITypeRequestor;
 import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
-import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
@@ -55,6 +53,13 @@
         }
         td.binding.methods();
         td.binding.fields();
+        /*
+        if (td.methods != null) {
+            for (int i = 0; i < td.methods.length; i++) {
+                td.methods[i].resolveStatements();
+            }         
+        }
+        */
     }
 
     @Override
--- a/tools/java/src/stx/libjava/tools/text/Highlighter.java	Thu Oct 03 15:30:51 2013 +0100
+++ b/tools/java/src/stx/libjava/tools/text/Highlighter.java	Thu Oct 03 17:36:36 2013 +0100
@@ -1,35 +1,36 @@
 package stx.libjava.tools.text;
 
-import net.sf.jasperreports.components.sort.FieldFilter;
 
 import org.eclipse.jdt.core.compiler.InvalidInputException;
 import org.eclipse.jdt.internal.compiler.ASTVisitor;
-import org.eclipse.jdt.internal.compiler.CompilationResult;
 import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
 import org.eclipse.jdt.internal.compiler.ast.ASTNode;
+import org.eclipse.jdt.internal.compiler.ast.Argument;
 import org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference;
-import org.eclipse.jdt.internal.compiler.ast.ArrayReference;
 import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
+import org.eclipse.jdt.internal.compiler.ast.Assignment;
 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.FieldReference;
+import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
 import org.eclipse.jdt.internal.compiler.ast.MessageSend;
 import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
 import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
+import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
 import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
+import org.eclipse.jdt.internal.compiler.lookup.Binding;
 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
 import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
 import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
 import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
-import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
 import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
 
-import stx.libjava.tools.environment.Resolver;
 import stx.libjava.tools.parser.Parser;
 
 public class Highlighter extends Parser {
@@ -39,15 +40,10 @@
     
     public Highlighter() {
         this(new Marker() {            
-            @Override
-            public void mark(int kind, int from, int to) {
-            }
+           
         },
         new Indexer() {
-            
-            @Override
-            public void index(ASTNode node, int from, int to) {                
-            }
+                       
         });
     }
     
@@ -150,34 +146,51 @@
                      
         public void endVisit(FieldDeclaration fieldDeclaration, MethodScope scope) {
             marker.mark(Marker.MARK_FIELD, fieldDeclaration.sourceStart, fieldDeclaration.sourceEnd);
-            indexer.index(fieldDeclaration, fieldDeclaration.sourceStart, fieldDeclaration.sourceEnd);
+            indexer.addFieldDeclaration(fieldDeclaration, fieldDeclaration.sourceStart, fieldDeclaration.sourceEnd);
+        }
+        
+        public void endVisit(LocalDeclaration localDeclaration, BlockScope scope) {
+            marker.mark(Marker.MARK_LOCAL, localDeclaration.sourceStart, localDeclaration.sourceEnd);
+            indexer.addLocalDeclaration(localDeclaration, localDeclaration.sourceStart, localDeclaration.sourceEnd);
         }
         
+        public void endVisit(Argument localDeclaration, BlockScope scope) {
+            marker.mark(Marker.MARK_LOCAL, localDeclaration.sourceStart, localDeclaration.sourceEnd);
+            indexer.addLocalDeclaration(localDeclaration, localDeclaration.sourceStart, localDeclaration.sourceEnd);
+        }
+
+        
         public void endVisit(FieldReference fieldReference, BlockScope scope) {
             int start = (int)(fieldReference.nameSourcePosition >>> 32);
-            int stop  = (int)(fieldReference.nameSourcePosition & 0x0000FFFF);
+            int stop  = (int)(fieldReference.nameSourcePosition & 0x0000FFFFFFFF);
             marker.mark(Marker.MARK_FIELD, start, stop);
-            indexer.index(fieldReference, start, stop);
+            indexer.addVariableReference(fieldReference, start, stop);
         }
         public void endVisit(FieldReference fieldReference, ClassScope scope) {
             int start = (int)(fieldReference.nameSourcePosition >>> 32);
-            int stop  = (int)(fieldReference.nameSourcePosition & 0x0000FFFF);
+            int stop  = (int)(fieldReference.nameSourcePosition & 0x0000FFFFFFFF);
             marker.mark(Marker.MARK_FIELD, start, stop);
-            indexer.index(fieldReference, start, stop);
+            indexer.addVariableReference(fieldReference, start, stop);
         }
         
                 
         public void endVisit(MessageSend messageSend, BlockScope scope) {
             int start = (int)(messageSend.nameSourcePosition >>> 32);
-            int stop  = (int)(messageSend.nameSourcePosition & 0x0000FFFF);
+            int stop  = (int)(messageSend.nameSourcePosition & 0x0000FFFFFFFF);
             marker.mark(Marker.MARK_SELECTOR, start, stop);
-            indexer.index(messageSend, start, stop);                                                                        
+            indexer.addMessageSend(messageSend, start, stop);                                                                        
+        }
+        
+        public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) {
+            int start = methodDeclaration.sourceStart;
+            int stop  = start + methodDeclaration.selector.length - 1;
+            marker.mark(Marker.MARK_SELECTOR, start, stop);
+            indexer.methodEnter(methodDeclaration);
+            return true;
         }
         
         public void endVisit(MethodDeclaration methodDeclaration, ClassScope scope) {
-            int start = methodDeclaration.sourceStart;
-            int stop  = start + methodDeclaration.selector.length - 1;
-            marker.mark(Marker.MARK_SELECTOR, start, stop);            
+            indexer.methodLeave(methodDeclaration);
         }
         
         public void endVisit(
@@ -193,7 +206,7 @@
             if (type.token == TypeBinding.INT.simpleName) return;
             if (type.token == TypeBinding.LONG.simpleName) return;
             marker.mark(Marker.MARK_CLASS, type.sourceStart, type.sourceEnd);
-            indexer.index(type, type.sourceStart, type.sourceEnd);
+            indexer.addTypeReference(type, type.sourceStart, type.sourceEnd);
 
         }
         public void endVisit(
@@ -209,7 +222,7 @@
             if (type.token == TypeBinding.INT.simpleName) return;
             if (type.token == TypeBinding.LONG.simpleName) return;
             marker.mark(Marker.MARK_CLASS, type.sourceStart, type.sourceEnd);
-            indexer.index(type, type.sourceStart, type.sourceEnd);
+            indexer.addTypeReference(type, type.sourceStart, type.sourceEnd);
 
         }
         
@@ -224,7 +237,7 @@
             if (type.token == TypeBinding.INT.simpleName) return;
             if (type.token == TypeBinding.LONG.simpleName) return;
             marker.mark(Marker.MARK_CLASS, type.sourceStart, type.sourceEnd);
-            indexer.index(type, type.sourceStart, type.sourceEnd);
+            indexer.addTypeReference(type, type.sourceStart, type.sourceEnd);
         }
         public void endVisit(ArrayTypeReference type, ClassScope scope) {
             if (type.token == TypeBinding.VOID.simpleName) return;
@@ -237,40 +250,92 @@
             if (type.token == TypeBinding.INT.simpleName) return;
             if (type.token == TypeBinding.LONG.simpleName) return;
             marker.mark(Marker.MARK_CLASS, type.sourceStart, type.sourceEnd);
-            indexer.index(type, type.sourceStart, type.sourceEnd);
+            indexer.addTypeReference(type, type.sourceStart, type.sourceEnd);
+        }
+        
+        protected void breakpoint() {}
+        
+        public void endVisit(
+                SingleNameReference ref,
+                BlockScope scope) {
+            if (ref.binding == null) {
+                breakpoint();
+                ref.resolve(scope);
+            }
+            
+            boolean isField = (ref.bits & ASTNode.RestrictiveFlagMASK) == Binding.FIELD;
+            if (isField) {
+                marker.mark(Marker.MARK_FIELD, ref.sourceStart,  ref.sourceEnd);
+            }
+            indexer.addVariableReference(ref, ref.sourceStart,  ref.sourceEnd);                      
         }
+        
+        public void endVisit(
+                QualifiedNameReference ref,
+                BlockScope scope) {
+            if (ref.binding == null) ref.resolve(scope);
+/*            
+            Binding[] bindings = ref.getOtherFieldBindings(scope);
+            for (int i = 0; i < bindings.length; i++) {
+                Binding
+            }
+            
+            boolean isField = (ref.bits & ASTNode.RestrictiveFlagMASK) == Binding.FIELD;
+            if (isField) {
+                marker.mark(Marker.MARK_FIELD, ref.sourceStart,  ref.sourceEnd);
+            }
+            indexer.addVariableReference(ref, ref.sourceStart,  ref.sourceEnd);
+*/
+        }
+        
+        public void endVisit(Assignment assignment, BlockScope scope) {
+            if ((assignment.lhs.bits & ASTNode.RestrictiveFlagMASK) == Binding.FIELD) {
+                marker.mark(Marker.MARK_FIELD_ASSIGNED, assignment.lhs.sourceStart,  assignment.lhs.sourceEnd);
+            }
+        }
+        
+         
 
         
         public void endVisit(
                 ArrayQualifiedTypeReference type,
                 BlockScope scope) {
             marker.mark(Marker.MARK_CLASS, type.sourceStart, type.sourceEnd);
-            indexer.index(type, type.sourceStart, type.sourceEnd);
+            indexer.addTypeReference(type, type.sourceStart, type.sourceEnd);
         }
         public void endVisit(
                 ArrayQualifiedTypeReference type,
                 ClassScope scope) {
             marker.mark(Marker.MARK_CLASS, type.sourceStart, type.sourceEnd);
-            indexer.index(type, type.sourceStart, type.sourceEnd);                
+            indexer.addTypeReference(type, type.sourceStart, type.sourceEnd);                
         }
         
         public void endVisit(
                 QualifiedTypeReference type,
-                BlockScope scope) {
+                BlockScope scope) {            
             marker.mark(Marker.MARK_CLASS, type.sourceStart, type.sourceEnd);
-            indexer.index(type, type.sourceStart, type.sourceEnd);                
+            indexer.addTypeReference(type, type.sourceStart, type.sourceEnd);                
 
         }
         public void endVisit(
                 QualifiedTypeReference type,
                 ClassScope scope) {
             marker.mark(Marker.MARK_CLASS, type.sourceStart, type.sourceEnd);
-            indexer.index(type, type.sourceStart, type.sourceEnd);                
+            indexer.addTypeReference(type, type.sourceStart, type.sourceEnd);                
         }
         
+        public boolean visit(
+                ConstructorDeclaration constructorDeclaration,
+                ClassScope scope) {
+            indexer.methodEnter(constructorDeclaration);
+            return true;
+        }
+
+        
         public void endVisit(
                 ConstructorDeclaration constructorDeclaration,
                 ClassScope scope) {
+            indexer.methodLeave(constructorDeclaration);
             int start = constructorDeclaration.sourceStart;
             int stop  = start + constructorDeclaration.selector.length - 1;
             marker.mark(Marker.MARK_SELECTOR, start, stop);
@@ -327,7 +392,8 @@
             case TokenNamecase:
             case TokenNamereturn:
                 marker.mark(Marker.MARK_KEYWORD_FLOW, getCurrentTokenStartPosition(), getCurrentTokenEndPosition());
-                break;                                            
+                break;           
+            case TokenNameabstract:
             case TokenNameassert:
             case TokenNameboolean:            
             case TokenNamebyte:            
--- a/tools/java/src/stx/libjava/tools/text/Indexer.java	Thu Oct 03 15:30:51 2013 +0100
+++ b/tools/java/src/stx/libjava/tools/text/Indexer.java	Thu Oct 03 17:36:36 2013 +0100
@@ -1,6 +1,13 @@
 package stx.libjava.tools.text;
 
-import org.eclipse.jdt.internal.compiler.ast.ASTNode;
+import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.MessageSend;
+import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
+import org.eclipse.jdt.internal.compiler.ast.Reference;
+import org.eclipse.jdt.internal.compiler.ast.TypeReference;
+
 
 public abstract class Indexer {
 
@@ -8,7 +15,47 @@
      * Add given @param node to the index. 
      *  
      * @param node node to add to the index.
+     * @param from start position of the node (its relevant part for highlighting/navigation)
+     * @param to stop position of the node (its relevant part for highlighting/navigation)
+     *   
      */
-    public abstract void index(ASTNode node, int from, int to);
+    public void addFieldDeclaration(FieldDeclaration node, int from, int to) {}
+    
+    /**
+     * Same as {@link #addFieldDeclaration(FieldDeclaration, int, int)} but for local variables
+     * (temporaries and arguments) 
+     */
+    public void addLocalDeclaration(LocalDeclaration node, int from, int to) {}
+
+    
+    /**
+     * Same as {@link #addFieldDeclaration(FieldDeclaration, int, int)} but for types 
+     */
+    public void addTypeReference(TypeReference node, int from, int to) {}
+
+    /**
+     * Same as {@link #addFieldDeclaration(FieldDeclaration, int, int)} but for name references
+     * (variables, fields)
+     */
+    public void addVariableReference(Reference node, int from, int to) {}
+    
+    /**
+     * Same as {@link #addFieldDeclaration(FieldDeclaration, int, int)} but for method
+     * invocations
+     */
+    public void addMessageSend(MessageSend node, int from, int to) {}       
+        
+    /**
+     * Called whenever a method is entered, i.e., before its arguments and
+     * statements are visited.   
+     */
+    public void methodEnter(AbstractMethodDeclaration method) {}
+    
+    /**
+     * Called whenever a method is left, i.e., after all its arguments and 
+     * statements are visited.   
+     */
+    public void methodLeave(AbstractMethodDeclaration method) {}
+    
 
 }
--- a/tools/java/src/stx/libjava/tools/text/Marker.java	Thu Oct 03 15:30:51 2013 +0100
+++ b/tools/java/src/stx/libjava/tools/text/Marker.java	Thu Oct 03 17:36:36 2013 +0100
@@ -24,5 +24,5 @@
     public static final int MARK_CLASS          = 12;
     
 
-    public abstract void mark(int kind, int from, int to);
+    public void mark(int kind, int from, int to) {}
 }