List of usage examples for org.eclipse.jdt.internal.compiler.ast ConstructorDeclaration traverse
@Override
public void traverse(ASTVisitor visitor, ClassScope classScope)
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 w ww . j a va2s .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:lombok.eclipse.handlers.HandleConstructor.java
License:Open Source License
public static ConstructorDeclaration createConstructor(AccessLevel level, EclipseNode type, Collection<EclipseNode> fields, boolean allToDefault, Boolean suppressConstructorProperties, EclipseNode sourceNode, List<Annotation> onConstructor) { ASTNode source = sourceNode.get();/*from w ww . j a va 2s .co m*/ TypeDeclaration typeDeclaration = ((TypeDeclaration) type.get()); long p = (long) source.sourceStart << 32 | source.sourceEnd; boolean isEnum = (((TypeDeclaration) type.get()).modifiers & ClassFileConstants.AccEnum) != 0; if (isEnum) level = AccessLevel.PRIVATE; if (suppressConstructorProperties == null) { if (fields.isEmpty()) { suppressConstructorProperties = false; } else { suppressConstructorProperties = Boolean.TRUE.equals(type.getAst() .readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES)); } } ConstructorDeclaration constructor = new ConstructorDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); constructor.modifiers = toEclipseModifier(level); constructor.selector = typeDeclaration.name; constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper); constructor.constructorCall.sourceStart = source.sourceStart; constructor.constructorCall.sourceEnd = source.sourceEnd; constructor.thrownExceptions = null; constructor.typeParameters = null; constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart; constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd; constructor.arguments = null; List<Argument> params = new ArrayList<Argument>(); List<Statement> assigns = new ArrayList<Statement>(); List<Statement> nullChecks = new ArrayList<Statement>(); for (EclipseNode fieldNode : fields) { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); char[] rawName = field.name; char[] fieldName = removePrefixFromField(fieldNode); FieldReference thisX = new FieldReference(rawName, p); int s = (int) (p >> 32); int e = (int) p; thisX.receiver = new ThisReference(s, e); Expression assignmentExpr = allToDefault ? getDefaultExpr(field.type, s, e) : new SingleNameReference(fieldName, p); Assignment assignment = new Assignment(thisX, assignmentExpr, (int) p); assignment.sourceStart = (int) (p >> 32); assignment.sourceEnd = assignment.statementEnd = (int) (p >> 32); assigns.add(assignment); if (!allToDefault) { long fieldPos = (((long) field.sourceStart) << 32) | field.sourceEnd; Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source), Modifier.FINAL); Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); if (nonNulls.length != 0) { Statement nullCheck = generateNullCheck(field, sourceNode); if (nullCheck != null) nullChecks.add(nullCheck); } parameter.annotations = copyAnnotations(source, nonNulls, nullables); params.add(parameter); } } nullChecks.addAll(assigns); constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]); constructor.arguments = params.isEmpty() ? null : params.toArray(new Argument[params.size()]); /* Generate annotations that must be put on the generated method, and attach them. */ { Annotation[] constructorProperties = null; if (!allToDefault && !suppressConstructorProperties && level != AccessLevel.PRIVATE && level != AccessLevel.PACKAGE && !isLocalType(type)) { constructorProperties = createConstructorProperties(source, fields); } constructor.annotations = copyAnnotations(source, onConstructor.toArray(new Annotation[0]), constructorProperties); } constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope); return constructor; }
From source file:lombok.eclipse.handlers.HandleSuperBuilder.java
License:Open Source License
/** * Generates a constructor that has a builder as the only parameter. * The values from the builder are used to initialize the fields of new instances. * * @param typeNode// w w w .j av a 2 s .c o m * the type (with the {@code @Builder} annotation) for which a * constructor should be generated. * @param typeParams * @param builderFields a list of fields in the builder which should be assigned to new instances. * @param source the annotation (used for setting source code locations for the generated code). * @param callBuilderBasedSuperConstructor * If {@code true}, the constructor will explicitly call a super * constructor with the builder as argument. Requires * {@code builderClassAsParameter != null}. */ private void generateBuilderBasedConstructor(EclipseNode typeNode, TypeParameter[] typeParams, List<BuilderFieldData> builderFields, EclipseNode sourceNode, String builderClassName, boolean callBuilderBasedSuperConstructor) { ASTNode source = sourceNode.get(); TypeDeclaration typeDeclaration = ((TypeDeclaration) typeNode.get()); long p = (long) source.sourceStart << 32 | source.sourceEnd; ConstructorDeclaration constructor = new ConstructorDeclaration( ((CompilationUnitDeclaration) typeNode.top().get()).compilationResult); constructor.modifiers = toEclipseModifier(AccessLevel.PROTECTED); constructor.selector = typeDeclaration.name; if (callBuilderBasedSuperConstructor) { constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.Super); constructor.constructorCall.arguments = new Expression[] { new SingleNameReference(BUILDER_VARIABLE_NAME, p) }; } else { constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper); } constructor.constructorCall.sourceStart = source.sourceStart; constructor.constructorCall.sourceEnd = source.sourceEnd; constructor.thrownExceptions = null; constructor.typeParameters = null; constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart; constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd; TypeReference[] wildcards = new TypeReference[] { new Wildcard(Wildcard.UNBOUND), new Wildcard(Wildcard.UNBOUND) }; TypeReference builderType = new ParameterizedSingleTypeReference(builderClassName.toCharArray(), mergeToTypeReferences(typeParams, wildcards), 0, p); constructor.arguments = new Argument[] { new Argument(BUILDER_VARIABLE_NAME, p, builderType, Modifier.FINAL) }; List<Statement> statements = new ArrayList<Statement>(); for (BuilderFieldData fieldNode : builderFields) { char[] fieldName = removePrefixFromField(fieldNode.originalFieldNode); FieldReference fieldInThis = new FieldReference(fieldNode.rawName, p); int s = (int) (p >> 32); int e = (int) p; fieldInThis.receiver = new ThisReference(s, e); Expression assignmentExpr; if (fieldNode.singularData != null && fieldNode.singularData.getSingularizer() != null) { fieldNode.singularData.getSingularizer().appendBuildCode(fieldNode.singularData, typeNode, statements, fieldNode.name, BUILDER_VARIABLE_NAME_STRING); assignmentExpr = new SingleNameReference(fieldNode.name, p); } else { char[][] variableInBuilder = new char[][] { BUILDER_VARIABLE_NAME, fieldName }; long[] positions = new long[] { p, p }; assignmentExpr = new QualifiedNameReference(variableInBuilder, positions, s, e); } Statement assignment = new Assignment(fieldInThis, assignmentExpr, (int) p); // In case of @Builder.Default, set the value to the default if it was NOT set in the builder. if (fieldNode.nameOfSetFlag != null) { char[][] setVariableInBuilder = new char[][] { BUILDER_VARIABLE_NAME, fieldNode.nameOfSetFlag }; long[] positions = new long[] { p, p }; QualifiedNameReference setVariableInBuilderRef = new QualifiedNameReference(setVariableInBuilder, positions, s, e); MessageSend defaultMethodCall = new MessageSend(); defaultMethodCall.sourceStart = source.sourceStart; defaultMethodCall.sourceEnd = source.sourceEnd; defaultMethodCall.receiver = new SingleNameReference(((TypeDeclaration) typeNode.get()).name, 0L); defaultMethodCall.selector = fieldNode.nameOfDefaultProvider; defaultMethodCall.typeArguments = typeParameterNames( ((TypeDeclaration) typeNode.get()).typeParameters); Statement defaultAssignment = new Assignment(fieldInThis, defaultMethodCall, (int) p); IfStatement ifBlockForDefault = new IfStatement(setVariableInBuilderRef, assignment, defaultAssignment, s, e); statements.add(ifBlockForDefault); } else { statements.add(assignment); } if (hasNonNullAnnotations(fieldNode.originalFieldNode)) { Statement nullCheck = generateNullCheck((FieldDeclaration) fieldNode.originalFieldNode.get(), sourceNode); if (nullCheck != null) statements.add(nullCheck); } } constructor.statements = statements.isEmpty() ? null : statements.toArray(new Statement[0]); constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope); injectMethod(typeNode, constructor); }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void recoverStatements() { class MethodVisitor extends ASTVisitor { public ASTVisitor typeVisitor; TypeDeclaration enclosingType; // used only for initializer TypeDeclaration[] types = new TypeDeclaration[0]; int typePtr = -1; public void endVisit(ConstructorDeclaration constructorDeclaration, ClassScope scope) { endVisitMethod(constructorDeclaration, scope); }/*from w w w. j a v a 2 s. c om*/ public void endVisit(Initializer initializer, MethodScope scope) { if (initializer.block == null) return; TypeDeclaration[] foundTypes = null; int length = 0; if (this.typePtr > -1) { length = this.typePtr + 1; foundTypes = new TypeDeclaration[length]; System.arraycopy(this.types, 0, foundTypes, 0, length); } ReferenceContext oldContext = Parser.this.referenceContext; Parser.this.recoveryScanner.resetTo(initializer.bodyStart, initializer.bodyEnd); Scanner oldScanner = Parser.this.scanner; Parser.this.scanner = Parser.this.recoveryScanner; parseStatements(this.enclosingType, initializer.bodyStart, initializer.bodyEnd, foundTypes, Parser.this.compilationUnit); Parser.this.scanner = oldScanner; Parser.this.referenceContext = oldContext; for (int i = 0; i < length; i++) { foundTypes[i].traverse(this.typeVisitor, scope); } } public void endVisit(MethodDeclaration methodDeclaration, ClassScope scope) { endVisitMethod(methodDeclaration, scope); } private void endVisitMethod(AbstractMethodDeclaration methodDeclaration, ClassScope scope) { TypeDeclaration[] foundTypes = null; int length = 0; if (this.typePtr > -1) { length = this.typePtr + 1; foundTypes = new TypeDeclaration[length]; System.arraycopy(this.types, 0, foundTypes, 0, length); } ReferenceContext oldContext = Parser.this.referenceContext; Parser.this.recoveryScanner.resetTo(methodDeclaration.bodyStart, methodDeclaration.bodyEnd); Scanner oldScanner = Parser.this.scanner; Parser.this.scanner = Parser.this.recoveryScanner; parseStatements(methodDeclaration, methodDeclaration.bodyStart, methodDeclaration.bodyEnd, foundTypes, Parser.this.compilationUnit); Parser.this.scanner = oldScanner; Parser.this.referenceContext = oldContext; for (int i = 0; i < length; i++) { foundTypes[i].traverse(this.typeVisitor, scope); } } public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) { this.typePtr = -1; return true; } public boolean visit(Initializer initializer, MethodScope scope) { this.typePtr = -1; if (initializer.block == null) return false; return true; } public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) { this.typePtr = -1; return true; } private boolean visit(TypeDeclaration typeDeclaration) { if (this.types.length <= ++this.typePtr) { int length = this.typePtr; System.arraycopy(this.types, 0, this.types = new TypeDeclaration[length * 2 + 1], 0, length); } this.types[this.typePtr] = typeDeclaration; return false; } public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) { return this.visit(typeDeclaration); } public boolean visit(TypeDeclaration typeDeclaration, ClassScope scope) { return this.visit(typeDeclaration); } } class TypeVisitor extends ASTVisitor { public MethodVisitor methodVisitor; TypeDeclaration[] types = new TypeDeclaration[0]; int typePtr = -1; public void endVisit(TypeDeclaration typeDeclaration, BlockScope scope) { endVisitType(); } public void endVisit(TypeDeclaration typeDeclaration, ClassScope scope) { endVisitType(); } private void endVisitType() { this.typePtr--; } public boolean visit(ConstructorDeclaration constructorDeclaration, ClassScope scope) { if (constructorDeclaration.isDefaultConstructor()) return false; constructorDeclaration.traverse(this.methodVisitor, scope); return false; } public boolean visit(Initializer initializer, MethodScope scope) { if (initializer.block == null) return false; this.methodVisitor.enclosingType = this.types[this.typePtr]; initializer.traverse(this.methodVisitor, scope); return false; } public boolean visit(MethodDeclaration methodDeclaration, ClassScope scope) { methodDeclaration.traverse(this.methodVisitor, scope); return false; } private boolean visit(TypeDeclaration typeDeclaration) { if (this.types.length <= ++this.typePtr) { int length = this.typePtr; System.arraycopy(this.types, 0, this.types = new TypeDeclaration[length * 2 + 1], 0, length); } this.types[this.typePtr] = typeDeclaration; return true; } public boolean visit(TypeDeclaration typeDeclaration, BlockScope scope) { return this.visit(typeDeclaration); } public boolean visit(TypeDeclaration typeDeclaration, ClassScope scope) { return this.visit(typeDeclaration); } } MethodVisitor methodVisitor = new MethodVisitor(); TypeVisitor typeVisitor = new TypeVisitor(); methodVisitor.typeVisitor = typeVisitor; typeVisitor.methodVisitor = methodVisitor; if (this.referenceContext instanceof AbstractMethodDeclaration) { ((AbstractMethodDeclaration) this.referenceContext).traverse(methodVisitor, (ClassScope) null); } else if (this.referenceContext instanceof TypeDeclaration) { TypeDeclaration typeContext = (TypeDeclaration) this.referenceContext; int length = typeContext.fields.length; for (int i = 0; i < length; i++) { final FieldDeclaration fieldDeclaration = typeContext.fields[i]; switch (fieldDeclaration.getKind()) { case AbstractVariableDeclaration.INITIALIZER: Initializer initializer = (Initializer) fieldDeclaration; if (initializer.block == null) break; methodVisitor.enclosingType = typeContext; initializer.traverse(methodVisitor, (MethodScope) null); break; } } } }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.BaseAllocationExpression.java
License:Open Source License
private boolean isArgOfOtherCtor(ConstructorDeclaration constructorDecl, BlockScope scope) { // two marker exception types: @SuppressWarnings("serial") class FoundException extends RuntimeException { /*empty*/} @SuppressWarnings("serial") class NotFoundException extends RuntimeException { /*empty*/ } try {/* w w w. j av a 2s . c o m*/ constructorDecl.traverse(new ASTVisitor() { int inCtorCall = 0; @Override public boolean visit(ExplicitConstructorCall ctorCall, BlockScope aScope) { this.inCtorCall++; return super.visit(ctorCall, aScope); } @Override public void endVisit(ExplicitConstructorCall explicitConstructor, BlockScope aScope) { super.endVisit(explicitConstructor, aScope); this.inCtorCall--; } @Override public boolean visit(Assignment assig, BlockScope aScope) { if (assig == BaseAllocationExpression.this) { if (this.inCtorCall > 0) throw new FoundException(); else throw new NotFoundException(); } return super.visit(assig, aScope); } }, scope.classScope()); } catch (FoundException fe) { return true; } catch (NotFoundException nfe) { return false; } return false; }