001: package visualdebugger.views;
002:
003: import java.util.HashSet;
004: import java.util.List;
005:
006: import org.eclipse.jdt.core.dom.*;
007:
008: import de.uka.ilkd.key.visualdebugger.VisualDebugger;
009:
010: public class InsertSepVisitor extends ASTVisitor {
011:
012: public static void replaceNode(ASTNode oldNode, ASTNode newNode) {
013: ASTNode parent = oldNode.getParent();
014: StructuralPropertyDescriptor location = oldNode
015: .getLocationInParent();
016: if (location.isChildProperty()) {
017: parent.setStructuralProperty(location, newNode);
018: } else if (location.isChildListProperty()) {
019: List list = (List) parent.getStructuralProperty(location);
020: int index = list.indexOf(oldNode);
021: list.set(index, newNode);
022: }
023: }
024:
025: private int id = 0;
026:
027: private final HashSet types = new HashSet();
028:
029: public void endVisit(ArrayAccess node) {
030: Expression index = node.getIndex();
031: MethodInvocation inv = getSepStatement(index.getAST(), ++id,
032: index);
033: replaceNode(index, inv);
034: }
035:
036: public void endVisit(FieldAccess node) {
037: final ITypeBinding expressionTypeBinding = node.getExpression()
038: .resolveTypeBinding();
039: if (expressionTypeBinding != null) {
040:
041: types.add(expressionTypeBinding);
042:
043: replaceNode(node.getExpression(), getSepStatement(node
044: .getAST(), ++id, node.getExpression()));
045: }
046: }
047:
048: public void endVisit(ForStatement node) {
049: final Expression guard = node.getExpression();
050: replaceNode(guard, getSepStatement(guard.getAST(), ++id, guard));
051: }
052:
053: public void endVisit(QualifiedName node) {
054:
055: if (node.getParent() instanceof QualifiedName) {
056: return;
057: }
058:
059: final Name qualifier = node.getQualifier();
060:
061: final IBinding qualifierBinding = qualifier.resolveBinding();
062:
063: if (qualifierBinding.getKind() == IBinding.PACKAGE) {
064: return;
065: }
066:
067: final IBinding simpleNameBinding = node.getName()
068: .resolveBinding();
069: if (simpleNameBinding == null
070: || Modifier.isStatic(simpleNameBinding.getModifiers())) {
071: return;
072: }
073:
074: if (qualifier.resolveTypeBinding() == null
075: || qualifier.resolveTypeBinding().isArray()) {
076: return;
077: }
078:
079: types.add(qualifier.resolveTypeBinding());
080:
081: MethodInvocation inv = getSepStatement(qualifier.getAST(),
082: ++id, qualifier);
083:
084: FieldAccess fa = node.getAST().newFieldAccess();
085:
086: fa.setName(node.getAST().newSimpleName(
087: node.getName().toString()));
088: fa.setExpression(inv);
089:
090: replaceNode(node, fa);
091: }
092:
093: public void endVisit(WhileStatement node) {
094: final Expression guard = node.getExpression();
095: MethodInvocation inv = getSepStatement(guard.getAST(), ++id,
096: guard);
097: replaceNode(guard, inv);
098: }
099:
100: private ExpressionStatement getSepStatement(AST ast, int id) {
101: MethodInvocation methodInvocation = ast.newMethodInvocation();
102:
103: methodInvocation.setExpression(ast
104: .newSimpleName(VisualDebugger.debugClass));
105: methodInvocation.setName(ast
106: .newSimpleName(VisualDebugger.sepName));
107:
108: NumberLiteral literal = ast.newNumberLiteral("" + id);
109: methodInvocation.arguments().add(literal);
110: ExpressionStatement expressionStatement = ast
111: .newExpressionStatement(methodInvocation);
112: return expressionStatement;
113: }
114:
115: private MethodInvocation getSepStatement(AST ast, int id,
116: Expression ex) {
117: MethodInvocation methodInvocation = ast.newMethodInvocation();
118: methodInvocation.setExpression(ast
119: .newSimpleName(VisualDebugger.debugClass));
120: // methodInvocation.setExpression(null);
121: methodInvocation.setName(ast
122: .newSimpleName(VisualDebugger.sepName));
123: NumberLiteral literal = ast.newNumberLiteral("" + id);
124: ASTNode ex2 = ASTNode.copySubtree(ast, ex);
125:
126: ASTParser parser = ASTParser.newParser(AST.JLS3);
127: parser.setKind(ASTParser.K_EXPRESSION);
128: parser.setSource(ex.toString().toCharArray()); // set source
129: // parser.setResolveBindings(true); // we need bindings later on
130: ex2 = (ASTNode.copySubtree(ast, (Expression) parser
131: .createAST(null /* IProgressMonitor */)));
132: // TODO !!!!!!!!!!!!!!!!!!!!!!!!
133:
134: methodInvocation.arguments().add(literal);
135: methodInvocation.arguments().add((Expression) ex2);
136:
137: return methodInvocation;
138: }
139:
140: public HashSet getTypes() {
141: return types;
142: }
143:
144: public boolean visit(ASTNode node) {
145: return true;
146: }
147:
148: public boolean visit(Block node) {
149: AST ast = node.getAST();
150: for (int i = 0; i < node.statements().size(); i++) {
151: id++;
152: if (!(node.statements().get(i) instanceof SuperMethodInvocation)
153: && !(node.statements().get(i) instanceof ConstructorInvocation))
154: node.statements().add(i, getSepStatement(ast, id));
155: i++;
156: }
157: return true;
158: }
159:
160: }
|