List of usage examples for org.eclipse.jdt.internal.compiler.ast ExplicitConstructorCall isImplicitSuper
public boolean isImplicitSuper()
From source file:com.android.tools.lint.psi.EcjPsiBuilder.java
License:Apache License
@NonNull private EcjPsiCodeBlock toBlock(@NonNull EcjPsiSourceElement parent, @Nullable Statement[] statements, @Nullable ExplicitConstructorCall constructorCall, int startOffset, int endOffset) { EcjPsiCodeBlock block = new EcjPsiCodeBlock(mManager); if (statements != null) { List<PsiStatement> psiStatements = Lists.newArrayListWithExpectedSize(statements.length + 1); if (constructorCall != null && !constructorCall.isImplicitSuper()) { // Not part of the normal statement list in ECJ psiStatements.add(toExplicitConstructorCall(block, constructorCall)); }// ww w .j ava 2 s.c o m for (Statement statement : statements) { EcjPsiStatement psiStatement = toStatement(block, statement); psiStatements.add(psiStatement); } block.setStatements(psiStatements.toArray(PsiStatement.EMPTY_ARRAY)); } else if (constructorCall != null && !constructorCall.isImplicitSuper()) { block.setStatements(new PsiStatement[] { toExplicitConstructorCall(block, constructorCall) }); } else { block.setStatements(PsiStatement.EMPTY_ARRAY); } parent.adoptChild(block); if (startOffset != -1) { block.setRange(startOffset, endOffset); } return block; }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.ConstructorLocator.java
License:Open Source License
protected void matchReportReference(ASTNode reference, IJavaElement element, Binding elementBinding, int accuracy, MatchLocator locator) throws CoreException { MethodBinding constructorBinding = null; boolean isSynthetic = false; if (reference instanceof ExplicitConstructorCall) { ExplicitConstructorCall call = (ExplicitConstructorCall) reference; isSynthetic = call.isImplicitSuper(); constructorBinding = call.binding; } else if (reference instanceof AllocationExpression) { AllocationExpression alloc = (AllocationExpression) reference; constructorBinding = alloc.binding; } else if (reference instanceof TypeDeclaration || reference instanceof FieldDeclaration) { super.matchReportReference(reference, element, elementBinding, accuracy, locator); if (this.match != null) return; }/* w ww . j a v a 2s .c o m*/ // Create search match this.match = locator.newMethodReferenceMatch(element, elementBinding, accuracy, -1, -1, true, isSynthetic, reference); // Look to refine accuracy if (constructorBinding instanceof ParameterizedGenericMethodBinding) { // parameterized generic method // Update match regarding constructor type arguments ParameterizedGenericMethodBinding parameterizedMethodBinding = (ParameterizedGenericMethodBinding) constructorBinding; this.match.setRaw(parameterizedMethodBinding.isRaw); TypeBinding[] typeBindings = parameterizedMethodBinding.isRaw ? null : parameterizedMethodBinding.typeArguments; updateMatch(typeBindings, locator, this.pattern.constructorArguments, this.pattern.hasConstructorParameters()); // Update match regarding declaring class type arguments if (constructorBinding.declaringClass.isParameterizedType() || constructorBinding.declaringClass.isRawType()) { ParameterizedTypeBinding parameterizedBinding = (ParameterizedTypeBinding) constructorBinding.declaringClass; if (!this.pattern.hasTypeArguments() && this.pattern.hasConstructorArguments() || parameterizedBinding.isParameterizedWithOwnVariables()) { // special case for constructor pattern which defines arguments but no type // in this case, we only use refined accuracy for constructor } else if (this.pattern.hasTypeArguments() && !this.pattern.hasConstructorArguments()) { // special case for constructor pattern which defines no constructor arguments but has type ones // in this case, we do not use refined accuracy updateMatch(parameterizedBinding, this.pattern.getTypeArguments(), this.pattern.hasTypeParameters(), 0, locator); } else { updateMatch(parameterizedBinding, this.pattern.getTypeArguments(), this.pattern.hasTypeParameters(), 0, locator); } } else if (this.pattern.hasTypeArguments()) { this.match.setRule(SearchPattern.R_ERASURE_MATCH); } // Update match regarding constructor parameters // TODO ? (frederic) } else if (constructorBinding instanceof ParameterizedMethodBinding) { // Update match regarding declaring class type arguments if (constructorBinding.declaringClass.isParameterizedType() || constructorBinding.declaringClass.isRawType()) { ParameterizedTypeBinding parameterizedBinding = (ParameterizedTypeBinding) constructorBinding.declaringClass; if (!this.pattern.hasTypeArguments() && this.pattern.hasConstructorArguments()) { // special case for constructor pattern which defines arguments but no type updateMatch(parameterizedBinding, new char[][][] { this.pattern.constructorArguments }, this.pattern.hasTypeParameters(), 0, locator); } else if (!parameterizedBinding.isParameterizedWithOwnVariables()) { updateMatch(parameterizedBinding, this.pattern.getTypeArguments(), this.pattern.hasTypeParameters(), 0, locator); } } else if (this.pattern.hasTypeArguments()) { this.match.setRule(SearchPattern.R_ERASURE_MATCH); } // Update match regarding constructor parameters // TODO ? (frederic) } else if (this.pattern.hasConstructorArguments()) { // binding has no type params, compatible erasure if pattern does this.match.setRule(SearchPattern.R_ERASURE_MATCH); } // See whether it is necessary to report or not if (this.match.getRule() == 0) return; // impossible match boolean report = (this.isErasureMatch && this.match.isErasure()) || (this.isEquivalentMatch && this.match.isEquivalent()) || this.match.isExact(); if (!report) return; // Report match int offset = reference.sourceStart; this.match.setOffset(offset); this.match.setLength(reference.sourceEnd - offset + 1); if (reference instanceof FieldDeclaration) { // enum declaration FieldDeclaration enumConstant = (FieldDeclaration) reference; if (enumConstant.initialization instanceof QualifiedAllocationExpression) { locator.reportAccurateEnumConstructorReference(this.match, enumConstant, (QualifiedAllocationExpression) enumConstant.initialization); return; } } locator.report(this.match); }
From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.ConstructorLocator.java
License:Open Source License
public SearchMatch newDeclarationMatch(ASTNode reference, IJavaElement element, Binding binding, int accuracy, int length, MatchLocator locator) { this.match = null; int offset = reference.sourceStart; if (this.pattern.findReferences) { if (reference instanceof TypeDeclaration) { TypeDeclaration type = (TypeDeclaration) reference; AbstractMethodDeclaration[] methods = type.methods; if (methods != null) { for (int i = 0, max = methods.length; i < max; i++) { AbstractMethodDeclaration method = methods[i]; boolean synthetic = method.isDefaultConstructor() && method.sourceStart < type.bodyStart; this.match = locator.newMethodReferenceMatch(element, binding, accuracy, offset, length, method.isConstructor(), synthetic, method); }//from www . jav a2 s.co m } } else if (reference instanceof ConstructorDeclaration) { ConstructorDeclaration constructor = (ConstructorDeclaration) reference; ExplicitConstructorCall call = constructor.constructorCall; boolean synthetic = call != null && call.isImplicitSuper(); this.match = locator.newMethodReferenceMatch(element, binding, accuracy, offset, length, constructor.isConstructor(), synthetic, constructor); } } if (this.match != null) { return this.match; } // super implementation... return locator.newDeclarationMatch(element, binding, accuracy, reference.sourceStart, length); }
From source file:lombok.eclipse.handlers.HandleSneakyThrows.java
License:Open Source License
public void handleMethod(EclipseNode annotation, AbstractMethodDeclaration method, List<DeclaredException> exceptions) { if (method.isAbstract()) { annotation.addError("@SneakyThrows can only be used on concrete methods."); return;// w w w. java 2 s. com } if (method.statements == null || method.statements.length == 0) { boolean hasConstructorCall = false; if (method instanceof ConstructorDeclaration) { ExplicitConstructorCall constructorCall = ((ConstructorDeclaration) method).constructorCall; hasConstructorCall = constructorCall != null && !constructorCall.isImplicitSuper() && !constructorCall.isImplicitThis(); } if (hasConstructorCall) { annotation.addWarning( "Calls to sibling / super constructors are always excluded from @SneakyThrows; @SneakyThrows has been ignored because there is no other code in this constructor."); } else { annotation.addWarning("This method or constructor is empty; @SneakyThrows has been ignored."); } return; } Statement[] contents = method.statements; for (DeclaredException exception : exceptions) { contents = new Statement[] { buildTryCatchBlock(contents, exception, exception.node, method) }; } method.statements = contents; annotation.up().rebuild(); }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void consumeConstructorDeclaration() { // ConstructorDeclaration ::= ConstructorHeader ConstructorBody /*/*from w w w . j av a2 s . com*/ this.astStack : MethodDeclaration statements this.identifierStack : name ==> this.astStack : MethodDeclaration this.identifierStack : */ //must provide a default constructor call when needed int length; // pop the position of the { (body of the method) pushed in block decl this.intPtr--; this.intPtr--; //statements this.realBlockPtr--; ExplicitConstructorCall constructorCall = null; Statement[] statements = null; if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) { this.astPtr -= length; if (!this.options.ignoreMethodBodies) { if (this.astStack[this.astPtr + 1] instanceof ExplicitConstructorCall) { //avoid a isSomeThing that would only be used here BUT what is faster between two alternatives ? System.arraycopy(this.astStack, this.astPtr + 2, statements = new Statement[length - 1], 0, length - 1); constructorCall = (ExplicitConstructorCall) this.astStack[this.astPtr + 1]; } else { //need to add explicitly the super(); System.arraycopy(this.astStack, this.astPtr + 1, statements = new Statement[length], 0, length); constructorCall = SuperReference.implicitSuperConstructorCall(); } } } else { boolean insideFieldInitializer = false; if (this.diet) { for (int i = this.nestedType; i > 0; i--) { if (this.variablesCounter[i] > 0) { insideFieldInitializer = true; break; } } } if (!this.diet || insideFieldInitializer) { // add it only in non-diet mode, if diet_bodies, then constructor call will be added elsewhere. constructorCall = SuperReference.implicitSuperConstructorCall(); } } // now we know that the top of stack is a constructorDeclaration ConstructorDeclaration cd = (ConstructorDeclaration) this.astStack[this.astPtr]; cd.constructorCall = constructorCall; cd.statements = statements; //highlight of the implicit call on the method name if (constructorCall != null && cd.constructorCall.sourceEnd == 0) { cd.constructorCall.sourceEnd = cd.sourceEnd; cd.constructorCall.sourceStart = cd.sourceStart; } if (!(this.diet && this.dietInt == 0) && statements == null && (constructorCall == null || constructorCall.isImplicitSuper()) && !containsComment(cd.bodyStart, this.endPosition)) { cd.bits |= ASTNode.UndocumentedEmptyBlock; } //watch for } that could be given as a unicode ! ( u007D is '}' ) // store the this.endPosition (position just before the '}') in case there is // a trailing comment behind the end of the method cd.bodyEnd = this.endPosition; cd.declarationSourceEnd = flushCommentsDefinedPriorTo(this.endStatementPosition); }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.BaseAllocationExpression.java
License:Open Source License
/** * Initialize the type to create from the "playedBy" clause. * @param scope non-null/*from w w w.j av a2 s . c om*/ */ private void createAst(BlockScope scope) { if (this.isAstCreated) return; // already done. this.isAstCreated = true; // even if creation fails, don't try again. ReferenceBinding enclType; AbstractMethodDeclaration enclMethodDecl; ReferenceBinding baseclass = null; enclType = scope.enclosingSourceType(); enclMethodDecl = (AbstractMethodDeclaration) scope.methodScope().referenceContext; if (enclType.isDirectRole()) baseclass = ((MemberTypeBinding) enclType).baseclass(); if (baseclass == null || !enclMethodDecl.isConstructor()) { scope.problemReporter().baseConstructorCallInWrongMethod(this, scope.methodScope().referenceContext); return; } ConstructorDeclaration enclCtor = (ConstructorDeclaration) enclMethodDecl; if (this.isExpression) { if (!isArgOfOtherCtor(enclCtor, scope)) scope.problemReporter().baseConstructorExpressionOutsideCtorCall(this); } else { if (enclCtor.statements[0] != this) scope.problemReporter().baseConstructorCallIsNotFirst(this); } AstGenerator gen = new AstGenerator(this.sourceStart, this.sourceEnd); Expression allocation; if (this.enclosingInstance != null) { this.enclosingInstance = new PotentialLowerExpression(this.enclosingInstance, baseclass.enclosingType()); // FIXME(SH): check baseclass.enclosingType(); } if (baseclass.isDirectRole()) { // instead of new B() create: // receiver._OT$createB(): Expression receiver; if (RoleTypeBinding.isRoleWithExplicitAnchor(baseclass)) { RoleTypeBinding baseRole = (RoleTypeBinding) baseclass; ITeamAnchor anchor = baseRole._teamAnchor; ReferenceBinding startClass = anchor.getFirstDeclaringClass(); char[][] tokens = anchor.tokens(); if (startClass != null) { // relevant start class, create as receiver: // EnclType.this.field1. TypeReference startReference = gen.typeReference(startClass); startReference.setBaseclassDecapsulation(DecapsulationState.ALLOWED); receiver = gen.qualifiedThisReference(startReference); for (int i = 0; i < tokens.length; i++) { receiver = gen.fieldReference(receiver, tokens[i]); } } else { // the best name path defines the receiver: receiver = gen.qualifiedNameReference(tokens); } } else { if (this.enclosingInstance != null) { receiver = this.enclosingInstance; } else { if (TypeBinding.equalsEquals(baseclass.enclosingType(), enclType.enclosingType())) receiver = gen.thisReference(); // creating a role of the same team as base instance?? else receiver = gen.qualifiedThisReference(gen.typeReference(baseclass.enclosingType())); } } char[] selector = CharOperation.concat(CREATOR_PREFIX_NAME, baseclass.sourceName()); MessageSend allocSend = new MessageSend() { @Override public boolean isDecapsulationAllowed(Scope scope2) { // this message send can decapsulate independent of scope return true; } @Override public DecapsulationState getBaseclassDecapsulation() { return DecapsulationState.ALLOWED; } }; gen.setPositions(allocSend); allocSend.receiver = receiver; allocSend.selector = selector; allocSend.arguments = this.arguments; allocSend.accessId = -1; // request that MessageSend.resolveType() assigns a fresh accessId if decapsulation is detected allocation = allocSend; } else { AllocationExpression alloc = newAllocation(baseclass, gen); alloc.type.setBaseclassDecapsulation(DecapsulationState.ALLOWED); // report individually alloc.arguments = this.arguments; alloc.sourceStart = this.sourceStart; alloc.sourceEnd = this.sourceEnd; alloc.statementEnd = this.statementEnd; allocation = alloc; } this.arguments = null; // don't use any more. ExplicitConstructorCall selfcall = enclCtor.constructorCall; if (selfcall.isImplicitSuper() && enclType.superclass().isDirectRole() && enclType.superclass().baseclass() != null) { // implement 2.4.2(c): // transform "super(); base(args);" => "super(new MyBase(args)); nop;" enclCtor.constructorCall = genLiftCtorCall(allocation); enclCtor.statements[0] = new AstGenerator(this.sourceStart, this.sourceEnd).emptyStatement(); // pretend we are not calling base() because we already call the lifting-ctor. } else if (this.isExpression) { // similar to above: // translate "super(base(args), ...);" as "super(new MyBase(args), ...);" this.expression = allocation; // and ignore the assignment flavor of this node. } else { // needed by ASTConverter: this.expression = allocation; if (!enclType.roleModel.hasBaseclassProblem() && !scope.referenceType().ignoreFurtherInvestigation) { MethodModel.setCallsBaseCtor(enclCtor); // really creating base here, need to register this base object RoleModel boundRootRoleModel = enclType.roleModel.getBoundRootRole(); if (boundRootRoleModel == null) throw new InternalCompilerError( "Unexpected: role has neither baseclassProblem nor boundRootRole"); //$NON-NLS-1$ Statement[] regStats = Lifting.genRoleRegistrationStatements(scope, boundRootRoleModel, baseclass, enclCtor, gen); int len = enclCtor.statements.length; Statement[] newStats = new Statement[len + regStats.length]; newStats[0] = this; System.arraycopy(regStats, 0, newStats, 1, regStats.length); System.arraycopy(enclCtor.statements, 1, newStats, regStats.length + 1, len - 1); enclCtor.setStatements(newStats); } } }
From source file:spoon.support.builder.JDTTreeBuilder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w ww.j a va 2 s . c om public boolean visit(ExplicitConstructorCall explicitConstructor, BlockScope scope) { CtInvocation inv = factory.Core().createInvocation(); if (explicitConstructor.isImplicitSuper()) { inv.setImplicit(true); } inv.setExecutable(references.getExecutableReference(explicitConstructor.binding)); inv.getExecutable().setType(inv.getExecutable().getDeclaringType()); inv.setType(inv.getExecutable().getType()); context.enter(inv, explicitConstructor); if (explicitConstructor.qualification != null) { explicitConstructor.qualification.traverse(this, scope); } if (explicitConstructor.typeArguments != null) { for (int i = 0, typeArgumentsLength = explicitConstructor.typeArguments.length; i < typeArgumentsLength; i++) { explicitConstructor.typeArguments[i].traverse(this, scope); } } context.arguments.push(inv); if (explicitConstructor.arguments != null) { for (int i = 0, argumentLength = explicitConstructor.arguments.length; i < argumentLength; i++) explicitConstructor.arguments[i].traverse(this, scope); } context.arguments.pop(); return false; }
From source file:spoon.support.compiler.jdt.JDTTreeBuilder.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w w w . java2 s. c o m public boolean visit(ExplicitConstructorCall explicitConstructor, BlockScope scope) { CtInvocation<Object> inv = factory.Core().createInvocation(); if (explicitConstructor.isImplicitSuper()) { inv.setImplicit(true); } CtExecutableReference<Object> er = references.getExecutableReference(explicitConstructor.binding); inv.setExecutable(er); inv.getExecutable().setType((CtTypeReference<Object>) inv.getExecutable().getDeclaringType()); inv.setType(inv.getExecutable().getType()); if (explicitConstructor.genericTypeArguments() != null) { inv.getExecutable().setActualTypeArguments( references.getBoundedTypesReferences(explicitConstructor.genericTypeArguments())); } context.enter(inv, explicitConstructor); if (explicitConstructor.qualification != null) { explicitConstructor.qualification.traverse(this, scope); } if (explicitConstructor.typeArguments != null) { for (int i = 0, typeArgumentsLength = explicitConstructor.typeArguments.length; i < typeArgumentsLength; i++) { explicitConstructor.typeArguments[i].traverse(this, scope); } } context.arguments.push(inv); if (explicitConstructor.arguments != null) { for (int i = 0, argumentLength = explicitConstructor.arguments.length; i < argumentLength; i++) { explicitConstructor.arguments[i].traverse(this, scope); } } context.arguments.pop(); return false; }