Commit 4a71f35c authored by Clemens Pflaum's avatar Clemens Pflaum

Detect all variable positions and save to HashMap

parent 3a740c2a
......@@ -6,6 +6,7 @@ import java.util.List;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
......@@ -89,13 +90,16 @@ public class JavaCompiler {
node.accept(new LocalVariableDetector());
break;
case ActionCode:
blob="public class DummyClass{\n\tpublic void dummymethod("+"Integer e"+")\n\t{"+blob+"\n\t}\n}";
blob="public class DummyClass{\n\tpublic void dummymethod("+"Integer e, Integer f"+")\n\t{"+blob+"\n\t}\n}";
parser.setEnvironment(new String[] {""}, new String[] {""}, new String[] { "UTF-8"}, true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(blob.toCharArray());
parser.setResolveBindings(true);
node = parser.createAST(null);
node.accept(new LocalVariableDetector());
ASTVisitor visitor = new LocalVariableDetector();
node.accept(visitor);
break;
default:
throw new UnsupportedOperationException("java parsing not implemented: unknown blocktype");
......
package de.in.tum.www2.cup.ast;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
import org.eclipse.jface.text.Position;
public class LocalVariableDetector extends ASTVisitor {
HashMap<IVariableBinding, LinkedList<Position>> variableMap = new HashMap<IVariableBinding, LinkedList<Position>>();
@Override
public boolean visit(FieldDeclaration node) {
System.out.println("buhu");
for (Iterator iter = node.fragments().iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
IBinding test= fragment.resolveBinding();
System.out.println("Juhu");
IVariableBinding binding= (IVariableBinding) fragment.resolveBinding();
variableMap.put( binding, new LinkedList<Position>());
System.out.println("found FieldDeclaration " + fragment.getName().getIdentifier());
}
return false;
return true;
}
@Override
public boolean visit(VariableDeclarationStatement node) {
for (Iterator iter = node.fragments().iterator(); iter.hasNext();) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
IBinding test= fragment.resolveBinding();
IVariableBinding binding= (IVariableBinding) fragment.resolveBinding();
variableMap.put( binding, new LinkedList<Position>());
System.out.println("found VariableDeclarationStatement " + fragment.getName().getIdentifier());
}
return false;
}
@Override
public boolean visit(SingleVariableDeclaration node) {
IVariableBinding binding = (IVariableBinding) node.resolveBinding();
variableMap.put( binding, new LinkedList<Position>());
System.out.println("found SingleVariableDeclaration " + binding.getName());
System.out.println("Juhu");
return true;
}
@Override
public boolean visit(SimpleName node) {
IBinding nodeBinding = node.resolveBinding();
if(nodeBinding instanceof IVariableBinding){
IVariableBinding binding = (IVariableBinding) nodeBinding;
if(variableMap.containsKey(binding)){
variableMap.get(binding).add(new Position(node.getStartPosition(), node.getLength()));
}
System.out.println("found variableBinding " + nodeBinding.getName());
}else{
System.out.println("found SimpleName " + node.getIdentifier());
}
return false;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment