List of usage examples for org.eclipse.jdt.internal.compiler.ast LocalDeclaration traverse
@Override
public void traverse(ASTVisitor visitor, BlockScope scope)
From source file:lombok.eclipse.handlers.HandleHelper.java
License:Open Source License
@Override public void handle(AnnotationValues<Helper> annotation, Annotation ast, EclipseNode annotationNode) { handleExperimentalFlagUsage(annotationNode, ConfigurationKeys.HELPER_FLAG_USAGE, "@Helper"); EclipseNode annotatedType = annotationNode.up(); EclipseNode containingMethod = annotatedType == null ? null : annotatedType.up(); if (annotatedType == null || containingMethod == null || annotatedType.getKind() != Kind.TYPE || containingMethod.getKind() != Kind.METHOD) { annotationNode.addError("@Helper is legal only on method-local classes."); return;//w ww. ja v a 2s . c om } TypeDeclaration annotatedType_ = (TypeDeclaration) annotatedType.get(); AbstractMethodDeclaration amd = (AbstractMethodDeclaration) containingMethod.get(); Statement[] origStatements = amd.statements; int indexOfType = -1; for (int i = 0; i < origStatements.length; i++) { if (origStatements[i] == annotatedType_) { indexOfType = i; break; } } final List<String> knownMethodNames = new ArrayList<String>(); for (AbstractMethodDeclaration methodOfHelper : annotatedType_.methods) { if (!(methodOfHelper instanceof MethodDeclaration)) continue; char[] name = methodOfHelper.selector; if (name != null && name.length > 0 && name[0] != '<') knownMethodNames.add(new String(name)); } Collections.sort(knownMethodNames); final String[] knownMethodNames_ = knownMethodNames.toArray(new String[knownMethodNames.size()]); final char[] helperName = new char[annotatedType_.name.length + 1]; final boolean[] helperUsed = new boolean[1]; helperName[0] = '$'; System.arraycopy(annotatedType_.name, 0, helperName, 1, helperName.length - 1); ASTVisitor visitor = new ASTVisitor() { @Override public boolean visit(MessageSend messageSend, BlockScope scope) { if (messageSend.receiver instanceof ThisReference) { if ((((ThisReference) messageSend.receiver).bits & ASTNode.IsImplicitThis) == 0) return true; } else if (messageSend.receiver != null) return true; char[] name = messageSend.selector; if (name == null || name.length == 0 || name[0] == '<') return true; String n = new String(name); if (Arrays.binarySearch(knownMethodNames_, n) < 0) return true; messageSend.receiver = new SingleNameReference(helperName, Eclipse.pos(messageSend)); helperUsed[0] = true; return true; } }; for (int i = indexOfType + 1; i < origStatements.length; i++) { origStatements[i].traverse(visitor, null); } if (!helperUsed[0]) { annotationNode.addWarning("No methods of this helper class are ever used."); return; } Statement[] newStatements = new Statement[origStatements.length + 1]; System.arraycopy(origStatements, 0, newStatements, 0, indexOfType + 1); System.arraycopy(origStatements, indexOfType + 1, newStatements, indexOfType + 2, origStatements.length - indexOfType - 1); LocalDeclaration decl = new LocalDeclaration(helperName, 0, 0); decl.modifiers |= ClassFileConstants.AccFinal; AllocationExpression alloc = new AllocationExpression(); alloc.type = new SingleTypeReference(annotatedType_.name, 0L); decl.initialization = alloc; decl.type = new SingleTypeReference(annotatedType_.name, 0L); SetGeneratedByVisitor sgbvVisitor = new SetGeneratedByVisitor(annotationNode.get()); decl.traverse(sgbvVisitor, null); newStatements[indexOfType + 1] = decl; amd.statements = newStatements; }
From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java
License:Open Source License
@Override public boolean visit(TryStatement tryStatement, BlockScope scope) { CtTry t;//from ww w. ja v a 2 s . co m if (tryStatement.resources.length > 0) { t = factory.Core().createTryWithResource(); } else { t = factory.Core().createTry(); } context.enter(t, tryStatement); for (LocalDeclaration localDeclaration : tryStatement.resources) { localDeclaration.traverse(this, scope); } tryStatement.tryBlock.traverse(this, scope); if (tryStatement.catchArguments != null) { for (int i = 0; i < tryStatement.catchArguments.length; i++) { // the jdt catch Argument jdtCatch = tryStatement.catchArguments[i]; // case 1: old catch if (jdtCatch.type instanceof SingleTypeReference || jdtCatch.type instanceof QualifiedTypeReference) { CtTypeReference<Throwable> r = references.getTypeReference(jdtCatch.type.resolvedType); createCtCatch(jdtCatch, r); tryStatement.catchBlocks[i].traverse(this, scope); context.exit(jdtCatch); } else if (jdtCatch.type instanceof UnionTypeReference) { // case 2: Java 7 multiple catch blocks UnionTypeReference utr = (UnionTypeReference) jdtCatch.type; final List<CtTypeReference<?>> refs = new ArrayList<CtTypeReference<?>>( utr.typeReferences.length); for (TypeReference type : utr.typeReferences) { CtTypeReference<Throwable> r = references.getTypeReference(type.resolvedType); refs.add(r); } CtTypeReference<Throwable> r = references.getTypeReference(jdtCatch.type.resolvedType); createCtCatchJava7(jdtCatch, r, refs); tryStatement.catchBlocks[i].traverse(this, scope); context.exit(jdtCatch); } else { throw new RuntimeException("I don't know how to do this"); } } } if (tryStatement.finallyBlock != null) { context.finallyzer.push(t); tryStatement.finallyBlock.traverse(this, scope); context.finallyzer.pop(); } return false; }