List of usage examples for org.eclipse.jdt.internal.compiler.ast ConstructorDeclaration isDefaultConstructor
@Override
public boolean isDefaultConstructor()
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); }/* w w w .j a va 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.statemachine.copyinheritance.CopyInheritance.java
License:Open Source License
/** * Generate creation methods for a given role constructor. * for concrete R://from ww w.j a va 2 s . co m * R _OT$createR (mySignature) { return new __OT__R(myArgs); } * for abstract R: * abstract R _OT$createR (mySignature); * If role is inherited from super-Team, repeat creation method with * same signature but creating an instance of the local role type. * * @param teamDeclaration the team class to hold the creation method * @param roleModel the role to instantiate * @param constructor use this as a template to create the creation method, may be null. * @param constructorBinding non-null representation of constructor * @param needMethodBody used to signal whether generating statements is required * @return the creation method. */ public static MethodDeclaration createCreationMethod(TypeDeclaration teamDeclaration, RoleModel roleModel, ConstructorDeclaration constructor, MethodBinding constructorBinding, boolean needMethodBody) { int start, end; int modifiers; boolean hasError = false; if (constructor != null) { if (constructor.isTSuper || constructor.hasErrors()) return null; if (constructor.isDefaultConstructor() && roleModel.hasBaseclassProblem()) hasError = true; start = constructor.sourceStart; end = constructor.sourceEnd; modifiers = constructor.modifiers; constructorBinding = constructor.binding; } else { if (TSuperHelper.isTSuper(constructorBinding)) return null; start = teamDeclaration.sourceStart; end = teamDeclaration.sourceEnd; modifiers = constructorBinding.modifiers; } int originalModifiers = -1; // creation method must access constructor, make it at least protected: if (constructorBinding != null && (constructorBinding.isDefault() || constructorBinding.isPrivate())) { originalModifiers = constructorBinding.modifiers; MethodModel.getModel(constructorBinding).storeModifiers(originalModifiers); } AstGenerator gen = new AstGenerator(start, end); gen.replaceableEnclosingClass = teamDeclaration.binding; Argument[] newArguments = null; // Arguments (construct from bindings, using dummy names): if (constructorBinding != null) { TypeBinding[] srcParams = constructorBinding.parameters; if (srcParams != null) { newArguments = AstConverter.createArgumentsFromParameters(srcParams, gen); if (srcParams.length == 1 && TypeBinding.equalsEquals(srcParams[0], roleModel.getInterfacePartBinding())) { // single argument of type of this role itself? if (constructorBinding.isPrivate() || constructorBinding.isDefault()) roleModel.getAst().scope.problemReporter() .roleConstructorHiddenByLiftingConstructor(constructor); } if (Lifting.isLiftingCtor(constructorBinding)) { gen.addNonNullAnnotation(newArguments[0], teamDeclaration.scope.environment()); } } } if (newArguments != null && constructorBinding != null && Lifting.isLiftingCtor(constructorBinding)) newArguments[0].type.setBaseclassDecapsulation(DecapsulationState.REPORTED); // if we have source arguments, improve: use correct argument names: if (newArguments != null && constructor != null) { Argument[] srcArguments = constructor.arguments; if (srcArguments != null && srcArguments.length == newArguments.length) for (int i = 0; i < srcArguments.length; i++) newArguments[i].name = srcArguments[i].name; } TypeReference[] exceptions = null; if (constructor != null) { if (constructor.thrownExceptions != null) exceptions = AstClone.copyTypeArray(constructor.thrownExceptions); } else if (constructorBinding != null) { if (constructorBinding.thrownExceptions != Binding.NO_EXCEPTIONS) exceptions = AstClone.copyExceptions(constructorBinding, gen); } else { throw new InternalCompilerError("Either constructor or constructorBinding must be nonnull"); //$NON-NLS-1$ } MethodDeclaration newMethod = internalCreateCreationMethod(teamDeclaration, roleModel, modifiers, newArguments, exceptions, needMethodBody && !hasError, start, end); if (hasError) newMethod.tagAsHavingErrors(); if (newMethod != null) { MethodModel model = MethodModel.getModel(newMethod); model._srcCtor = constructorBinding; if (originalModifiers != -1) model.storeModifiers(originalModifiers); } if (newMethod != null && constructor != null) // faked source locations (binaries have no source poss, but compiled constructor has no errors.) AstClone.copySrcLocation(constructor, newMethod); return newMethod; }