List of usage examples for org.eclipse.jdt.internal.compiler.ast TypeDeclaration traverse
public void traverse(ASTVisitor visitor, ClassScope classScope)
From source file:ch.uzh.ifi.seal.changedistiller.ast.java.WhenDeclarationsAreConverted.java
License:Apache License
private void convertClass(String name) { createRootNode(JavaEntityType.METHOD_DECLARATION, name); TypeDeclaration type = CompilationUtils.findType(fCompilation.getCompilationUnit(), name); type.traverse(getDeclarationconverter(), (CompilationUnitScope) null); }
From source file:ch.uzh.ifi.seal.changedistiller.ast.java.WhenSourceCodeChangesAreClassified.java
License:Apache License
private Node convertClassDeclaration(String className, String sourceCode) { JavaCompilation compilation = CompilationUtils.compileSource(sourceCode); TypeDeclaration type = CompilationUtils.findType(compilation.getCompilationUnit(), className); Node root = new Node(JavaEntityType.CLASS, className); root.setEntity(new SourceCodeEntity(className, JavaEntityType.CLASS, new SourceRange(type.declarationSourceStart, type.declarationSourceEnd))); sDeclarationConverter.initialize(root, compilation.getScanner()); type.traverse(sDeclarationConverter, (ClassScope) null); return root;/* w ww . j av a 2s . c o m*/ }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocatorParser.java
License:Open Source License
/** * Parses the member bodies in the given type. * @param type TypeDeclaration//from www. ja va 2s.c o m * @param unit CompilationUnitDeclaration */ protected void parseBodies(TypeDeclaration type, CompilationUnitDeclaration unit) { FieldDeclaration[] fields = type.fields; if (fields != null) { for (int i = 0; i < fields.length; i++) { FieldDeclaration field = fields[i]; if (field instanceof Initializer) this.parse((Initializer) field, type, unit); field.traverse(this.localDeclarationVisitor, null); } } AbstractMethodDeclaration[] methods = type.methods; if (methods != null) { for (int i = 0; i < methods.length; i++) { AbstractMethodDeclaration method = methods[i]; if (method.sourceStart >= type.bodyStart) { // if not synthetic if (method instanceof MethodDeclaration) { MethodDeclaration methodDeclaration = (MethodDeclaration) method; this.parse(methodDeclaration, unit); methodDeclaration.traverse(this.localDeclarationVisitor, (ClassScope) null); } else if (method instanceof ConstructorDeclaration) { ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) method; this.parse(constructorDeclaration, unit, false); constructorDeclaration.traverse(this.localDeclarationVisitor, (ClassScope) null); } } else if (method.isDefaultConstructor()) { method.parseStatements(this, unit); } } } TypeDeclaration[] memberTypes = type.memberTypes; if (memberTypes != null) { for (int i = 0; i < memberTypes.length; i++) { TypeDeclaration memberType = memberTypes[i]; this.parseBodies(memberType, unit); memberType.traverse(this.localDeclarationVisitor, (ClassScope) null); } } }
From source file:com.google.gwt.dev.javac.UnusedImportsRemover.java
License:Apache License
void execImpl(CompilationUnitDeclaration cud) { if (cud.imports == null) { return;// w w w . j a v a 2s . c o m } AccumulateNamesVisitor astVisitor = new AccumulateNamesVisitor(); if (cud.types != null) { for (TypeDeclaration typeDecl : cud.types) { typeDecl.traverse(astVisitor, cud.scope); } } // for some reason JDT does not traverse package annotations even if the traversal started at // the Compilation unit declaration. Hence we do it manually. if (cud.currentPackage != null && cud.currentPackage.annotations != null) { for (Annotation annotation : cud.currentPackage.annotations) { annotation.traverse(astVisitor, (BlockScope) null); } } List<ImportReference> newImports = new ArrayList<ImportReference>(); for (ImportReference importRef : cud.imports) { String importName = new String(importRef.getImportName()[importRef.getImportName().length - 1]); if (importName.equals("*") || // very hacky it seems that this is the only way // to notice a import static blah.Blah.*; importRef.trailingStarPosition > 0 || usedNames.contains(importName)) { // Either a * or a possible reference, so keep it. newImports.add(importRef); } } if (newImports.size() != cud.imports.length) { cud.imports = newImports.toArray(new ImportReference[newImports.size()]); } }
From source file:com.google.gwt.dev.jjs.impl.GwtAstBuilder.java
License:Apache License
/** * Builds all the GWT AST nodes that correspond to one Java source file. * * @param cud The compiled form of the Java source from the JDT. * @param sourceMapPath the path that should be included in a sourcemap. * @param jsniMethods Native methods to add to the AST. * @param jsniRefs Map from JSNI references to their JDT definitions. * @param compilerContext the compiler context. * @return All the types seen in this source file. *//*from w w w . ja va 2 s .c om*/ public List<JDeclaredType> process(CompilationUnitDeclaration cud, String sourceMapPath, Map<MethodDeclaration, JsniMethod> jsniMethods, Map<String, Binding> jsniRefs, CompilerContext compilerContext) { if (cud.types == null) { return Collections.emptyList(); } this.sourceMapPath = sourceMapPath; this.jsniRefs = jsniRefs; this.jsniMethods = jsniMethods; this.compilerContext = compilerContext; newTypes = Lists.newArrayList(); curCud = new CudInfo(cud); for (TypeDeclaration typeDecl : cud.types) { createTypes(typeDecl); } // Now that types exist, cache Object, String, etc. javaLangObject = (JClassType) typeMap.get(cud.scope.getJavaLangObject()); javaLangString = (JClassType) typeMap.get(cud.scope.getJavaLangString()); javaLangClass = (JClassType) typeMap.get(cud.scope.getJavaLangClass()); javaLangThrowable = (JClassType) typeMap.get(cud.scope.getJavaLangThrowable()); for (TypeDeclaration typeDecl : cud.types) { // Resolve super type / interface relationships. resolveTypeRefs(typeDecl); } for (TypeDeclaration typeDecl : cud.types) { // Create fields and empty methods. createMembers(typeDecl); } for (TypeDeclaration typeDecl : cud.types) { // Build the code. typeDecl.traverse(astVisitor, cud.scope); } List<JDeclaredType> result = newTypes; // Clean up. typeMap.clearSource(); this.jsniRefs = jsniRefs; this.jsniMethods = jsniMethods; newTypes = null; curCud = null; javaLangObject = null; javaLangString = null; javaLangClass = null; javaLangThrowable = null; return result; }
From source file:lombok.eclipse.handlers.HandleBuilder.java
License:Open Source License
public EclipseNode makeBuilderClass(EclipseNode tdParent, String builderClassName, TypeParameter[] typeParams, ASTNode source) {//from w w w . ja v a 2s . co m TypeDeclaration parent = (TypeDeclaration) tdParent.get(); TypeDeclaration builder = new TypeDeclaration(parent.compilationResult); builder.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; builder.modifiers |= ClassFileConstants.AccPublic | ClassFileConstants.AccStatic; builder.typeParameters = copyTypeParams(typeParams, source); builder.name = builderClassName.toCharArray(); builder.traverse(new SetGeneratedByVisitor(source), (ClassScope) null); return injectType(tdParent, builder); }
From source file:lombok.eclipse.handlers.HandleFieldNameConstants.java
License:Open Source License
private void createInnerTypeFieldNameConstants(EclipseNode typeNode, EclipseNode errorNode, ASTNode source, AccessLevel level, List<EclipseNode> fields, boolean asEnum, String innerTypeName) { if (fields.isEmpty()) return;//ww w .ja va 2s. c o m ASTVisitor generatedByVisitor = new SetGeneratedByVisitor(source); TypeDeclaration parent = (TypeDeclaration) typeNode.get(); EclipseNode fieldsType = findInnerClass(typeNode, innerTypeName); boolean genConstr = false, genClinit = false; char[] name = innerTypeName.toCharArray(); TypeDeclaration generatedInnerType = null; if (fieldsType == null) { generatedInnerType = new TypeDeclaration(parent.compilationResult); generatedInnerType.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; generatedInnerType.modifiers = toEclipseModifier(level) | (asEnum ? ClassFileConstants.AccEnum : (ClassFileConstants.AccStatic | ClassFileConstants.AccFinal)); generatedInnerType.name = name; fieldsType = injectType(typeNode, generatedInnerType); genConstr = true; genClinit = asEnum; generatedInnerType.traverse(generatedByVisitor, ((TypeDeclaration) typeNode.get()).scope); } else { TypeDeclaration builderTypeDeclaration = (TypeDeclaration) fieldsType.get(); if (asEnum && (builderTypeDeclaration.modifiers & ClassFileConstants.AccEnum) == 0) { errorNode.addError("Existing " + innerTypeName + " must be declared as an 'enum'."); return; } if (!asEnum && (builderTypeDeclaration.modifiers & ClassFileConstants.AccStatic) == 0) { errorNode.addError("Existing " + innerTypeName + " must be declared as a 'static class'."); return; } genConstr = constructorExists(fieldsType) == MemberExistsResult.NOT_EXISTS; } if (genConstr) { ConstructorDeclaration constructor = new ConstructorDeclaration(parent.compilationResult); constructor.selector = name; constructor.modifiers = ClassFileConstants.AccPrivate; ExplicitConstructorCall superCall = new ExplicitConstructorCall(0); superCall.sourceStart = source.sourceStart; superCall.sourceEnd = source.sourceEnd; superCall.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; constructor.constructorCall = superCall; if (!asEnum) constructor.statements = new Statement[0]; injectMethod(fieldsType, constructor); } if (genClinit) { Clinit cli = new Clinit(parent.compilationResult); injectMethod(fieldsType, cli); cli.traverse(generatedByVisitor, ((TypeDeclaration) fieldsType.get()).scope); } for (EclipseNode fieldNode : fields) { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); char[] fName = field.name; if (fieldExists(new String(fName), fieldsType) != MemberExistsResult.NOT_EXISTS) continue; int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; FieldDeclaration constantField = new FieldDeclaration(fName, pS, pE); constantField.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; if (asEnum) { AllocationExpression ac = new AllocationExpression(); ac.enumConstant = constantField; ac.sourceStart = source.sourceStart; ac.sourceEnd = source.sourceEnd; constantField.initialization = ac; constantField.modifiers = 0; } else { constantField.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_STRING, new long[] { p, p, p }); constantField.initialization = new StringLiteral(field.name, pS, pE, 0); constantField.modifiers = ClassFileConstants.AccPublic | ClassFileConstants.AccStatic | ClassFileConstants.AccFinal; } injectField(fieldsType, constantField); constantField.traverse(generatedByVisitor, ((TypeDeclaration) fieldsType.get()).initializerScope); } }
From source file:lombok.eclipse.handlers.HandleSuperBuilder.java
License:Open Source License
private EclipseNode generateBuilderAbstractClass(EclipseNode tdParent, String builderClass, TypeReference superclassBuilderClass, TypeParameter[] typeParams, ASTNode source, String classGenericName, String builderGenericName) { TypeDeclaration parent = (TypeDeclaration) tdParent.get(); TypeDeclaration builder = new TypeDeclaration(parent.compilationResult); builder.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; builder.modifiers |= ClassFileConstants.AccPublic | ClassFileConstants.AccStatic | ClassFileConstants.AccAbstract; builder.name = builderClass.toCharArray(); // Keep any type params of the annotated class. builder.typeParameters = Arrays.copyOf(copyTypeParams(typeParams, source), typeParams.length + 2); // Add builder-specific type params required for inheritable builders. // 1. The return type for the build() method, named "C", which extends the annotated class. TypeParameter o = new TypeParameter(); o.name = classGenericName.toCharArray(); o.type = cloneSelfType(tdParent, source); builder.typeParameters[builder.typeParameters.length - 2] = o; // 2. The return type for all setter methods, named "B", which extends this builder class. o = new TypeParameter(); o.name = builderGenericName.toCharArray(); TypeReference[] typerefs = appendBuilderTypeReferences(typeParams, classGenericName, builderGenericName); o.type = new ParameterizedSingleTypeReference(builderClass.toCharArray(), typerefs, 0, 0); builder.typeParameters[builder.typeParameters.length - 1] = o; builder.superclass = copyType(superclassBuilderClass, source); builder.createDefaultConstructor(false, true); builder.traverse(new SetGeneratedByVisitor(source), (ClassScope) null); return injectType(tdParent, builder); }
From source file:lombok.eclipse.handlers.HandleSuperBuilder.java
License:Open Source License
private EclipseNode generateBuilderImplClass(EclipseNode tdParent, String builderImplClass, String builderAbstractClass, TypeParameter[] typeParams, ASTNode source) { TypeDeclaration parent = (TypeDeclaration) tdParent.get(); TypeDeclaration builder = new TypeDeclaration(parent.compilationResult); builder.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; builder.modifiers |= ClassFileConstants.AccPrivate | ClassFileConstants.AccStatic | ClassFileConstants.AccFinal; builder.name = builderImplClass.toCharArray(); // Add type params if there are any. if (typeParams != null && typeParams.length > 0) builder.typeParameters = copyTypeParams(typeParams, source); if (builderAbstractClass != null) { // Extend the abstract builder. // 1. Add any type params of the annotated class. TypeReference[] typeArgs = new TypeReference[typeParams.length + 2]; for (int i = 0; i < typeParams.length; i++) { typeArgs[i] = new SingleTypeReference(typeParams[i].name, 0); }// w ww . ja v a 2 s. com // 2. The return type for the build() method (named "C" in the abstract builder), which is the annotated class. // 3. The return type for all setter methods (named "B" in the abstract builder), which is this builder class. typeArgs[typeArgs.length - 2] = cloneSelfType(tdParent, source); typeArgs[typeArgs.length - 1] = createTypeReferenceWithTypeParameters(builderImplClass, typeParams); builder.superclass = new ParameterizedSingleTypeReference(builderAbstractClass.toCharArray(), typeArgs, 0, 0); } builder.createDefaultConstructor(false, true); builder.traverse(new SetGeneratedByVisitor(source), (ClassScope) null); return injectType(tdParent, builder); }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.control.Dependencies.java
License:Open Source License
/** * Perform several substitutions in method bodies. * (See TransformStatementsVisitor).//from ww w. j ava 2s.c o m * * BinaryTypes: nothing to do. * * Recursion: NESTED_TEAMS. */ private static boolean establishStatementsTransformed(TypeModel clazz) { TypeDeclaration type = clazz.getAst(); if (type != null) { if (needMethodBodies(type)) { TransformStatementsVisitor transformer = new TransformStatementsVisitor(clazz.getWeavingScheme()); if ((type.bits & ASTNode.IsLocalType) != 0) { MethodScope methodScope = type.scope.methodScope(); if (methodScope != null) transformer.checkPushCallinMethod(methodScope.referenceMethod()); } type.traverse(transformer, type.scope.compilationUnitScope()); } else if (clazz.isTeam()) { if (type.memberTypes != null) { for (int i = 0; i < type.memberTypes.length; i++) { establishStatementsTransformed(type.memberTypes[i].getRoleModel()); } } } if (needMethodBodies(type) || type.isConverted) // converted types may contain local types but no other statements! { RecordLocalTypesVisitor recorder = new RecordLocalTypesVisitor(); recorder.recordLocalTypesFor(type); } } clazz.setState(ITranslationStates.STATE_STATEMENTS_TRANSFORMED); clazz.setMemberState(ITranslationStates.STATE_STATEMENTS_TRANSFORMED); return true; }