0001: /*
0002: * Spoon - http://spoon.gforge.inria.fr/
0003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
0004: *
0005: * This software is governed by the CeCILL-C License under French law and
0006: * abiding by the rules of distribution of free software. You can use, modify
0007: * and/or redistribute the software under the terms of the CeCILL-C license as
0008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
0009: *
0010: * This program is distributed in the hope that it will be useful, but WITHOUT
0011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
0013: *
0014: * The fact that you are presently reading this means that you have had
0015: * knowledge of the CeCILL-C license and that you accept its terms.
0016: */
0017:
0018: package spoon.support.builder;
0019:
0020: import java.util.ArrayList;
0021: import java.util.List;
0022: import java.util.Map;
0023: import java.util.Set;
0024: import java.util.Stack;
0025: import java.util.TreeMap;
0026: import java.util.TreeSet;
0027:
0028: import org.eclipse.jdt.internal.compiler.ASTVisitor;
0029: import org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression;
0030: import org.eclipse.jdt.internal.compiler.ast.ASTNode;
0031: import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
0032: import org.eclipse.jdt.internal.compiler.ast.AllocationExpression;
0033: import org.eclipse.jdt.internal.compiler.ast.Annotation;
0034: import org.eclipse.jdt.internal.compiler.ast.AnnotationMethodDeclaration;
0035: import org.eclipse.jdt.internal.compiler.ast.Argument;
0036: import org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression;
0037: import org.eclipse.jdt.internal.compiler.ast.ArrayInitializer;
0038: import org.eclipse.jdt.internal.compiler.ast.ArrayReference;
0039: import org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference;
0040: import org.eclipse.jdt.internal.compiler.ast.AssertStatement;
0041: import org.eclipse.jdt.internal.compiler.ast.Assignment;
0042: import org.eclipse.jdt.internal.compiler.ast.BinaryExpression;
0043: import org.eclipse.jdt.internal.compiler.ast.Block;
0044: import org.eclipse.jdt.internal.compiler.ast.BreakStatement;
0045: import org.eclipse.jdt.internal.compiler.ast.CaseStatement;
0046: import org.eclipse.jdt.internal.compiler.ast.CastExpression;
0047: import org.eclipse.jdt.internal.compiler.ast.CharLiteral;
0048: import org.eclipse.jdt.internal.compiler.ast.ClassLiteralAccess;
0049: import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
0050: import org.eclipse.jdt.internal.compiler.ast.CompoundAssignment;
0051: import org.eclipse.jdt.internal.compiler.ast.ConditionalExpression;
0052: import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration;
0053: import org.eclipse.jdt.internal.compiler.ast.ContinueStatement;
0054: import org.eclipse.jdt.internal.compiler.ast.DoStatement;
0055: import org.eclipse.jdt.internal.compiler.ast.DoubleLiteral;
0056: import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
0057: import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
0058: import org.eclipse.jdt.internal.compiler.ast.Expression;
0059: import org.eclipse.jdt.internal.compiler.ast.ExtendedStringLiteral;
0060: import org.eclipse.jdt.internal.compiler.ast.FalseLiteral;
0061: import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
0062: import org.eclipse.jdt.internal.compiler.ast.FieldReference;
0063: import org.eclipse.jdt.internal.compiler.ast.FloatLiteral;
0064: import org.eclipse.jdt.internal.compiler.ast.ForStatement;
0065: import org.eclipse.jdt.internal.compiler.ast.ForeachStatement;
0066: import org.eclipse.jdt.internal.compiler.ast.IfStatement;
0067: import org.eclipse.jdt.internal.compiler.ast.Initializer;
0068: import org.eclipse.jdt.internal.compiler.ast.InstanceOfExpression;
0069: import org.eclipse.jdt.internal.compiler.ast.IntLiteral;
0070: import org.eclipse.jdt.internal.compiler.ast.Javadoc;
0071: import org.eclipse.jdt.internal.compiler.ast.LabeledStatement;
0072: import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
0073: import org.eclipse.jdt.internal.compiler.ast.LongLiteral;
0074: import org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation;
0075: import org.eclipse.jdt.internal.compiler.ast.MemberValuePair;
0076: import org.eclipse.jdt.internal.compiler.ast.MessageSend;
0077: import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
0078: import org.eclipse.jdt.internal.compiler.ast.NormalAnnotation;
0079: import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
0080: import org.eclipse.jdt.internal.compiler.ast.OR_OR_Expression;
0081: import org.eclipse.jdt.internal.compiler.ast.OperatorIds;
0082: import org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference;
0083: import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference;
0084: import org.eclipse.jdt.internal.compiler.ast.PostfixExpression;
0085: import org.eclipse.jdt.internal.compiler.ast.PrefixExpression;
0086: import org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression;
0087: import org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference;
0088: import org.eclipse.jdt.internal.compiler.ast.QualifiedThisReference;
0089: import org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference;
0090: import org.eclipse.jdt.internal.compiler.ast.ReturnStatement;
0091: import org.eclipse.jdt.internal.compiler.ast.SingleMemberAnnotation;
0092: import org.eclipse.jdt.internal.compiler.ast.SingleNameReference;
0093: import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
0094: import org.eclipse.jdt.internal.compiler.ast.Statement;
0095: import org.eclipse.jdt.internal.compiler.ast.StringLiteral;
0096: import org.eclipse.jdt.internal.compiler.ast.StringLiteralConcatenation;
0097: import org.eclipse.jdt.internal.compiler.ast.SuperReference;
0098: import org.eclipse.jdt.internal.compiler.ast.SwitchStatement;
0099: import org.eclipse.jdt.internal.compiler.ast.SynchronizedStatement;
0100: import org.eclipse.jdt.internal.compiler.ast.ThisReference;
0101: import org.eclipse.jdt.internal.compiler.ast.ThrowStatement;
0102: import org.eclipse.jdt.internal.compiler.ast.TrueLiteral;
0103: import org.eclipse.jdt.internal.compiler.ast.TryStatement;
0104: import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
0105: import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
0106: import org.eclipse.jdt.internal.compiler.ast.TypeReference;
0107: import org.eclipse.jdt.internal.compiler.ast.UnaryExpression;
0108: import org.eclipse.jdt.internal.compiler.ast.WhileStatement;
0109: import org.eclipse.jdt.internal.compiler.ast.Wildcard;
0110: import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
0111: import org.eclipse.jdt.internal.compiler.lookup.ArrayBinding;
0112: import org.eclipse.jdt.internal.compiler.lookup.BaseTypeBinding;
0113: import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding;
0114: import org.eclipse.jdt.internal.compiler.lookup.Binding;
0115: import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
0116: import org.eclipse.jdt.internal.compiler.lookup.CaptureBinding;
0117: import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
0118: import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope;
0119: import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
0120: import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
0121: import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
0122: import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
0123: import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
0124: import org.eclipse.jdt.internal.compiler.lookup.ParameterizedTypeBinding;
0125: import org.eclipse.jdt.internal.compiler.lookup.RawTypeBinding;
0126: import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
0127: import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
0128: import org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding;
0129: import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
0130: import org.eclipse.jdt.internal.compiler.lookup.WildcardBinding;
0131:
0132: import spoon.reflect.CoreFactory;
0133: import spoon.reflect.Factory;
0134: import spoon.reflect.code.BinaryOperatorKind;
0135: import spoon.reflect.code.CtArrayAccess;
0136: import spoon.reflect.code.CtAssert;
0137: import spoon.reflect.code.CtAssignment;
0138: import spoon.reflect.code.CtBinaryOperator;
0139: import spoon.reflect.code.CtBlock;
0140: import spoon.reflect.code.CtBreak;
0141: import spoon.reflect.code.CtCase;
0142: import spoon.reflect.code.CtCatch;
0143: import spoon.reflect.code.CtConditional;
0144: import spoon.reflect.code.CtContinue;
0145: import spoon.reflect.code.CtDo;
0146: import spoon.reflect.code.CtExpression;
0147: import spoon.reflect.code.CtFieldAccess;
0148: import spoon.reflect.code.CtFor;
0149: import spoon.reflect.code.CtForEach;
0150: import spoon.reflect.code.CtIf;
0151: import spoon.reflect.code.CtInvocation;
0152: import spoon.reflect.code.CtLiteral;
0153: import spoon.reflect.code.CtLocalVariable;
0154: import spoon.reflect.code.CtLoop;
0155: import spoon.reflect.code.CtNewArray;
0156: import spoon.reflect.code.CtNewClass;
0157: import spoon.reflect.code.CtOperatorAssignment;
0158: import spoon.reflect.code.CtReturn;
0159: import spoon.reflect.code.CtStatement;
0160: import spoon.reflect.code.CtSwitch;
0161: import spoon.reflect.code.CtSynchronized;
0162: import spoon.reflect.code.CtTargetedExpression;
0163: import spoon.reflect.code.CtThrow;
0164: import spoon.reflect.code.CtTry;
0165: import spoon.reflect.code.CtUnaryOperator;
0166: import spoon.reflect.code.CtVariableAccess;
0167: import spoon.reflect.code.CtWhile;
0168: import spoon.reflect.code.UnaryOperatorKind;
0169: import spoon.reflect.cu.CompilationUnit;
0170: import spoon.reflect.declaration.CtAnnotation;
0171: import spoon.reflect.declaration.CtAnonymousExecutable;
0172: import spoon.reflect.declaration.CtClass;
0173: import spoon.reflect.declaration.CtConstructor;
0174: import spoon.reflect.declaration.CtElement;
0175: import spoon.reflect.declaration.CtEnum;
0176: import spoon.reflect.declaration.CtExecutable;
0177: import spoon.reflect.declaration.CtField;
0178: import spoon.reflect.declaration.CtGenericElement;
0179: import spoon.reflect.declaration.CtInterface;
0180: import spoon.reflect.declaration.CtMethod;
0181: import spoon.reflect.declaration.CtPackage;
0182: import spoon.reflect.declaration.CtParameter;
0183: import spoon.reflect.declaration.CtSimpleType;
0184: import spoon.reflect.declaration.CtType;
0185: import spoon.reflect.declaration.CtTypedElement;
0186: import spoon.reflect.declaration.CtVariable;
0187: import spoon.reflect.declaration.ModifierKind;
0188: import spoon.reflect.reference.CtArrayTypeReference;
0189: import spoon.reflect.reference.CtExecutableReference;
0190: import spoon.reflect.reference.CtFieldReference;
0191: import spoon.reflect.reference.CtLocalVariableReference;
0192: import spoon.reflect.reference.CtPackageReference;
0193: import spoon.reflect.reference.CtParameterReference;
0194: import spoon.reflect.reference.CtTypeParameterReference;
0195: import spoon.reflect.reference.CtTypeReference;
0196: import spoon.reflect.reference.CtVariableReference;
0197: import spoon.reflect.visitor.CtInheritanceScanner;
0198: import spoon.reflect.visitor.Query;
0199: import spoon.reflect.visitor.filter.TypeFilter;
0200: import spoon.template.Template;
0201:
0202: /**
0203: * A visitor for iterating through the parse tree.
0204: */
0205: public class JDTTreeBuilder extends ASTVisitor {
0206:
0207: public class ASTPair {
0208: public CtElement element;
0209:
0210: public ASTNode node;
0211:
0212: public ASTPair(CtElement element, ASTNode node) {
0213: super ();
0214: this .element = element;
0215: this .node = node;
0216: }
0217: }
0218:
0219: public class BuilderContext {
0220: Stack<String> annotationValueName = new Stack<String>();
0221:
0222: Stack<CtElement> arguments = new Stack<CtElement>();
0223:
0224: Stack<CtElement> arrayInitializer = new Stack<CtElement>();
0225:
0226: List<CtTypeReference<?>> casts = new ArrayList<CtTypeReference<?>>();
0227:
0228: CompilationUnitDeclaration compilationunitdeclaration;
0229:
0230: List<CtSimpleType<?>> createdTypes = new ArrayList<CtSimpleType<?>>();
0231:
0232: Stack<CtTry> finallyzer = new Stack<CtTry>();
0233:
0234: boolean forinit = false;
0235:
0236: boolean forupdate = false;
0237:
0238: Stack<String> label = new Stack<String>();
0239:
0240: boolean selector = false;
0241:
0242: /**
0243: * Stack of all parents elements
0244: */
0245: Stack<ASTPair> stack = new Stack<ASTPair>();
0246:
0247: Stack<CtTargetedExpression<?, ?>> target = new Stack<CtTargetedExpression<?, ?>>();
0248:
0249: public void addCreatedType(CtSimpleType<?> type) {
0250: createdTypes.add(type);
0251: }
0252:
0253: @SuppressWarnings("unchecked")
0254: void enter(CtElement e, ASTNode node) {
0255: stack.push(new ASTPair(e, node));
0256: // aststack.push(node);
0257: if (compilationunitdeclaration != null) {
0258: CoreFactory cf = factory.Core();
0259: int sourceStart = node.sourceStart;
0260: int sourceEnd = node.sourceEnd;
0261: if ((e instanceof CtBlock)
0262: && (node instanceof MethodDeclaration)) {
0263: sourceStart = ((MethodDeclaration) node).bodyStart;
0264: sourceEnd = ((MethodDeclaration) node).bodyEnd;
0265: }
0266: CompilationUnit cu = factory.CompilationUnit().create(
0267: new String(compilationunitdeclaration
0268: .getFileName()));
0269: e
0270: .setPosition(cf
0271: .createSourcePosition(
0272: cu,
0273: sourceStart,
0274: sourceEnd,
0275: compilationunitdeclaration.compilationResult.lineSeparatorPositions));
0276: }
0277: ASTPair pair = stack.peek();
0278: CtElement current = pair.element;
0279:
0280: if (current instanceof CtExpression) {
0281: while (!casts.isEmpty())
0282: ((CtExpression<?>) current).getTypeCasts().add(
0283: casts.remove(0));
0284: }
0285: if (current instanceof CtStatement
0286: && !context.label.isEmpty()) {
0287: ((CtStatement) current).setLabel(context.label.pop());
0288: }
0289:
0290: if (e instanceof CtTypedElement
0291: && node instanceof Expression) {
0292: if (((CtTypedElement<?>) e).getType() == null)
0293: ((CtTypedElement<?>) e)
0294: .setType(references
0295: .getTypeReference(((Expression) node).resolvedType));
0296: }
0297:
0298: }
0299:
0300: @SuppressWarnings("unchecked")
0301: void exit(ASTNode node) {
0302: ASTPair pair = stack.pop();
0303: if (pair.node != node)
0304: throw new RuntimeException("Unconsistant Stack " + node);
0305: CtElement current = pair.element;
0306: if (!stack.isEmpty()) {
0307: current.setParent(stack.peek().element);
0308: exiter.child = current;
0309: exiter.scan(stack.peek().element);
0310: }
0311: if (template && (current instanceof CtClass)) {
0312: factory.Template().add(
0313: (CtClass<? extends Template>) current);
0314: }
0315: }
0316:
0317: public List<CtSimpleType<?>> getCreatedTypes() {
0318: return createdTypes;
0319: }
0320:
0321: public boolean isArgument(CtElement e) {
0322: return arguments.size() > 0 && arguments.peek() == e;
0323: }
0324:
0325: private void popArgument(CtElement e) {
0326: if (arguments.pop() != e)
0327: throw new RuntimeException("Unconsistant stack");
0328: }
0329:
0330: private void pushArgument(CtElement e) {
0331: arguments.push(e);
0332: }
0333: }
0334:
0335: @SuppressWarnings("unchecked")
0336: public class ParentExiter extends CtInheritanceScanner {
0337: CtElement child;
0338:
0339: @Override
0340: public void scanCtElement(CtElement e) {
0341: if (child instanceof CtAnnotation
0342: && context.annotationValueName.isEmpty()) {
0343: e.getAnnotations().add((CtAnnotation) child);
0344: return;
0345: }
0346: }
0347:
0348: @Override
0349: public <R> void scanCtExecutable(CtExecutable<R> e) {
0350: if (child instanceof CtParameter) {
0351: e.getParameters().add((CtParameter<?>) child);
0352: return;
0353: } else if (child instanceof CtBlock) {
0354: e.setBody((CtBlock<R>) child);
0355: return;
0356: }
0357: super .scanCtExecutable(e);
0358: }
0359:
0360: @Override
0361: public void scanCtGenericElement(CtGenericElement e) {
0362: return;
0363: }
0364:
0365: @Override
0366: public void scanCtLoop(CtLoop loop) {
0367: if (loop.getBody() == null && child instanceof CtStatement) {
0368: loop.setBody((CtStatement) child);
0369: }
0370: super .scanCtLoop(loop);
0371: }
0372:
0373: @Override
0374: public <T> void scanCtSimpleType(CtSimpleType<T> t) {
0375: if (child instanceof CtSimpleType) {
0376: if (t.getNestedTypes().contains(child)) {
0377: t.getNestedTypes().remove(child);
0378: }
0379: t.getNestedTypes().add((CtSimpleType<?>) child);
0380: return;
0381: } else if (child instanceof CtField) {
0382: t.getFields().add((CtField<?>) child);
0383: return;
0384: } else if (child instanceof CtConstructor) {
0385: return;
0386: }
0387: super .scanCtSimpleType(t);
0388: }
0389:
0390: @Override
0391: public <T, E extends CtExpression<?>> void scanCtTargetedExpression(
0392: CtTargetedExpression<T, E> targetedExpression) {
0393: if (!context.target.isEmpty()
0394: && context.target.peek() == targetedExpression) {
0395: targetedExpression.setTarget((E) child);
0396: return;
0397: }
0398: super .scanCtTargetedExpression(targetedExpression);
0399: }
0400:
0401: @Override
0402: public <T> void scanCtType(CtType<T> type) {
0403: if (child instanceof CtMethod) {
0404: type.getMethods().add((CtMethod<?>) child);
0405: return;
0406: }
0407: super .scanCtType(type);
0408: }
0409:
0410: @Override
0411: public <T> void scanCtVariable(CtVariable<T> v) {
0412: if (child instanceof CtExpression
0413: && !context.arguments.isEmpty()
0414: && context.arguments.peek() == v) {
0415: v.setDefaultExpression((CtExpression<T>) child);
0416: return;
0417: }
0418: super .scanCtVariable(v);
0419: }
0420:
0421: @Override
0422: public <A extends java.lang.annotation.Annotation> void visitCtAnnotation(
0423: CtAnnotation<A> annotation) {
0424: String name = context.annotationValueName.peek();
0425: Object value = child;
0426:
0427: if (value instanceof CtVariableAccess)
0428: value = ((CtVariableAccess) value).getVariable();
0429: if (value instanceof CtFieldReference
0430: && ((CtFieldReference) value).getSimpleName()
0431: .equals("class")) {
0432: value = ((CtFieldReference) value).getType();
0433: }
0434:
0435: if (!context.arrayInitializer.isEmpty()
0436: && context.arrayInitializer.peek() == annotation) {
0437: // Array
0438: if (annotation.getElementValues().containsKey(name)) {
0439: ((ArrayList) annotation.getElementValues()
0440: .get(name)).add(value);
0441: } else {
0442: ArrayList lst = new ArrayList();
0443: lst.add(value);
0444: annotation.getElementValues().put(name, lst);
0445: }
0446: } else {
0447: annotation.getElementValues().put(name, value);
0448: }
0449: super .visitCtAnnotation(annotation);
0450: }
0451:
0452: @Override
0453: public void visitCtAnonymousExecutable(CtAnonymousExecutable e) {
0454: if (child instanceof CtBlock) {
0455: e.setBody((CtBlock) child);
0456: return;
0457: }
0458: super .visitCtAnonymousExecutable(e);
0459: }
0460:
0461: @Override
0462: public <T, E extends CtExpression<?>> void visitCtArrayAccess(
0463: CtArrayAccess<T, E> arrayAccess) {
0464: if (context.arguments.size() > 0
0465: && context.arguments.peek() == arrayAccess) {
0466: arrayAccess
0467: .setIndexExpression((CtExpression<Integer>) child);
0468: return;
0469: } else if (arrayAccess.getTarget() == null) {
0470: arrayAccess.setTarget((E) child);
0471: return;
0472: }
0473: super .visitCtArrayAccess(arrayAccess);
0474: }
0475:
0476: @Override
0477: public <T> void visitCtAssert(CtAssert<T> asserted) {
0478: if (child instanceof CtExpression)
0479: if (!context.arguments.isEmpty()
0480: && context.arguments.peek() == asserted) {
0481: asserted.setExpression((CtExpression<T>) child);
0482: return;
0483: } else {
0484: asserted
0485: .setAssertExpression((CtExpression<Boolean>) child);
0486: return;
0487: }
0488: super .visitCtAssert(asserted);
0489: }
0490:
0491: @Override
0492: public <T, A extends T> void visitCtAssignment(
0493: CtAssignment<T, A> assignement) {
0494: if (assignement.getAssigned() == null) {
0495: assignement.setAssigned((CtExpression<T>) child);
0496: return;
0497: } else if (assignement.getAssignment() == null) {
0498: assignement.setAssignment((CtExpression<A>) child);
0499: return;
0500: }
0501: super .visitCtAssignment(assignement);
0502: }
0503:
0504: @Override
0505: public <T> void visitCtBinaryOperator(
0506: CtBinaryOperator<T> operator) {
0507: if (child instanceof CtExpression)
0508: if (operator.getLeftHandOperand() == null) {
0509: operator
0510: .setLeftHandOperand((CtExpression<?>) child);
0511: return;
0512: } else if (operator.getRightHandOperand() == null) {
0513: operator
0514: .setRightHandOperand((CtExpression<?>) child);
0515: return;
0516: }
0517: super .visitCtBinaryOperator(operator);
0518: }
0519:
0520: @Override
0521: public <R> void visitCtBlock(CtBlock<R> block) {
0522: if (child instanceof CtStatement) {
0523: block.getStatements().add((CtStatement) child);
0524: return;
0525: }
0526: super .visitCtBlock(block);
0527: }
0528:
0529: @Override
0530: public <E> void visitCtCase(CtCase<E> caseStatement) {
0531: if (context.selector
0532: && caseStatement.getCaseExpression() == null
0533: && child instanceof CtExpression) {
0534: caseStatement
0535: .setCaseExpression((CtExpression<E>) child);
0536: return;
0537: } else if (child instanceof CtStatement) {
0538: caseStatement.getStatements().add((CtStatement) child);
0539: return;
0540: }
0541: super .visitCtCase(caseStatement);
0542: }
0543:
0544: @Override
0545: public void visitCtCatch(CtCatch catchBlock) {
0546: if (child instanceof CtBlock) {
0547: catchBlock.setBody((CtBlock) child);
0548: return;
0549: } else if (child instanceof CtLocalVariable) {
0550: catchBlock
0551: .setParameter((CtLocalVariable<? extends Throwable>) child);
0552: return;
0553: }
0554: super .visitCtCatch(catchBlock);
0555: }
0556:
0557: @Override
0558: public <T> void visitCtClass(CtClass<T> ctClass) {
0559: if (child instanceof CtConstructor) {
0560: CtConstructor c = (CtConstructor) child;
0561: ctClass.getConstructors().add(c);
0562: if (c.getPosition() != null
0563: && c.getPosition()
0564: .equals(ctClass.getPosition())) {
0565: c.setImplicit(true);
0566: }
0567: }
0568: if (child instanceof CtAnonymousExecutable) {
0569: ctClass.getAnonymousExecutables().add(
0570: (CtAnonymousExecutable) child);
0571: }
0572: super .visitCtClass(ctClass);
0573: }
0574:
0575: @Override
0576: public <T> void visitCtConditional(CtConditional<T> conditional) {
0577: if (child instanceof CtExpression) {
0578: if (conditional.getCondition() == null) {
0579: conditional
0580: .setCondition((CtExpression<Boolean>) child);
0581: } else if (conditional.getThenExpression() == null) {
0582: conditional
0583: .setThenExpression((CtExpression<T>) child);
0584: } else if (conditional.getElseExpression() == null) {
0585: conditional
0586: .setElseExpression((CtExpression<T>) child);
0587: }
0588: }
0589: super .visitCtConditional(conditional);
0590: }
0591:
0592: @Override
0593: public void visitCtDo(CtDo doLoop) {
0594: if (child instanceof CtExpression
0595: && doLoop.getLoopingExpression() == null) {
0596: doLoop
0597: .setLoopingExpression((CtExpression<Boolean>) child);
0598: return;
0599: }
0600: super .visitCtDo(doLoop);
0601: }
0602:
0603: @Override
0604: public <T> void visitCtField(CtField<T> f) {
0605: if (f.getDefaultExpression() == null
0606: && child instanceof CtExpression) {
0607: f.setDefaultExpression((CtExpression<T>) child);
0608: return;
0609: }
0610: super .visitCtField(f);
0611: }
0612:
0613: @Override
0614: public void visitCtFor(CtFor forLoop) {
0615: if (context.forinit && child instanceof CtStatement) {
0616: forLoop.getForInit().add((CtStatement) child);
0617: return;
0618: }
0619: if (!context.forupdate && forLoop.getExpression() == null
0620: && child instanceof CtExpression) {
0621: forLoop.setExpression((CtExpression<Boolean>) child);
0622: return;
0623: }
0624: if (context.forupdate && child instanceof CtStatement) {
0625: forLoop.getForUpdate().add((CtStatement) child);
0626: return;
0627: }
0628: super .visitCtFor(forLoop);
0629: }
0630:
0631: @Override
0632: public void visitCtForEach(CtForEach foreach) {
0633: if (foreach.getVariable() == null
0634: && child instanceof CtVariable) {
0635: foreach.setVariable((CtLocalVariable<?>) child);
0636: return;
0637: } else if (foreach.getExpression() == null
0638: && child instanceof CtExpression) {
0639: foreach.setExpression((CtExpression<?>) child);
0640: return;
0641: } else if (child instanceof CtStatement) {
0642: foreach.setBody((CtStatement) child);
0643: return;
0644: }
0645: super .visitCtForEach(foreach);
0646: }
0647:
0648: @Override
0649: public void visitCtIf(CtIf ifElement) {
0650: if (ifElement.getCondition() == null
0651: && child instanceof CtExpression) {
0652: ifElement.setCondition((CtExpression<Boolean>) child);
0653: return;
0654: } else if (child instanceof CtStatement) {
0655: if (ifElement.getThenStatement() == null) {
0656: ifElement.setThenStatement((CtStatement) child);
0657: return;
0658: } else if (ifElement.getElseStatement() == null) {
0659: ifElement.setElseStatement((CtStatement) child);
0660: return;
0661: }
0662: }
0663: super .visitCtIf(ifElement);
0664: }
0665:
0666: @Override
0667: public <T> void visitCtInvocation(CtInvocation<T> invocation) {
0668: if (context.isArgument(invocation)
0669: && child instanceof CtExpression) {
0670: invocation.getArguments().add((CtExpression<?>) child);
0671: return;
0672: } else if (child instanceof CtExpression) {
0673: invocation.setTarget((CtExpression<?>) child);
0674: return;
0675: }
0676: super .visitCtInvocation(invocation);
0677: }
0678:
0679: @Override
0680: public <T> void visitCtNewArray(CtNewArray<T> newArray) {
0681: if (context.isArgument(newArray)) {
0682: newArray.getDimensionExpressions().add(
0683: (CtExpression<Integer>) child);
0684: return;
0685: } else if (child instanceof CtExpression) {
0686: newArray.getElements().add((CtExpression<?>) child);
0687: return;
0688: }
0689: }
0690:
0691: @Override
0692: public <T> void visitCtNewClass(CtNewClass<T> newClass) {
0693: if (context.isArgument(newClass)
0694: && child instanceof CtExpression) {
0695: newClass.getArguments().add((CtExpression<?>) child);
0696: return;
0697: } else if (child instanceof CtClass) {
0698: newClass.setAnonymousClass((CtClass<?>) child);
0699: ((CtClass<?>) child).setSuperclass(newClass.getType());
0700: return;
0701: }
0702: super .visitCtNewClass(newClass);
0703: }
0704:
0705: @Override
0706: public void visitCtPackage(CtPackage ctPackage) {
0707: if (child instanceof CtSimpleType) {
0708: if (ctPackage.getTypes().contains(child)) {
0709: ctPackage.getTypes().remove(child);
0710: }
0711: ctPackage.getTypes().add((CtSimpleType<?>) child);
0712: context.addCreatedType((CtSimpleType<?>) child);
0713: if (child.getPosition() != null
0714: && child.getPosition().getCompilationUnit() != null) {
0715: child.getPosition().getCompilationUnit()
0716: .getDeclaredTypes().add(
0717: (CtSimpleType) child);
0718: }
0719: return;
0720: }
0721: super .visitCtPackage(ctPackage);
0722: }
0723:
0724: @Override
0725: public <R> void visitCtReturn(CtReturn<R> returnStatement) {
0726: if (child instanceof CtExpression) {
0727: returnStatement
0728: .setReturnedExpression((CtExpression<R>) child);
0729: return;
0730: }
0731: super .visitCtReturn(returnStatement);
0732: }
0733:
0734: @Override
0735: public <E> void visitCtSwitch(CtSwitch<E> switchStatement) {
0736: if (switchStatement.getSelector() == null
0737: && child instanceof CtExpression) {
0738: switchStatement.setSelector((CtExpression<E>) child);
0739: return;
0740: }
0741: if (child instanceof CtCase) {
0742: switchStatement.getCases().add((CtCase) child);
0743: return;
0744: }
0745: super .visitCtSwitch(switchStatement);
0746: }
0747:
0748: @Override
0749: public void visitCtSynchronized(CtSynchronized synchro) {
0750: if (synchro.getExpression() == null
0751: && child instanceof CtExpression) {
0752: synchro.setExpression((CtExpression<?>) child);
0753: return;
0754: }
0755: if (synchro.getBlock() == null && child instanceof CtBlock) {
0756: synchro.setBlock((CtBlock) child);
0757: return;
0758: }
0759: super .visitCtSynchronized(synchro);
0760: }
0761:
0762: @Override
0763: public void visitCtThrow(CtThrow throwStatement) {
0764: if (throwStatement.getThrownExpression() == null) {
0765: throwStatement
0766: .setThrownExpression((CtExpression<? extends Throwable>) child);
0767: return;
0768: }
0769: super .visitCtThrow(throwStatement);
0770: }
0771:
0772: @Override
0773: public void visitCtTry(CtTry tryBlock) {
0774: if (child instanceof CtBlock) {
0775: if (!context.finallyzer.isEmpty()
0776: && context.finallyzer.peek() == tryBlock)
0777: tryBlock.setFinalizer((CtBlock) child);
0778: else
0779: tryBlock.setBody((CtBlock) child);
0780: return;
0781: } else if (child instanceof CtCatch) {
0782: tryBlock.getCatchers().add((CtCatch) child);
0783: return;
0784: }
0785: super .visitCtTry(tryBlock);
0786: }
0787:
0788: @Override
0789: public <T> void visitCtUnaryOperator(CtUnaryOperator<T> operator) {
0790: if (operator.getOperand() == null
0791: && child instanceof CtExpression) {
0792: operator.setOperand((CtExpression<T>) child);
0793: return;
0794: }
0795: super .visitCtUnaryOperator(operator);
0796: }
0797:
0798: @Override
0799: public void visitCtWhile(CtWhile whileLoop) {
0800: if (whileLoop.getLoopingExpression() == null
0801: && child instanceof CtExpression) {
0802: whileLoop
0803: .setLoopingExpression((CtExpression<Boolean>) child);
0804: return;
0805: }
0806: super .visitCtWhile(whileLoop);
0807: }
0808: };
0809:
0810: public class ReferenceBuilder {
0811:
0812: Map<String, CtTypeReference<?>> basestypes = new TreeMap<String, CtTypeReference<?>>();
0813:
0814: Set<String> typevars = new TreeSet<String>();
0815:
0816: boolean bounds = false;
0817:
0818: public CtTypeReference<?> getBoundedTypeReference(
0819: TypeBinding binding) {
0820: bounds = true;
0821: CtTypeReference<?> ref = getTypeReference(binding);
0822: bounds = false;
0823: return ref;
0824: }
0825:
0826: @SuppressWarnings("unchecked")
0827: public CtExecutableReference getExecutableReference(
0828: MethodBinding exec) {
0829: CtExecutableReference ref = factory.Core()
0830: .createExecutableReference();
0831: ref.setDeclaringType(getTypeReference(exec.declaringClass));
0832: ref.setType(getTypeReference(exec.returnType));
0833: ref.setSimpleName(new String(exec.selector));
0834: ref.setStatic(exec.isStatic());
0835: if (exec.parameters != null) {
0836: for (TypeBinding b : exec.parameters)
0837: ref.getParameterTypes().add(getTypeReference(b));
0838: }
0839: if (exec.typeVariables != null) {
0840: for (TypeVariableBinding b : exec.typeVariables)
0841: ref.getActualTypeArguments().add(
0842: getTypeReference(b));
0843: }
0844:
0845: return ref;
0846: }
0847:
0848: public CtPackageReference getPackageReference(
0849: PackageBinding reference) {
0850: String name = new String(reference.shortReadableName());
0851: if (name.length() == 0)
0852: return null;
0853: CtPackageReference ref = factory.Core()
0854: .createPackageReference();
0855: ref.setSimpleName(name);
0856: return ref;
0857: }
0858:
0859: @SuppressWarnings("unchecked")
0860: public CtTypeReference getTypeReference(TypeBinding binding) {
0861: CtTypeReference ref = null;
0862: if (binding == null)
0863: return null;
0864: if (binding instanceof RawTypeBinding) {
0865: ref = getTypeReference(((ParameterizedTypeBinding) binding)
0866: .genericType());
0867: } else if (binding instanceof ParameterizedTypeBinding) {
0868: ref = getTypeReference(((ParameterizedTypeBinding) binding)
0869: .genericType());
0870: if (((ParameterizedTypeBinding) binding).arguments != null)
0871: for (TypeBinding b : ((ParameterizedTypeBinding) binding).arguments) {
0872: ref.getActualTypeArguments().add(
0873: getTypeReference(b));
0874: }
0875: } else if (binding instanceof BinaryTypeBinding) {
0876: ref = factory.Core().createTypeReference();
0877: if (binding.enclosingType() != null) {
0878: ref.setDeclaringType(getTypeReference(binding
0879: .enclosingType()));
0880: } else {
0881: ref.setPackage(getPackageReference(binding
0882: .getPackage()));
0883: }
0884: ref.setSimpleName(new String(binding.sourceName()));
0885:
0886: } else if (binding instanceof TypeVariableBinding) {
0887: ref = factory.Core().createTypeParameterReference();
0888: if (binding instanceof CaptureBinding) {
0889: ref.setSimpleName("?");
0890: bounds = true;
0891: } else {
0892: ref.setSimpleName(new String(binding.sourceName()));
0893: }
0894: TypeVariableBinding b = (TypeVariableBinding) binding;
0895: if (bounds && b.super class != null
0896: && b.firstBound == b.super class) {
0897: boolean oldBounds = bounds;
0898: bounds = false;
0899: ((CtTypeParameterReference) ref).getBounds().add(
0900: getTypeReference(b.super class));
0901: bounds = oldBounds;
0902: }
0903: if (bounds
0904: && b.super Interfaces != null
0905: && b.super Interfaces != Binding.NO_SUPERINTERFACES) {
0906: bounds = false;
0907: for (int i = 0, length = b.super Interfaces.length; i < length; i++) {
0908: TypeBinding tb = b.super Interfaces[i];
0909: ((CtTypeParameterReference) ref).getBounds()
0910: .add(getTypeReference(tb));
0911: }
0912: }
0913: if (binding instanceof CaptureBinding)
0914: bounds = false;
0915:
0916: } else if (binding instanceof BaseTypeBinding) {
0917: String name = new String(binding.sourceName());
0918: ref = basestypes.get(name);
0919: if (ref == null) {
0920: ref = factory.Core().createTypeReference();
0921: ref.setSimpleName(name);
0922: basestypes.put(name, ref);
0923: }
0924: } else if (binding instanceof WildcardBinding) {
0925: CtTypeParameterReference reference = factory.Core()
0926: .createTypeParameterReference();
0927: reference.setSimpleName("?");
0928: if (((WildcardBinding) binding).boundKind == Wildcard.SUPER)
0929: reference.setUpper(false);
0930:
0931: if (((WildcardBinding) binding).bound != null)
0932: reference
0933: .getBounds()
0934: .add(
0935: getTypeReference(((WildcardBinding) binding).bound));
0936: ref = reference;
0937: } else if (binding instanceof SourceTypeBinding) {
0938: ref = factory.Core().createTypeReference();
0939: if (binding.isAnonymousType()) {
0940: ref.setSimpleName("");
0941: } else {
0942: ref.setSimpleName(new String(binding.sourceName()));
0943: if (binding.enclosingType() != null)
0944: ref.setDeclaringType(getTypeReference(binding
0945: .enclosingType()));
0946: else
0947: ref.setPackage(getPackageReference(binding
0948: .getPackage()));
0949: }
0950: } else if (binding instanceof ArrayBinding) {
0951: CtArrayTypeReference arrayref = factory.Core()
0952: .createArrayTypeReference();
0953: ref = arrayref;
0954: for (int i = 1; i < binding.dimensions(); i++) {
0955: CtArrayTypeReference tmp = factory.Core()
0956: .createArrayTypeReference();
0957: arrayref.setComponentType(tmp);
0958: arrayref = tmp;
0959: }
0960: arrayref.setComponentType(getTypeReference(binding
0961: .leafComponentType()));
0962: } else {
0963: throw new RuntimeException("Unkown TypeBinding");
0964: }
0965: return ref;
0966: }
0967:
0968: @SuppressWarnings("unchecked")
0969: public CtVariableReference getVariableReference(
0970: VariableBinding varbin) {
0971:
0972: if (varbin instanceof FieldBinding) {
0973: CtFieldReference ref = factory.Core()
0974: .createFieldReference();
0975: ref.setSimpleName(new String(varbin.name));
0976: ref.setType(getTypeReference(varbin.type));
0977:
0978: if (((FieldBinding) varbin).declaringClass != null)
0979: ref
0980: .setDeclaringType(getTypeReference(((FieldBinding) varbin).declaringClass));
0981: else {
0982: ref.setDeclaringType(ref.getType());
0983: }
0984: ref.setFinal(varbin.isFinal());
0985: ref
0986: .setStatic((varbin.modifiers & ClassFileConstants.AccStatic) != 0);
0987: return ref;
0988: } else if (varbin instanceof LocalVariableBinding) {
0989: if (((LocalVariableBinding) varbin).declaration instanceof Argument
0990: && ((LocalVariableBinding) varbin).declaringScope instanceof MethodScope) {
0991: CtParameterReference ref = factory.Core()
0992: .createParameterReference();
0993: ref.setSimpleName(new String(varbin.name));
0994: ref.setType(getTypeReference(varbin.type));
0995: ref
0996: .setDeclaringExecutable(getExecutableReference(((AbstractMethodDeclaration) ((MethodScope) ((LocalVariableBinding) varbin).declaringScope)
0997: .referenceContext()).binding));
0998: return ref;
0999: } else {
1000: CtLocalVariableReference ref = factory.Core()
1001: .createLocalVariableReference();
1002: ref.setSimpleName(new String(varbin.name));
1003: CtTypeReference ref2 = getTypeReference(varbin.type);
1004: ref.setType(ref2);
1005: ref.setDeclaration(getLocalVariableDeclaration(ref
1006: .getSimpleName()));
1007: return ref;
1008: }
1009: } else {
1010: throw new RuntimeException("Unknow VariableBinding");
1011: }
1012: }
1013: }
1014:
1015: public static String cleanJavadoc(String doc) {
1016: StringBuffer ret = new StringBuffer();
1017: String[] docs = doc.split("\n");
1018: for (int i = 1; i < docs.length - 1; i++) {
1019: ret.append(docs[i].substring(docs[i].indexOf('*') + 1));
1020: ret.append("\n");
1021: }
1022: // clean '\r'
1023: StringBuffer ret2 = new StringBuffer();
1024: for (int i = 0; i < ret.length(); i++) {
1025: if (ret.charAt(i) != '\r')
1026: ret2.append(ret.charAt(i));
1027: }
1028: return ret2.toString();
1029: }
1030:
1031: public static Set<ModifierKind> getModifier(int mod) {
1032: Set<ModifierKind> ret = new TreeSet<ModifierKind>();
1033: if ((mod & ClassFileConstants.AccPublic) != 0)
1034: ret.add(ModifierKind.PUBLIC);
1035: if ((mod & ClassFileConstants.AccPrivate) != 0)
1036: ret.add(ModifierKind.PRIVATE);
1037: if ((mod & ClassFileConstants.AccProtected) != 0)
1038: ret.add(ModifierKind.PROTECTED);
1039: if ((mod & ClassFileConstants.AccStatic) != 0)
1040: ret.add(ModifierKind.STATIC);
1041: if ((mod & ClassFileConstants.AccFinal) != 0)
1042: ret.add(ModifierKind.FINAL);
1043: if ((mod & ClassFileConstants.AccSynchronized) != 0)
1044: ret.add(ModifierKind.SYNCHRONIZED);
1045: if ((mod & ClassFileConstants.AccVolatile) != 0)
1046: ret.add(ModifierKind.VOLATILE);
1047: if ((mod & ClassFileConstants.AccTransient) != 0)
1048: ret.add(ModifierKind.TRANSIENT);
1049: if ((mod & ClassFileConstants.AccAbstract) != 0)
1050: ret.add(ModifierKind.ABSTRACT);
1051: if ((mod & ClassFileConstants.AccStrictfp) != 0)
1052: ret.add(ModifierKind.STRICTFP);
1053: if ((mod & ClassFileConstants.AccNative) != 0)
1054: ret.add(ModifierKind.NATIVE);
1055: return ret;
1056: }
1057:
1058: /**
1059: * Search the line number corresponding to a specific position
1060: */
1061: public static final int searchLineNumber(int[] startLineIndexes,
1062: int position) {
1063: if (startLineIndexes == null)
1064: return 1;
1065: int length = startLineIndexes.length;
1066: if (length == 0)
1067: return 1;
1068: int g = 0, d = length - 1;
1069: int m = 0, start;
1070: while (g <= d) {
1071: m = (g + d) / 2;
1072: if (position < (start = startLineIndexes[m])) {
1073: d = m - 1;
1074: } else if (position > start) {
1075: g = m + 1;
1076: } else {
1077: return m + 1;
1078: }
1079: }
1080: if (position < startLineIndexes[m]) {
1081: return m + 1;
1082: }
1083: return m + 2;
1084: }
1085:
1086: BuilderContext context = new BuilderContext();
1087:
1088: ParentExiter exiter = new ParentExiter();
1089:
1090: Factory factory;
1091:
1092: ReferenceBuilder references = new ReferenceBuilder();
1093:
1094: public boolean template = false;
1095:
1096: public JDTTreeBuilder(Factory factory) {
1097: super ();
1098: this .factory = factory;
1099: }
1100:
1101: private void createExpression(StringLiteralConcatenation literal,
1102: BlockScope scope, List<Expression> rst) {
1103: if (rst.isEmpty())
1104: return;
1105:
1106: rst.get(0).traverse(this , scope);
1107: rst.remove(0);
1108:
1109: if (rst.size() > 1) {
1110: CtBinaryOperator<?> op = factory.Core()
1111: .createBinaryOperator();
1112: op.setKind(BinaryOperatorKind.PLUS);
1113: context.enter(op, literal);
1114: createExpression(literal, scope, rst);
1115: context.exit(literal);
1116: } else {
1117: createExpression(literal, scope, rst);
1118: }
1119:
1120: }
1121:
1122: CtSimpleType<?> createType(TypeDeclaration typeDeclaration) {
1123: CtSimpleType<?> type = null;
1124: if ((typeDeclaration.modifiers & ClassFileConstants.AccAnnotation) != 0) {
1125: type = (CtSimpleType<?>) factory.Core()
1126: .createAnnotationType();
1127: } else if ((typeDeclaration.modifiers & ClassFileConstants.AccEnum) != 0) {
1128: CtEnum<?> e = factory.Core().createEnum();
1129: if (typeDeclaration.super Interfaces != null) {
1130: for (TypeReference ref : typeDeclaration.super Interfaces) {
1131: e
1132: .getSuperInterfaces()
1133: .add(
1134: references
1135: .getTypeReference(ref.resolvedType));
1136: }
1137: }
1138: type = e;
1139: } else if ((typeDeclaration.modifiers & ClassFileConstants.AccInterface) != 0) {
1140: CtInterface<?> interf = factory.Core().createInterface();
1141: if (typeDeclaration.super Interfaces != null) {
1142: for (TypeReference ref : typeDeclaration.super Interfaces) {
1143: interf
1144: .getSuperInterfaces()
1145: .add(
1146: references
1147: .getTypeReference(ref.resolvedType));
1148: }
1149: }
1150: if (typeDeclaration.typeParameters != null)
1151: for (TypeParameter p : typeDeclaration.typeParameters) {
1152: interf
1153: .getFormalTypeParameters()
1154: .add(
1155: references
1156: .getBoundedTypeReference(p.binding));
1157: }
1158: type = interf;
1159: } else {
1160: CtClass<?> cl = factory.Core().createClass();
1161: if (typeDeclaration.super class != null) {
1162: cl
1163: .setSuperclass(references
1164: .getTypeReference(typeDeclaration.super class.resolvedType));
1165: }
1166: if (typeDeclaration.super Interfaces != null) {
1167: for (TypeReference ref : typeDeclaration.super Interfaces) {
1168: cl
1169: .getSuperInterfaces()
1170: .add(
1171: references
1172: .getTypeReference(ref.resolvedType));
1173: }
1174: }
1175: if (typeDeclaration.typeParameters != null)
1176: for (TypeParameter p : typeDeclaration.typeParameters) {
1177: cl
1178: .getFormalTypeParameters()
1179: .add(
1180: references
1181: .getBoundedTypeReference(p.binding));
1182: }
1183: type = cl;
1184: }
1185: type.setSimpleName(new String(typeDeclaration.name));
1186: // Setting modifiers
1187: type.setModifiers(getModifier(typeDeclaration.modifiers));
1188: // type.setDocComment(getJavaDoc(typeDeclaration.javadoc));
1189:
1190: return type;
1191: }
1192:
1193: @Override
1194: public void endVisit(AllocationExpression allocationExpression,
1195: BlockScope scope) {
1196: context.exit(allocationExpression);
1197: }
1198:
1199: @Override
1200: public void endVisit(AND_AND_Expression and_and_Expression,
1201: BlockScope scope) {
1202: context.exit(and_and_Expression);
1203: }
1204:
1205: @Override
1206: public void endVisit(
1207: AnnotationMethodDeclaration annotationTypeDeclaration,
1208: ClassScope classScope) {
1209: context.exit(annotationTypeDeclaration);
1210: }
1211:
1212: public void endVisit(Argument argument, BlockScope scope) {
1213: context.exit(argument);
1214: }
1215:
1216: @Override
1217: public void endVisit(
1218: ArrayAllocationExpression arrayAllocationExpression,
1219: BlockScope scope) {
1220: context.exit(arrayAllocationExpression);
1221: }
1222:
1223: @Override
1224: public void endVisit(ArrayInitializer arrayInitializer,
1225: BlockScope scope) {
1226: if (!context.arrayInitializer.isEmpty()) {
1227: context.arrayInitializer.pop();
1228: } else {
1229: context.exit(arrayInitializer);
1230: }
1231: }
1232:
1233: @Override
1234: public void endVisit(ArrayReference arrayReference, BlockScope scope) {
1235: context.exit(arrayReference);
1236: }
1237:
1238: @Override
1239: public void endVisit(ArrayTypeReference arrayTypeReference,
1240: BlockScope scope) {
1241: context.exit(arrayTypeReference);
1242: }
1243:
1244: @Override
1245: public void endVisit(AssertStatement assertStatement,
1246: BlockScope scope) {
1247: context.exit(assertStatement);
1248: }
1249:
1250: @Override
1251: public void endVisit(Assignment assignment, BlockScope scope) {
1252: context.exit(assignment);
1253: }
1254:
1255: @Override
1256: public void endVisit(BinaryExpression binaryExpression,
1257: BlockScope scope) {
1258: context.exit(binaryExpression);
1259: }
1260:
1261: @Override
1262: public void endVisit(Block block, BlockScope scope) {
1263: context.exit(block);
1264: }
1265:
1266: @Override
1267: public void endVisit(BreakStatement breakStatement, BlockScope scope) {
1268: context.exit(breakStatement);
1269: }
1270:
1271: @Override
1272: public void endVisit(CaseStatement caseStatement, BlockScope scope) {
1273: context.exit(caseStatement);
1274: }
1275:
1276: @Override
1277: public void endVisit(CharLiteral charLiteral, BlockScope scope) {
1278: context.exit(charLiteral);
1279: }
1280:
1281: @Override
1282: public void endVisit(ClassLiteralAccess classLiteral,
1283: BlockScope scope) {
1284: context.exit(classLiteral);
1285: }
1286:
1287: @Override
1288: public void endVisit(CompoundAssignment compoundAssignment,
1289: BlockScope scope) {
1290: context.exit(compoundAssignment);
1291: }
1292:
1293: @Override
1294: public void endVisit(ConditionalExpression conditionalExpression,
1295: BlockScope scope) {
1296: context.exit(conditionalExpression);
1297: }
1298:
1299: public void endVisit(ConstructorDeclaration constructorDeclaration,
1300: ClassScope scope) {
1301: context.exit(constructorDeclaration);
1302: if (context.stack.peek().node == constructorDeclaration)
1303: context.exit(constructorDeclaration);
1304: }
1305:
1306: @Override
1307: public void endVisit(ContinueStatement continueStatement,
1308: BlockScope scope) {
1309: context.exit(continueStatement);
1310: }
1311:
1312: @Override
1313: public void endVisit(DoStatement doStatement, BlockScope scope) {
1314: context.exit(doStatement);
1315: }
1316:
1317: @Override
1318: public void endVisit(DoubleLiteral doubleLiteral, BlockScope scope) {
1319: context.exit(doubleLiteral);
1320: }
1321:
1322: @Override
1323: public void endVisit(EqualExpression equalExpression,
1324: BlockScope scope) {
1325: context.exit(equalExpression);
1326: }
1327:
1328: @Override
1329: public void endVisit(ExplicitConstructorCall explicitConstructor,
1330: BlockScope scope) {
1331: context.exit(explicitConstructor);
1332: }
1333:
1334: public void endVisit(ExtendedStringLiteral extendedStringLiteral,
1335: BlockScope scope) {
1336: context.exit(extendedStringLiteral);
1337: }
1338:
1339: @Override
1340: public void endVisit(FalseLiteral falseLiteral, BlockScope scope) {
1341: context.exit(falseLiteral);
1342: }
1343:
1344: public void endVisit(FieldDeclaration fieldDeclaration,
1345: MethodScope scope) {
1346: context.exit(fieldDeclaration);
1347: }
1348:
1349: @SuppressWarnings("unchecked")
1350: public void endVisit(FieldReference fieldReference, BlockScope scope) {
1351: context.exit(fieldReference);
1352: }
1353:
1354: @Override
1355: public void endVisit(FloatLiteral floatLiteral, BlockScope scope) {
1356: context.exit(floatLiteral);
1357: }
1358:
1359: public void endVisit(ForeachStatement forStatement, BlockScope scope) {
1360: context.exit(forStatement);
1361: }
1362:
1363: @Override
1364: public void endVisit(ForStatement forStatement, BlockScope scope) {
1365: context.exit(forStatement);
1366: }
1367:
1368: @Override
1369: public void endVisit(IfStatement ifStatement, BlockScope scope) {
1370: context.exit(ifStatement);
1371: }
1372:
1373: @Override
1374: public void endVisit(Initializer initializer, MethodScope scope) {
1375: context.exit(initializer);
1376: }
1377:
1378: @Override
1379: public void endVisit(InstanceOfExpression instanceOfExpression,
1380: BlockScope scope) {
1381: context.exit(instanceOfExpression);
1382: }
1383:
1384: @Override
1385: public void endVisit(IntLiteral intLiteral, BlockScope scope) {
1386: context.exit(intLiteral);
1387: }
1388:
1389: @Override
1390: public void endVisit(LocalDeclaration localDeclaration,
1391: BlockScope scope) {
1392: context.exit(localDeclaration);
1393: }
1394:
1395: public void endVisit(LongLiteral longLiteral, BlockScope scope) {
1396: context.exit(longLiteral);
1397: }
1398:
1399: @Override
1400: public void endVisit(MarkerAnnotation annotation, BlockScope scope) {
1401: context.exit(annotation);
1402: }
1403:
1404: @Override
1405: public void endVisit(MemberValuePair pair, BlockScope scope) {
1406: if (!context.annotationValueName.pop().equals(
1407: new String(pair.name))) {
1408: throw new RuntimeException("Unconsistant Stack");
1409: }
1410: }
1411:
1412: @Override
1413: public void endVisit(MessageSend messageSend, BlockScope scope) {
1414: context.exit(messageSend);
1415: }
1416:
1417: public void endVisit(MethodDeclaration methodDeclaration,
1418: ClassScope scope) {
1419: // Exit from method and Block
1420: context.exit(methodDeclaration);
1421: if (context.stack.peek().node == methodDeclaration)
1422: context.exit(methodDeclaration);
1423: }
1424:
1425: public void endVisit(NormalAnnotation annotation, BlockScope scope) {
1426: context.exit(annotation);
1427: }
1428:
1429: @Override
1430: public void endVisit(NullLiteral nullLiteral, BlockScope scope) {
1431: context.exit(nullLiteral);
1432: }
1433:
1434: @Override
1435: public void endVisit(OR_OR_Expression or_or_Expression,
1436: BlockScope scope) {
1437: context.exit(or_or_Expression);
1438: }
1439:
1440: @Override
1441: public void endVisit(
1442: ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference,
1443: ClassScope scope) {
1444: context.exit(parameterizedQualifiedTypeReference);
1445: }
1446:
1447: @Override
1448: public void endVisit(
1449: ParameterizedSingleTypeReference parameterizedSingleTypeReference,
1450: BlockScope scope) {
1451: context.exit(parameterizedSingleTypeReference);
1452: }
1453:
1454: @Override
1455: public void endVisit(
1456: ParameterizedSingleTypeReference parameterizedSingleTypeReference,
1457: ClassScope scope) {
1458: context.exit(parameterizedSingleTypeReference);
1459: }
1460:
1461: @Override
1462: public void endVisit(PostfixExpression postfixExpression,
1463: BlockScope scope) {
1464: context.exit(postfixExpression);
1465: }
1466:
1467: @Override
1468: public void endVisit(PrefixExpression prefixExpression,
1469: BlockScope scope) {
1470: context.exit(prefixExpression);
1471: }
1472:
1473: @Override
1474: public void endVisit(
1475: QualifiedAllocationExpression qualifiedAllocationExpression,
1476: BlockScope scope) {
1477: endVisit((AllocationExpression) qualifiedAllocationExpression,
1478: scope);
1479: }
1480:
1481: public void endVisit(QualifiedNameReference qualifiedNameReference,
1482: BlockScope scope) {
1483: if (context.stack.peek().node == qualifiedNameReference)
1484: context.exit(qualifiedNameReference);
1485: }
1486:
1487: @Override
1488: public void endVisit(QualifiedThisReference qualifiedThisReference,
1489: BlockScope scope) {
1490: endVisit((ThisReference) qualifiedThisReference, scope);
1491: }
1492:
1493: @Override
1494: public void endVisit(QualifiedTypeReference arg0, BlockScope arg1) {
1495: context.exit(arg0);
1496: }
1497:
1498: @Override
1499: public void endVisit(ReturnStatement returnStatement,
1500: BlockScope scope) {
1501: context.exit(returnStatement);
1502: }
1503:
1504: @Override
1505: public void endVisit(SingleMemberAnnotation annotation,
1506: BlockScope scope) {
1507: if (!context.annotationValueName.pop().equals("value")) {
1508: throw new RuntimeException("unconsistant Stack");
1509: }
1510: context.exit(annotation);
1511: }
1512:
1513: @Override
1514: public void endVisit(SingleNameReference singleNameReference,
1515: BlockScope scope) {
1516: if (context.stack.peek().node == singleNameReference)
1517: context.exit(singleNameReference);
1518: }
1519:
1520: @Override
1521: public void endVisit(SingleTypeReference singleTypeReference,
1522: BlockScope scope) {
1523: context.exit(singleTypeReference);
1524: }
1525:
1526: @Override
1527: public void endVisit(SingleTypeReference singleTypeReference,
1528: ClassScope scope) {
1529: context.exit(singleTypeReference);
1530: }
1531:
1532: @Override
1533: public void endVisit(StringLiteral stringLiteral, BlockScope scope) {
1534: context.exit(stringLiteral);
1535: }
1536:
1537: @Override
1538: public void endVisit(StringLiteralConcatenation literal,
1539: BlockScope scope) {
1540: context.exit(literal);
1541: }
1542:
1543: @Override
1544: public void endVisit(SuperReference super Reference, BlockScope scope) {
1545: context.exit(super Reference);
1546: }
1547:
1548: @Override
1549: public void endVisit(SwitchStatement switchStatement,
1550: BlockScope scope) {
1551: context.exit(switchStatement);
1552: }
1553:
1554: @Override
1555: public void endVisit(SynchronizedStatement synchronizedStatement,
1556: BlockScope scope) {
1557: context.exit(synchronizedStatement);
1558: }
1559:
1560: @Override
1561: public void endVisit(ThisReference this Reference, BlockScope scope) {
1562: context.exit(this Reference);
1563: }
1564:
1565: public void endVisit(ThrowStatement throwStatement, BlockScope scope) {
1566: context.exit(throwStatement);
1567: }
1568:
1569: @Override
1570: public void endVisit(TrueLiteral trueLiteral, BlockScope scope) {
1571: context.exit(trueLiteral);
1572: }
1573:
1574: @Override
1575: public void endVisit(TryStatement tryStatement, BlockScope scope) {
1576: context.exit(tryStatement);
1577: }
1578:
1579: @Override
1580: public void endVisit(TypeDeclaration localTypeDeclaration,
1581: BlockScope scope) {
1582: context.exit(localTypeDeclaration);
1583: }
1584:
1585: @Override
1586: public void endVisit(TypeDeclaration memberTypeDeclaration,
1587: ClassScope scope) {
1588: while (!context.stack.isEmpty()
1589: && context.stack.peek().node == memberTypeDeclaration) {
1590: context.exit(memberTypeDeclaration);
1591: }
1592: }
1593:
1594: @Override
1595: public void endVisit(TypeDeclaration typeDeclaration,
1596: CompilationUnitScope scope) {
1597: while (!context.stack.isEmpty()
1598: && context.stack.peek().node == typeDeclaration) {
1599: context.exit(typeDeclaration);
1600: }
1601: context.compilationunitdeclaration = null;
1602: }
1603:
1604: @Override
1605: public void endVisit(UnaryExpression unaryExpression,
1606: BlockScope scope) {
1607: context.exit(unaryExpression);
1608: }
1609:
1610: @Override
1611: public void endVisit(WhileStatement whileStatement, BlockScope scope) {
1612: context.exit(whileStatement);
1613: }
1614:
1615: BinaryOperatorKind getBinaryOperatorKind(int bits) {
1616: // switch ((bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) {
1617: switch (bits) {
1618: case OperatorIds.EQUAL_EQUAL:
1619: return BinaryOperatorKind.EQ;
1620: case OperatorIds.LESS_EQUAL:
1621: return BinaryOperatorKind.LE;
1622: case OperatorIds.GREATER_EQUAL:
1623: return BinaryOperatorKind.GE;
1624: case OperatorIds.NOT_EQUAL:
1625: return BinaryOperatorKind.NE;
1626: case OperatorIds.LEFT_SHIFT:
1627: return BinaryOperatorKind.SL;
1628: case OperatorIds.RIGHT_SHIFT:
1629: return BinaryOperatorKind.SR;
1630: case OperatorIds.UNSIGNED_RIGHT_SHIFT:
1631: return BinaryOperatorKind.USR;
1632: case OperatorIds.OR_OR:
1633: return BinaryOperatorKind.OR;
1634: case OperatorIds.AND_AND:
1635: return BinaryOperatorKind.AND;
1636: case OperatorIds.PLUS:
1637: return BinaryOperatorKind.PLUS;
1638: case OperatorIds.MINUS:
1639: return BinaryOperatorKind.MINUS;
1640: case OperatorIds.NOT:
1641: return BinaryOperatorKind.NE;
1642: case OperatorIds.REMAINDER:
1643: return BinaryOperatorKind.MOD;
1644: case OperatorIds.XOR:
1645: return BinaryOperatorKind.BITXOR;
1646: case OperatorIds.AND:
1647: return BinaryOperatorKind.BITAND;
1648: case OperatorIds.MULTIPLY:
1649: return BinaryOperatorKind.MUL;
1650: case OperatorIds.OR:
1651: return BinaryOperatorKind.BITOR;
1652: case OperatorIds.DIVIDE:
1653: return BinaryOperatorKind.DIV;
1654: case OperatorIds.GREATER:
1655: return BinaryOperatorKind.GT;
1656: case OperatorIds.LESS:
1657: return BinaryOperatorKind.LT;
1658: case OperatorIds.QUESTIONCOLON:
1659: throw new RuntimeException("Unknow operator");
1660: case OperatorIds.EQUAL:
1661: return BinaryOperatorKind.EQ;
1662: }
1663: return null;
1664: }
1665:
1666: public List<CtSimpleType<?>> getCreatedTypes() {
1667: return context.getCreatedTypes();
1668: }
1669:
1670: public String getJavaDoc(Javadoc javadoc,
1671: CompilationUnitDeclaration declaration) {
1672: if (javadoc != null) {
1673: String s = new String(
1674: declaration.compilationResult.compilationUnit
1675: .getContents(), javadoc.sourceStart,
1676: javadoc.sourceEnd - javadoc.sourceStart + 1);
1677: return cleanJavadoc(s);
1678: }
1679: return null;
1680: }
1681:
1682: @SuppressWarnings("unchecked")
1683: protected <T> CtLocalVariable<T> getLocalVariableDeclaration(
1684: final String name) {
1685: List<CtElement> reversedElements = new ArrayList<CtElement>();
1686: for (ASTPair element : context.stack) {
1687: reversedElements.add(0, element.element);
1688: }
1689:
1690: for (CtElement element : reversedElements) {
1691: // TODO check if the variable is visible from here
1692:
1693: List<CtLocalVariable> var = Query.getElements(element,
1694: new TypeFilter<CtLocalVariable>(
1695: CtLocalVariable.class) {
1696: @Override
1697: public boolean matches(CtLocalVariable element) {
1698: return name.equals(element.getSimpleName())
1699: && super .matches(element);
1700: }
1701: });
1702:
1703: if (var.size() > 0) {
1704: return var.get(0);
1705: }
1706: }
1707: throw new IllegalStateException(
1708: "Could not find declaration for local variable " + name);
1709: }
1710:
1711: UnaryOperatorKind getUnaryOperator(int op) {
1712: switch (op) {
1713: case OperatorIds.PLUS:
1714: return UnaryOperatorKind.POS;
1715: case OperatorIds.MINUS:
1716: return UnaryOperatorKind.NEG;
1717: case OperatorIds.NOT:
1718: return UnaryOperatorKind.NOT;
1719: case OperatorIds.TWIDDLE:
1720: return UnaryOperatorKind.COMPL;
1721: }
1722: return null;
1723: }
1724:
1725: @SuppressWarnings("unchecked")
1726: @Override
1727: public boolean visit(AllocationExpression allocationExpression,
1728: BlockScope scope) {
1729: CtNewClass<?> c = factory.Core().createNewClass();
1730: if (allocationExpression.type != null)
1731: c
1732: .setType(references
1733: .getTypeReference(allocationExpression.type.resolvedType));
1734: c.setExecutable(references
1735: .getExecutableReference(allocationExpression.binding));
1736: c.getExecutable().setType(
1737: (CtTypeReference) c.getExecutable().getDeclaringType());
1738: context.enter(c, allocationExpression);
1739:
1740: if (allocationExpression.enclosingInstance() != null) {
1741: context.target.push(c);
1742: allocationExpression.enclosingInstance().traverse(this ,
1743: scope);
1744: context.target.pop();
1745: }
1746:
1747: context.pushArgument(c);
1748: if (allocationExpression.arguments != null) {
1749: for (Expression e : allocationExpression.arguments)
1750: e.traverse(this , scope);
1751: }
1752: context.popArgument(c);
1753: return false;
1754: }
1755:
1756: @Override
1757: public boolean visit(AND_AND_Expression and_and_Expression,
1758: BlockScope scope) {
1759: CtBinaryOperator<?> op = factory.Core().createBinaryOperator();
1760: op
1761: .setKind(getBinaryOperatorKind((and_and_Expression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT));
1762: context.enter(op, and_and_Expression);
1763: return true; // do nothing by default, keep traversing
1764: }
1765:
1766: @SuppressWarnings("unchecked")
1767: @Override
1768: public boolean visit(
1769: AnnotationMethodDeclaration annotationTypeDeclaration,
1770: ClassScope classScope) {
1771: CtField<?> f = factory.Core().createField();
1772: f.setSimpleName(new String(annotationTypeDeclaration.selector));
1773: f
1774: .setType(references
1775: .getTypeReference(annotationTypeDeclaration.binding.returnType));
1776: context.enter(f, annotationTypeDeclaration);
1777:
1778: if (annotationTypeDeclaration.annotations != null) {
1779: int annotationsLength = annotationTypeDeclaration.annotations.length;
1780: for (int i = 0; i < annotationsLength; i++)
1781: annotationTypeDeclaration.annotations[i].traverse(this ,
1782: annotationTypeDeclaration.scope);
1783: }
1784:
1785: if (annotationTypeDeclaration.defaultValue != null) {
1786: annotationTypeDeclaration.defaultValue.traverse(this ,
1787: annotationTypeDeclaration.scope);
1788: }
1789: return false;
1790: }
1791:
1792: @SuppressWarnings("unchecked")
1793: public boolean visit(Argument argument, BlockScope scope) {
1794: CtParameter<?> p = factory.Core().createParameter();
1795: p.setSimpleName(new String(argument.name));
1796: p.setVarArgs(argument.isVarArgs());
1797: p.setModifiers(getModifier(argument.modifiers));
1798: if (argument.type != null)
1799: p.setType(references
1800: .getTypeReference(argument.type.resolvedType));
1801: context.enter(p, argument);
1802: if (argument.initialization != null)
1803: argument.initialization.traverse(this , scope);
1804:
1805: if (argument.annotations != null)
1806: for (Annotation a : argument.annotations)
1807: a.traverse(this , scope);
1808: return false;
1809: }
1810:
1811: @SuppressWarnings("unchecked")
1812: @Override
1813: public boolean visit(
1814: ArrayAllocationExpression arrayAllocationExpression,
1815: BlockScope scope) {
1816: CtNewArray<?> array = factory.Core().createNewArray();
1817: array
1818: .setType(references
1819: .getTypeReference(arrayAllocationExpression.resolvedType));
1820:
1821: context.enter(array, arrayAllocationExpression);
1822:
1823: context.pushArgument(array);
1824: if (arrayAllocationExpression.dimensions != null)
1825: for (Expression e : arrayAllocationExpression.dimensions)
1826: if (e != null)
1827: e.traverse(this , scope);
1828: context.popArgument(array);
1829:
1830: if (arrayAllocationExpression.initializer != null
1831: && arrayAllocationExpression.initializer.expressions != null) {
1832: for (Expression e : arrayAllocationExpression.initializer.expressions)
1833: e.traverse(this , scope);
1834: }
1835: return false;
1836: }
1837:
1838: @Override
1839: public boolean visit(ArrayInitializer arrayInitializer,
1840: BlockScope scope) {
1841:
1842: if (context.annotationValueName.isEmpty()) {
1843: CtNewArray<?> array = factory.Core().createNewArray();
1844: context.enter(array, arrayInitializer);
1845: } else {
1846: context.arrayInitializer.push(context.stack.peek().element);
1847: }
1848: return super .visit(arrayInitializer, scope);
1849: }
1850:
1851: @Override
1852: public boolean visit(ArrayReference arrayReference, BlockScope scope) {
1853: CtArrayAccess<?, ?> a = factory.Core().createArrayAccess();
1854: context.enter(a, arrayReference);
1855: arrayReference.receiver.traverse(this , scope);
1856: context.arguments.push(a);
1857: arrayReference.position.traverse(this , scope);
1858: context.arguments.pop();
1859: return false;
1860: }
1861:
1862: @Override
1863: public boolean visit(ArrayTypeReference arrayTypeReference,
1864: BlockScope scope) {
1865: CtLiteral<CtTypeReference<?>> l = factory.Core()
1866: .createLiteral();
1867: l.setValue(references
1868: .getTypeReference(arrayTypeReference.resolvedType));
1869: context.enter(l, arrayTypeReference);
1870: return true;
1871: }
1872:
1873: @Override
1874: public boolean visit(AssertStatement assertStatement,
1875: BlockScope scope) {
1876: CtAssert<?> a = factory.Core().createAssert();
1877: context.enter(a, assertStatement);
1878: assertStatement.assertExpression.traverse(this , scope);
1879: context.arguments.push(a);
1880: if (assertStatement.exceptionArgument != null) {
1881: assertStatement.exceptionArgument.traverse(this , scope);
1882: }
1883: context.arguments.pop();
1884: return false;
1885: }
1886:
1887: @Override
1888: public boolean visit(Assignment assignment, BlockScope scope) {
1889: CtAssignment<Object, Object> assign = factory.Core()
1890: .createAssignment();
1891: context.enter(assign, assignment);
1892: return true;
1893: }
1894:
1895: @Override
1896: public boolean visit(BinaryExpression binaryExpression,
1897: BlockScope scope) {
1898: CtBinaryOperator<?> op = factory.Core().createBinaryOperator();
1899: op
1900: .setKind(getBinaryOperatorKind((binaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT));
1901: context.enter(op, binaryExpression);
1902: return true;
1903: }
1904:
1905: @Override
1906: public boolean visit(Block block, BlockScope scope) {
1907: CtBlock<?> b = factory.Core().createBlock();
1908: context.enter(b, block);
1909: return true;
1910: }
1911:
1912: @Override
1913: public boolean visit(BreakStatement breakStatement, BlockScope scope) {
1914: CtBreak b = factory.Core().createBreak();
1915: if (breakStatement.label != null)
1916: b.setTargetLabel(new String(breakStatement.label));
1917: context.enter(b, breakStatement);
1918: return true;
1919: }
1920:
1921: @Override
1922: public boolean visit(CaseStatement caseStatement, BlockScope scope) {
1923: CtCase<?> c = factory.Core().createCase();
1924: context.enter(c, caseStatement);
1925:
1926: if (caseStatement.constantExpression != null) {
1927: context.selector = true;
1928: caseStatement.constantExpression.traverse(this , scope);
1929: context.selector = false;
1930: }
1931: return false;
1932: }
1933:
1934: @Override
1935: public boolean visit(CastExpression castExpression, BlockScope scope) {
1936: context.casts.add(references
1937: .getTypeReference(castExpression.resolvedType));
1938: castExpression.expression.traverse(this , scope);
1939: return false;
1940: }
1941:
1942: @Override
1943: public boolean visit(CharLiteral charLiteral, BlockScope scope) {
1944: CtLiteral<Character> l = factory.Core().createLiteral();
1945: l.setValue(charLiteral.constant.charValue());
1946: context.enter(l, charLiteral);
1947: return true;
1948: }
1949:
1950: @SuppressWarnings("unchecked")
1951: @Override
1952: public boolean visit(ClassLiteralAccess classLiteral,
1953: BlockScope scope) {
1954: CtTypeReference ref = references
1955: .getTypeReference(classLiteral.targetType);
1956: CtFieldReference fr = factory.Core().createFieldReference();
1957: fr.setSimpleName("class");
1958: fr.setType(ref);
1959: fr.setDeclaringType(ref);
1960:
1961: CtFieldAccess<Class> fa = factory.Core().createFieldAccess();
1962: fa.setType(ref);
1963: fa.setVariable(fr);
1964:
1965: context.enter(fa, classLiteral);
1966:
1967: return true;
1968: }
1969:
1970: @Override
1971: public boolean visit(CompoundAssignment compoundAssignment,
1972: BlockScope scope) {
1973: CtOperatorAssignment<Object, Object> a = factory.Core()
1974: .createOperatorAssignment();
1975: a.setKind(getBinaryOperatorKind(compoundAssignment.operator));
1976: context.enter(a, compoundAssignment);
1977: return super .visit(compoundAssignment, scope);
1978: }
1979:
1980: @Override
1981: public boolean visit(ConditionalExpression conditionalExpression,
1982: BlockScope scope) {
1983: CtConditional<?> c = factory.Core().createConditional();
1984: context.enter(c, conditionalExpression);
1985: return super .visit(conditionalExpression, scope);
1986: }
1987:
1988: @SuppressWarnings("unchecked")
1989: public boolean visit(ConstructorDeclaration constructorDeclaration,
1990: ClassScope scope) {
1991: CtConstructor c = factory.Core().createConstructor();
1992: c.setModifiers(getModifier(constructorDeclaration.modifiers));
1993:
1994: c.setDocComment(getJavaDoc(constructorDeclaration.javadoc,
1995: scope.referenceCompilationUnit()));
1996:
1997: context.enter(c, constructorDeclaration);
1998:
1999: if (constructorDeclaration.annotations != null) {
2000: int annotationsLength = constructorDeclaration.annotations.length;
2001: for (int i = 0; i < annotationsLength; i++)
2002: constructorDeclaration.annotations[i].traverse(this ,
2003: constructorDeclaration.scope);
2004: }
2005:
2006: context.pushArgument(c);
2007: if (constructorDeclaration.arguments != null) {
2008: int argumentLength = constructorDeclaration.arguments.length;
2009: for (int i = 0; i < argumentLength; i++)
2010: constructorDeclaration.arguments[i].traverse(this ,
2011: constructorDeclaration.scope);
2012: }
2013: context.popArgument(c);
2014:
2015: if (constructorDeclaration.thrownExceptions != null) {
2016: for (TypeReference r : constructorDeclaration.thrownExceptions)
2017: c.getThrownTypes().add(
2018: references.getTypeReference(r.resolvedType));
2019: }
2020:
2021: // Create block
2022: if (!constructorDeclaration.isAbstract()) {
2023: CtBlock<?> b = factory.Core().createBlock();
2024: context.enter(b, constructorDeclaration);
2025: }
2026:
2027: if (constructorDeclaration.constructorCall != null)
2028: constructorDeclaration.constructorCall.traverse(this ,
2029: constructorDeclaration.scope);
2030:
2031: if (constructorDeclaration.statements != null) {
2032: for (Statement s : constructorDeclaration.statements)
2033: s.traverse(this , constructorDeclaration.scope);
2034: }
2035: return false;
2036: }
2037:
2038: @Override
2039: public boolean visit(ContinueStatement continueStatement,
2040: BlockScope scope) {
2041: CtContinue c = factory.Core().createContinue();
2042: context.enter(c, continueStatement);
2043: return true;
2044: }
2045:
2046: @Override
2047: public boolean visit(DoStatement doStatement, BlockScope scope) {
2048: CtDo d = factory.Core().createDo();
2049: context.enter(d, doStatement);
2050: return true;
2051: }
2052:
2053: @Override
2054: public boolean visit(DoubleLiteral doubleLiteral, BlockScope scope) {
2055: CtLiteral<Double> d = factory.Core().createLiteral();
2056: d.setValue(doubleLiteral.constant.doubleValue());
2057: context.enter(d, doubleLiteral);
2058: return true;
2059: }
2060:
2061: @Override
2062: public boolean visit(EqualExpression equalExpression,
2063: BlockScope scope) {
2064: CtBinaryOperator<?> op = factory.Core().createBinaryOperator();
2065: op
2066: .setKind(getBinaryOperatorKind((equalExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT));
2067: context.enter(op, equalExpression);
2068: return true; // do nothing by default, keep traversing
2069: }
2070:
2071: @SuppressWarnings("unchecked")
2072: @Override
2073: public boolean visit(ExplicitConstructorCall explicitConstructor,
2074: BlockScope scope) {
2075: CtInvocation inv = factory.Core().createInvocation();
2076: if (explicitConstructor.isImplicitSuper()) {
2077: inv.setImplicit(true);
2078: }
2079: inv.setExecutable(references
2080: .getExecutableReference(explicitConstructor.binding));
2081: inv.getExecutable().setType(
2082: inv.getExecutable().getDeclaringType());
2083: inv.setType(inv.getExecutable().getType());
2084:
2085: context.enter(inv, explicitConstructor);
2086:
2087: if (explicitConstructor.qualification != null) {
2088: explicitConstructor.qualification.traverse(this , scope);
2089: }
2090: if (explicitConstructor.typeArguments != null) {
2091: for (int i = 0, typeArgumentsLength = explicitConstructor.typeArguments.length; i < typeArgumentsLength; i++) {
2092: explicitConstructor.typeArguments[i].traverse(this ,
2093: scope);
2094: }
2095: }
2096:
2097: context.arguments.push(inv);
2098: if (explicitConstructor.arguments != null) {
2099: for (int i = 0, argumentLength = explicitConstructor.arguments.length; i < argumentLength; i++)
2100: explicitConstructor.arguments[i].traverse(this , scope);
2101: }
2102: context.arguments.pop();
2103:
2104: return false;
2105: }
2106:
2107: public boolean visit(ExtendedStringLiteral extendedStringLiteral,
2108: BlockScope scope) {
2109: CtLiteral<String> l = factory.Core().createLiteral();
2110: l.setValue(new String(extendedStringLiteral.source()));
2111: context.enter(l, extendedStringLiteral);
2112: return true;
2113: }
2114:
2115: @Override
2116: public boolean visit(FalseLiteral falseLiteral, BlockScope scope) {
2117: CtLiteral<Boolean> l = factory.Core().createLiteral();
2118: l.setValue(false);
2119: context.enter(l, falseLiteral);
2120: return true;
2121: }
2122:
2123: @SuppressWarnings("unchecked")
2124: @Override
2125: public boolean visit(FieldDeclaration fieldDeclaration,
2126: MethodScope scope) {
2127: CtField<?> field = factory.Core().createField();
2128: field.setSimpleName(new String(fieldDeclaration.name));
2129: if (fieldDeclaration.type != null)
2130: field
2131: .setType(references
2132: .getTypeReference(fieldDeclaration.type.resolvedType));
2133: field.setModifiers(getModifier(fieldDeclaration.modifiers));
2134:
2135: field.setDocComment(getJavaDoc(fieldDeclaration.javadoc, scope
2136: .referenceCompilationUnit()));
2137:
2138: context.enter(field, fieldDeclaration);
2139:
2140: if (fieldDeclaration.annotations != null) {
2141: int annotationsLength = fieldDeclaration.annotations.length;
2142: for (int i = 0; i < annotationsLength; i++)
2143: fieldDeclaration.annotations[i].traverse(this , scope);
2144: }
2145:
2146: if (fieldDeclaration.initialization != null)
2147: fieldDeclaration.initialization.traverse(this , scope);
2148: return false;
2149: }
2150:
2151: @SuppressWarnings("unchecked")
2152: public boolean visit(FieldReference fieldReference, BlockScope scope) {
2153: CtFieldAccess<?> acc = factory.Core().createFieldAccess();
2154: acc.setVariable(references
2155: .getVariableReference(fieldReference.binding));
2156: acc.setType(references
2157: .getTypeReference(fieldReference.resolvedType));
2158: if (fieldReference.receiverType instanceof ArrayBinding
2159: && new String(fieldReference.token).equals("length")) {
2160: acc
2161: .getVariable()
2162: .setDeclaringType(
2163: references
2164: .getTypeReference(fieldReference.receiverType));
2165: }
2166: context.enter(acc, fieldReference);
2167:
2168: context.target.push(acc);
2169: fieldReference.receiver.traverse(this , scope);
2170: context.target.pop();
2171: return false;
2172: }
2173:
2174: @Override
2175: public boolean visit(FloatLiteral floatLiteral, BlockScope scope) {
2176: CtLiteral<Float> l = factory.Core().createLiteral();
2177: l.setValue(floatLiteral.constant.floatValue());
2178: context.enter(l, floatLiteral);
2179: return true;
2180: }
2181:
2182: @Override
2183: public boolean visit(ForeachStatement forStatement, BlockScope scope) {
2184: CtForEach fe = factory.Core().createForEach();
2185: context.enter(fe, forStatement);
2186: return true;
2187: }
2188:
2189: @Override
2190: public boolean visit(ForStatement forStatement, BlockScope scope) {
2191: CtFor for1 = factory.Core().createFor();
2192: context.enter(for1, forStatement);
2193:
2194: if (forStatement.initializations != null) {
2195: context.forinit = true;
2196: int initializationsLength = forStatement.initializations.length;
2197: for (int i = 0; i < initializationsLength; i++)
2198: forStatement.initializations[i].traverse(this , scope);
2199: context.forinit = false;
2200: }
2201: if (forStatement.condition != null)
2202: forStatement.condition.traverse(this , scope);
2203:
2204: if (forStatement.increments != null) {
2205: context.forupdate = true;
2206: int incrementsLength = forStatement.increments.length;
2207: for (int i = 0; i < incrementsLength; i++)
2208: forStatement.increments[i].traverse(this , scope);
2209: context.forupdate = false;
2210: }
2211: if (forStatement.action != null)
2212: forStatement.action.traverse(this , scope);
2213:
2214: return false;
2215: }
2216:
2217: @Override
2218: public boolean visit(IfStatement ifStatement, BlockScope scope) {
2219: CtIf ifs = factory.Core().createIf();
2220: context.enter(ifs, ifStatement);
2221: return super .visit(ifStatement, scope);
2222: }
2223:
2224: @Override
2225: public boolean visit(Initializer initializer, MethodScope scope) {
2226: CtAnonymousExecutable b = factory.Core()
2227: .createAnonymousExecutable();
2228: if (initializer.isStatic())
2229: b.getModifiers().add(ModifierKind.STATIC);
2230: context.enter(b, initializer);
2231: return true;
2232: }
2233:
2234: @Override
2235: public boolean visit(InstanceOfExpression instanceOfExpression,
2236: BlockScope scope) {
2237: CtBinaryOperator<?> op = factory.Core().createBinaryOperator();
2238: op.setKind(BinaryOperatorKind.INSTANCEOF);
2239: context.enter(op, instanceOfExpression);
2240: return true;
2241: }
2242:
2243: @SuppressWarnings("unchecked")
2244: @Override
2245: public boolean visit(IntLiteral intLiteral, BlockScope scope) {
2246: CtLiteral<Integer> l = factory.Core().createLiteral();
2247: l.setType(references.getTypeReference(intLiteral.resolvedType));
2248: l.setValue(intLiteral.value);
2249: context.enter(l, intLiteral);
2250: return true;
2251: }
2252:
2253: @Override
2254: public boolean visit(LabeledStatement labeledStatement,
2255: BlockScope scope) {
2256: context.label.push(new String(labeledStatement.label));
2257: return true;
2258: }
2259:
2260: @SuppressWarnings("unchecked")
2261: @Override
2262: public boolean visit(LocalDeclaration localDeclaration,
2263: BlockScope scope) {
2264: CtLocalVariable<?> v = factory.Core().createLocalVariable();
2265: v.setSimpleName(new String(localDeclaration.name));
2266: v.setType(references
2267: .getTypeReference(localDeclaration.type.resolvedType));
2268: v.setModifiers(getModifier(localDeclaration.modifiers));
2269: context.enter(v, localDeclaration);
2270:
2271: if (localDeclaration.initialization != null) {
2272: context.arguments.push(v);
2273: localDeclaration.initialization.traverse(this , scope);
2274: context.arguments.pop();
2275: }
2276:
2277: if (localDeclaration.annotations != null)
2278: for (Annotation a : localDeclaration.annotations)
2279: a.traverse(this , scope);
2280:
2281: return false;
2282: }
2283:
2284: @SuppressWarnings("unchecked")
2285: @Override
2286: public boolean visit(LongLiteral longLiteral, BlockScope scope) {
2287: CtLiteral<Long> l = factory.Core().createLiteral();
2288: l.setValue(longLiteral.constant.longValue());
2289: l
2290: .setType(references
2291: .getTypeReference(longLiteral.resolvedType));
2292: context.enter(l, longLiteral);
2293: return true;
2294: }
2295:
2296: @SuppressWarnings("unchecked")
2297: @Override
2298: public boolean visit(MarkerAnnotation annotation, BlockScope scope) {
2299: CtAnnotation a = factory.Core().createAnnotation();
2300: a.setAnnotationType(references
2301: .getTypeReference(annotation.resolvedType));
2302: context.enter(a, annotation);
2303: return true;
2304: }
2305:
2306: @SuppressWarnings("unchecked")
2307: @Override
2308: public boolean visit(MemberValuePair pair, BlockScope scope) {
2309: context.annotationValueName.push(new String(pair.name));
2310: return true;
2311: }
2312:
2313: @SuppressWarnings("unchecked")
2314: @Override
2315: public boolean visit(MessageSend messageSend, BlockScope scope) {
2316: CtInvocation<?> inv = factory.Core().createInvocation();
2317: if (messageSend.binding != null)
2318: inv.setExecutable(references
2319: .getExecutableReference(messageSend.binding));
2320: // inv
2321: // .setType(references
2322: // .getTypeReference(messageSend.binding.returnType));
2323: context.enter(inv, messageSend);
2324: if (!(messageSend.receiver.getClass()
2325: .equals(ThisReference.class)))
2326: messageSend.receiver.traverse(this , scope);
2327: context.pushArgument(inv);
2328: if (messageSend.arguments != null)
2329: for (Expression e : messageSend.arguments) {
2330: e.traverse(this , scope);
2331: }
2332: context.popArgument(inv);
2333: return false;
2334: }
2335:
2336: @SuppressWarnings("unchecked")
2337: @Override
2338: public boolean visit(MethodDeclaration methodDeclaration,
2339: ClassScope scope) {
2340: CtMethod<?> m = factory.Core().createMethod();
2341: m.setSimpleName(new String(methodDeclaration.selector));
2342: m
2343: .setType(references
2344: .getTypeReference(methodDeclaration.returnType.resolvedType));
2345: m.setModifiers(getModifier(methodDeclaration.modifiers));
2346: if (methodDeclaration.thrownExceptions != null) {
2347: for (TypeReference r : methodDeclaration.thrownExceptions)
2348: m.getThrownTypes().add(
2349: references.getTypeReference(r.resolvedType));
2350: }
2351: for (TypeBinding b : methodDeclaration.binding.typeVariables) {
2352: m.getFormalTypeParameters().add(
2353: references.getBoundedTypeReference(b));
2354: }
2355:
2356: m.setDocComment(getJavaDoc(methodDeclaration.javadoc, scope
2357: .referenceCompilationUnit()));
2358:
2359: context.enter(m, methodDeclaration);
2360:
2361: if (methodDeclaration.annotations != null)
2362: for (Annotation a : methodDeclaration.annotations)
2363: a.traverse(this , methodDeclaration.scope);
2364:
2365: if (methodDeclaration.arguments != null)
2366: for (Argument a : methodDeclaration.arguments)
2367: a.traverse(this , methodDeclaration.scope);
2368:
2369: // Create block
2370: if (!methodDeclaration.isAbstract()
2371: && (methodDeclaration.modifiers & ClassFileConstants.AccNative) == 0) {
2372: CtBlock<?> b = factory.Core().createBlock();
2373: context.enter(b, methodDeclaration);
2374: }
2375:
2376: if (methodDeclaration.statements != null) {
2377: for (Statement s : methodDeclaration.statements)
2378: s.traverse(this , methodDeclaration.scope);
2379: }
2380: return false;
2381: }
2382:
2383: @SuppressWarnings("unchecked")
2384: @Override
2385: public boolean visit(NormalAnnotation annotation, BlockScope scope) {
2386: CtAnnotation a = factory.Core().createAnnotation();
2387: a.setAnnotationType(references
2388: .getTypeReference(annotation.resolvedType));
2389: context.enter(a, annotation);
2390: return true;
2391: }
2392:
2393: @SuppressWarnings("unchecked")
2394: @Override
2395: public boolean visit(NullLiteral nullLiteral, BlockScope scope) {
2396: CtLiteral<?> lit = factory.Core().createLiteral();
2397: CtTypeReference ref = factory.Core().createTypeReference();
2398: ref.setSimpleName(CtTypeReference.NULL_TYPE_NAME);
2399: lit.setType(ref);
2400: context.enter(lit, nullLiteral);
2401: return true;
2402: }
2403:
2404: @Override
2405: public boolean visit(OR_OR_Expression or_or_Expression,
2406: BlockScope scope) {
2407: CtBinaryOperator<?> op = factory.Core().createBinaryOperator();
2408: op
2409: .setKind(getBinaryOperatorKind((or_or_Expression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT));
2410: context.enter(op, or_or_Expression);
2411: return true;
2412: }
2413:
2414: @Override
2415: public boolean visit(
2416: ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference,
2417: ClassScope scope) {
2418: CtLiteral<CtTypeReference<?>> l = factory.Core()
2419: .createLiteral();
2420: l
2421: .setValue(references
2422: .getBoundedTypeReference(parameterizedQualifiedTypeReference.resolvedType));
2423: context.enter(l, parameterizedQualifiedTypeReference);
2424: return true;
2425: }
2426:
2427: @Override
2428: public boolean visit(
2429: ParameterizedSingleTypeReference parameterizedSingleTypeReference,
2430: BlockScope scope) {
2431: CtLiteral<CtTypeReference<?>> l = factory.Core()
2432: .createLiteral();
2433: l
2434: .setValue(references
2435: .getBoundedTypeReference(parameterizedSingleTypeReference.resolvedType));
2436: context.enter(l, parameterizedSingleTypeReference);
2437: return true;
2438: }
2439:
2440: @Override
2441: public boolean visit(
2442: ParameterizedSingleTypeReference parameterizedSingleTypeReference,
2443: ClassScope scope) {
2444: CtLiteral<CtTypeReference<?>> l = factory.Core()
2445: .createLiteral();
2446: l
2447: .setValue(references
2448: .getBoundedTypeReference(parameterizedSingleTypeReference.resolvedType));
2449: context.enter(l, parameterizedSingleTypeReference);
2450: return super .visit(parameterizedSingleTypeReference, scope);
2451: }
2452:
2453: @Override
2454: public boolean visit(PostfixExpression postfixExpression,
2455: BlockScope scope) {
2456: CtUnaryOperator<?> op = factory.Core().createUnaryOperator();
2457: if (postfixExpression.operator == OperatorIds.PLUS)
2458: op.setKind(UnaryOperatorKind.POSTINC);
2459: if (postfixExpression.operator == OperatorIds.MINUS)
2460: op.setKind(UnaryOperatorKind.POSTDEC);
2461: context.enter(op, postfixExpression);
2462: return true;
2463: }
2464:
2465: @Override
2466: public boolean visit(PrefixExpression prefixExpression,
2467: BlockScope scope) {
2468: CtUnaryOperator<?> op = factory.Core().createUnaryOperator();
2469: if (prefixExpression.operator == OperatorIds.PLUS)
2470: op.setKind(UnaryOperatorKind.PREINC);
2471: if (prefixExpression.operator == OperatorIds.MINUS)
2472: op.setKind(UnaryOperatorKind.PREDEC);
2473: context.enter(op, prefixExpression);
2474: return true;
2475: }
2476:
2477: @SuppressWarnings("unchecked")
2478: @Override
2479: public boolean visit(
2480: QualifiedAllocationExpression qualifiedAllocationExpression,
2481: BlockScope scope) {
2482: boolean ret = visit(
2483: (AllocationExpression) qualifiedAllocationExpression,
2484: scope);
2485: if (qualifiedAllocationExpression.enclosingInstance != null)
2486: qualifiedAllocationExpression.enclosingInstance.traverse(
2487: this , scope);
2488: if (qualifiedAllocationExpression.anonymousType != null)
2489: qualifiedAllocationExpression.anonymousType.traverse(this ,
2490: scope);
2491:
2492: return ret;
2493: }
2494:
2495: @SuppressWarnings("unchecked")
2496: @Override
2497: public boolean visit(QualifiedNameReference qualifiedNameReference,
2498: BlockScope scope) {
2499: if (qualifiedNameReference.binding instanceof FieldBinding) {
2500: CtFieldAccess<?> fa = factory.Core().createFieldAccess();
2501: fa.setVariable(references
2502: .getVariableReference(qualifiedNameReference
2503: .fieldBinding()));
2504:
2505: if (qualifiedNameReference.otherBindings != null)
2506: for (FieldBinding b : qualifiedNameReference.otherBindings) {
2507: if (b != null) {
2508: CtFieldAccess other = factory.Core()
2509: .createFieldAccess();
2510: other.setVariable(references
2511: .getVariableReference(b));
2512: other.setTarget(fa);
2513: fa.setParent(other);
2514: fa = other;
2515: }
2516: }
2517: context.enter(fa, qualifiedNameReference);
2518: return true;
2519: } else if (qualifiedNameReference.binding instanceof VariableBinding) {
2520: CtVariableAccess va = factory.Core().createVariableAccess();
2521: va
2522: .setVariable(references
2523: .getVariableReference((VariableBinding) qualifiedNameReference.binding));
2524: va.setType(va.getVariable().getType());
2525: if (qualifiedNameReference.otherBindings != null) {
2526: for (FieldBinding b : qualifiedNameReference.otherBindings) {
2527: CtFieldAccess fa = factory.Core()
2528: .createFieldAccess();
2529: fa.setTarget(va);
2530: fa.setVariable(references.getVariableReference(b));
2531: fa
2532: .setType(references
2533: .getTypeReference(qualifiedNameReference.resolvedType));
2534: va.setParent(fa);
2535: va = fa;
2536: }
2537: }
2538: context.enter(va, qualifiedNameReference);
2539: return false;
2540: }
2541: return false;
2542: }
2543:
2544: @Override
2545: public boolean visit(QualifiedThisReference qualifiedThisReference,
2546: BlockScope scope) {
2547: return visit((ThisReference) qualifiedThisReference, scope);
2548: }
2549:
2550: @Override
2551: public boolean visit(QualifiedTypeReference arg0, BlockScope arg1) {
2552: CtLiteral<CtTypeReference<?>> l = factory.Core()
2553: .createLiteral();
2554: l.setValue(references.getTypeReference(arg0.resolvedType));
2555: context.enter(l, arg0);
2556: return true; // do nothing by default, keep traversing
2557: }
2558:
2559: @Override
2560: public boolean visit(ReturnStatement returnStatement,
2561: BlockScope scope) {
2562: CtReturn<?> ret = factory.Core().createReturn();
2563: context.enter(ret, returnStatement);
2564: return true;
2565: }
2566:
2567: @SuppressWarnings("unchecked")
2568: @Override
2569: public boolean visit(SingleMemberAnnotation annotation,
2570: BlockScope scope) {
2571: CtAnnotation a = factory.Core().createAnnotation();
2572: a.setAnnotationType(references
2573: .getTypeReference(annotation.resolvedType));
2574: context.enter(a, annotation);
2575: context.annotationValueName.push("value");
2576: return true;
2577: }
2578:
2579: @SuppressWarnings("unchecked")
2580: @Override
2581: public boolean visit(SingleNameReference singleNameReference,
2582: BlockScope scope) {
2583: CtVariableAccess<?> va = null;
2584: if (singleNameReference.binding instanceof FieldBinding) {
2585: va = factory.Core().createFieldAccess();
2586: va.setVariable(references
2587: .getVariableReference(singleNameReference
2588: .fieldBinding()));
2589: } else if (singleNameReference.binding instanceof VariableBinding) {
2590: va = factory.Core().createVariableAccess();
2591: va
2592: .setVariable(references
2593: .getVariableReference((VariableBinding) singleNameReference.binding));
2594: }
2595: if (va != null)
2596: context.enter(va, singleNameReference);
2597: return true;
2598: }
2599:
2600: @Override
2601: public boolean visit(SingleTypeReference singleTypeReference,
2602: BlockScope scope) {
2603: CtLiteral<CtTypeReference<?>> l = factory.Core()
2604: .createLiteral();
2605: l.setValue(references
2606: .getTypeReference(singleTypeReference.resolvedType));
2607: context.enter(l, singleTypeReference);
2608: return true; // do nothing by default, keep traversing
2609: }
2610:
2611: @Override
2612: public boolean visit(SingleTypeReference singleTypeReference,
2613: ClassScope scope) {
2614: CtLiteral<CtTypeReference<?>> l = factory.Core()
2615: .createLiteral();
2616: l.setValue(references
2617: .getTypeReference(singleTypeReference.resolvedType));
2618: context.enter(l, singleTypeReference);
2619: return true; // do nothing by default, keep traversing
2620: }
2621:
2622: @SuppressWarnings("unchecked")
2623: @Override
2624: public boolean visit(StringLiteral stringLiteral, BlockScope scope) {
2625: CtLiteral<String> s = factory.Core().createLiteral();
2626: // references.getTypeReference(stringLiteral.resolvedType) can be null
2627: s.setType(factory.Type().createReference(String.class));
2628: s.setValue(new String(stringLiteral.source()));
2629: context.enter(s, stringLiteral);
2630: return true;
2631: }
2632:
2633: @Override
2634: public boolean visit(StringLiteralConcatenation literal,
2635: BlockScope scope) {
2636: CtBinaryOperator<String> op = factory.Core()
2637: .createBinaryOperator();
2638: op.setKind(BinaryOperatorKind.PLUS);
2639: context.enter(op, literal);
2640:
2641: List<Expression> exp = new ArrayList<Expression>();
2642: for (int i = 0; i < literal.counter; i++)
2643: exp.add(literal.literals[i]);
2644:
2645: createExpression(literal, scope, exp);
2646: return false;
2647: }
2648:
2649: @SuppressWarnings("unchecked")
2650: @Override
2651: public boolean visit(SuperReference super Reference, BlockScope scope) {
2652: CtFieldReference<?> fr = factory.Core().createFieldReference();
2653: CtTypeReference ref = references
2654: .getTypeReference(super Reference.resolvedType);
2655: fr.setSimpleName("super");
2656: fr.setDeclaringType(ref);
2657: fr.setType(ref);
2658:
2659: CtFieldAccess fa = factory.Core().createFieldAccess();
2660: fa.setVariable(fr);
2661: context.enter(fa, super Reference);
2662: return super .visit(super Reference, scope);
2663: }
2664:
2665: @Override
2666: public boolean visit(SwitchStatement switchStatement,
2667: BlockScope scope) {
2668: CtSwitch<?> s = factory.Core().createSwitch();
2669: context.enter(s, switchStatement);
2670:
2671: switchStatement.expression
2672: .traverse(this , switchStatement.scope);
2673:
2674: if (switchStatement.statements != null) {
2675: int statementsLength = switchStatement.statements.length;
2676: for (int i = 0; i < statementsLength; i++) {
2677: if (switchStatement.statements[i] instanceof CaseStatement) {
2678: if (context.stack.peek().element instanceof CtCase) {
2679: context.exit(context.stack.peek().node);
2680: }
2681: CaseStatement cas = (CaseStatement) switchStatement.statements[i];
2682:
2683: visit(cas, switchStatement.scope);
2684: } else {
2685: switchStatement.statements[i].traverse(this ,
2686: switchStatement.scope);
2687: }
2688: }
2689: if (context.stack.peek().element instanceof CtCase) {
2690: context.exit(context.stack.peek().node);
2691: }
2692: }
2693: return false;
2694: }
2695:
2696: @Override
2697: public boolean visit(SynchronizedStatement synchronizedStatement,
2698: BlockScope scope) {
2699: CtSynchronized s = factory.Core().createSynchronized();
2700: context.enter(s, synchronizedStatement);
2701: return super .visit(synchronizedStatement, scope);
2702: }
2703:
2704: @SuppressWarnings("unchecked")
2705: @Override
2706: public boolean visit(ThisReference this Reference, BlockScope scope) {
2707: CtFieldReference fr = factory.Core().createFieldReference();
2708: CtTypeReference typeref = references
2709: .getTypeReference(this Reference.resolvedType);
2710: fr.setDeclaringType(typeref);
2711: fr.setType(typeref);
2712: fr.setSimpleName("this");
2713:
2714: CtFieldAccess fa = factory.Core().createFieldAccess();
2715: fa.setVariable(fr);
2716: fa.setType(typeref);
2717:
2718: context.enter(fa, this Reference);
2719: return true;
2720: }
2721:
2722: public boolean visit(ThrowStatement throwStatement, BlockScope scope) {
2723: CtThrow t = factory.Core().createThrow();
2724: context.enter(t, throwStatement);
2725: return true;
2726: }
2727:
2728: @Override
2729: public boolean visit(TrueLiteral trueLiteral, BlockScope scope) {
2730: CtLiteral<Boolean> l = factory.Core().createLiteral();
2731: l.setValue(true);
2732: context.enter(l, trueLiteral);
2733: return true;
2734: }
2735:
2736: @SuppressWarnings("unchecked")
2737: public boolean visit(TryStatement tryStatement, BlockScope scope) {
2738: CtTry t = factory.Core().createTry();
2739: context.enter(t, tryStatement);
2740: tryStatement.tryBlock.traverse(this , scope);
2741: if (tryStatement.catchArguments != null) {
2742: for (int i = 0; i < tryStatement.catchArguments.length; i++) {
2743: CtCatch c = factory.Core().createCatch();
2744: context.enter(c, tryStatement.catchBlocks[i]);
2745: CtLocalVariable var = factory.Core()
2746: .createLocalVariable();
2747: var.setSimpleName(new String(
2748: tryStatement.catchArguments[i].name));
2749: var
2750: .setType(references
2751: .getTypeReference(tryStatement.catchArguments[i].binding.type));
2752: var
2753: .setModifiers(getModifier(tryStatement.catchArguments[i].modifiers));
2754: context.enter(var, tryStatement.catchArguments[i]);
2755: context.exit(tryStatement.catchArguments[i]);
2756: tryStatement.catchBlocks[i].traverse(this , scope);
2757: context.exit(tryStatement.catchBlocks[i]);
2758: }
2759: }
2760: if (tryStatement.finallyBlock != null) {
2761: context.finallyzer.push(t);
2762: tryStatement.finallyBlock.traverse(this , scope);
2763: context.finallyzer.pop();
2764: }
2765: return false;
2766: }
2767:
2768: @Override
2769: public boolean visit(TypeDeclaration localTypeDeclaration,
2770: BlockScope scope) {
2771: CtSimpleType<?> t = createType(localTypeDeclaration);
2772: t.setDocComment(getJavaDoc(localTypeDeclaration.javadoc, scope
2773: .referenceCompilationUnit()));
2774: context.enter(t, localTypeDeclaration);
2775:
2776: // AST bug HACK (see TypeDeclaration.traverse)
2777: if (localTypeDeclaration.fields != null) {
2778: int length = localTypeDeclaration.fields.length;
2779: for (int i = 0; i < length; i++) {
2780: FieldDeclaration field;
2781: if ((field = localTypeDeclaration.fields[i]).isStatic()) {
2782: // local type actually can have static fields
2783: field.traverse(this ,
2784: localTypeDeclaration.initializerScope);
2785: }
2786: }
2787: }
2788:
2789: return true;
2790: }
2791:
2792: @Override
2793: public boolean visit(TypeDeclaration memberTypeDeclaration,
2794: ClassScope scope) {
2795: CtSimpleType<?> type = createType(memberTypeDeclaration);
2796: type.setDocComment(getJavaDoc(memberTypeDeclaration.javadoc,
2797: scope.referenceCompilationUnit()));
2798: context.enter(type, memberTypeDeclaration);
2799:
2800: // AST bug HACK
2801: if (memberTypeDeclaration.annotations != null)
2802: for (Annotation a : memberTypeDeclaration.annotations) {
2803: a.traverse(this , (BlockScope) null);
2804: }
2805:
2806: return true;
2807: }
2808:
2809: @Override
2810: public boolean visit(TypeDeclaration typeDeclaration,
2811: CompilationUnitScope scope) {
2812:
2813: if (new String(typeDeclaration.name).equals("package-info")) {
2814: CtPackage pack = factory.Package().getOrCreate(
2815: new String(typeDeclaration.binding.fPackage
2816: .readableName()));
2817: context.enter(pack, typeDeclaration);
2818:
2819: // AST bug HACK
2820: if (typeDeclaration.annotations != null)
2821: for (Annotation a : typeDeclaration.annotations) {
2822: a.traverse(this , (BlockScope) null);
2823: }
2824: return false;
2825: } else {
2826: CtSimpleType<?> type = createType(typeDeclaration);
2827:
2828: type.setDocComment(getJavaDoc(typeDeclaration.javadoc,
2829: scope.referenceContext));
2830:
2831: CtPackage pack = null;
2832: if (!template) {
2833:
2834: if (typeDeclaration.binding.fPackage
2835: .shortReadableName() != null
2836: && typeDeclaration.binding.fPackage
2837: .shortReadableName().length > 0) {
2838: pack = factory.Package().getOrCreate(
2839: new String(typeDeclaration.binding.fPackage
2840: .shortReadableName()));
2841: } else {
2842: pack = factory.Package().getOrCreate(
2843: CtPackage.TOP_LEVEL_PACKAGE_NAME);
2844: }
2845: context.enter(pack, typeDeclaration);
2846: } else {
2847: char[] packname = typeDeclaration.binding.fPackage
2848: .shortReadableName();
2849: if (packname.length > 0) {
2850: pack = factory.Core().createPackage();
2851: pack.setSimpleName(new String(packname));
2852: type.setParent(pack);
2853: context.enter(pack, typeDeclaration);
2854: }
2855: }
2856: context.compilationunitdeclaration = scope.referenceContext;
2857: context.enter(type, typeDeclaration);
2858:
2859: // AST bug HACK
2860: if (typeDeclaration.annotations != null)
2861: for (Annotation a : typeDeclaration.annotations) {
2862: a.traverse(this , (BlockScope) null);
2863: }
2864:
2865: if (typeDeclaration.memberTypes != null) {
2866: int length = typeDeclaration.memberTypes.length;
2867: for (int i = 0; i < length; i++)
2868: typeDeclaration.memberTypes[i].traverse(this ,
2869: typeDeclaration.scope);
2870: }
2871: if (typeDeclaration.fields != null) {
2872: int length = typeDeclaration.fields.length;
2873: for (int i = 0; i < length; i++) {
2874: FieldDeclaration field;
2875: if ((field = typeDeclaration.fields[i]).isStatic()) {
2876: field.traverse(this ,
2877: typeDeclaration.staticInitializerScope);
2878: } else {
2879: field.traverse(this ,
2880: typeDeclaration.initializerScope);
2881: }
2882: }
2883: }
2884: if (typeDeclaration.methods != null) {
2885: int length = typeDeclaration.methods.length;
2886: for (int i = 0; i < length; i++)
2887: typeDeclaration.methods[i].traverse(this ,
2888: typeDeclaration.scope);
2889: }
2890: return false;
2891: }
2892: }
2893:
2894: @Override
2895: public boolean visit(UnaryExpression unaryExpression,
2896: BlockScope scope) {
2897: CtUnaryOperator<?> op = factory.Core().createUnaryOperator();
2898: op
2899: .setKind(getUnaryOperator((unaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT));
2900: context.enter(op, unaryExpression);
2901: return true;
2902: }
2903:
2904: @Override
2905: public boolean visit(WhileStatement whileStatement, BlockScope scope) {
2906: CtWhile w = factory.Core().createWhile();
2907: context.enter(w, whileStatement);
2908: return true;
2909: }
2910:
2911: }
|