Example usage for org.eclipse.jdt.internal.compiler.ast SuperReference implicitSuperConstructorCall

List of usage examples for org.eclipse.jdt.internal.compiler.ast SuperReference implicitSuperConstructorCall

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast SuperReference implicitSuperConstructorCall.

Prototype

public static ExplicitConstructorCall implicitSuperConstructorCall() 

Source Link

Usage

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  a va 2 s  .co  m
    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.jdt.internal.compiler.parser.Parser.java

License:Open Source License

public void parse(ConstructorDeclaration cd, CompilationUnitDeclaration unit, boolean recordLineSeparator) {
    //only parse the method body of cd
    //fill out its statements

    //convert bugs into parse error

    boolean oldMethodRecoveryActivated = this.methodRecoveryActivated;
    if (this.options.performMethodsFullRecovery) {
        this.methodRecoveryActivated = true;
    }//from w  w w  . j a  v a 2s. c o  m

    initialize();
    goForBlockStatementsopt();
    if (recordLineSeparator) {
        this.scanner.recordLineSeparator = true;
    }
    this.nestedMethod[this.nestedType]++;
    pushOnRealBlockStack(0);

    this.referenceContext = cd;
    this.compilationUnit = unit;

    this.scanner.resetTo(cd.bodyStart, cd.bodyEnd);
    try {
        parse();
    } catch (AbortCompilation ex) {
        this.lastAct = ERROR_ACTION;
    } finally {
        this.nestedMethod[this.nestedType]--;
        if (this.options.performStatementsRecovery) {
            this.methodRecoveryActivated = oldMethodRecoveryActivated;
        }
    }

    checkNonNLSAfterBodyEnd(cd.declarationSourceEnd);

    if (this.lastAct == ERROR_ACTION) {
        cd.bits |= ASTNode.HasSyntaxErrors;
        initialize();
        return;
    }

    //statements
    cd.explicitDeclarations = this.realBlockStack[this.realBlockPtr--];
    int length;
    if (this.astLengthPtr > -1 && (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, cd.statements = new Statement[length - 1], 0,
                        length - 1);
                cd.constructorCall = (ExplicitConstructorCall) this.astStack[this.astPtr + 1];
            } else { //need to add explicitly the super();
                System.arraycopy(this.astStack, this.astPtr + 1, cd.statements = new Statement[length], 0,
                        length);
                cd.constructorCall = SuperReference.implicitSuperConstructorCall();
            }
        }
    } else {
        if (!this.options.ignoreMethodBodies) {
            cd.constructorCall = SuperReference.implicitSuperConstructorCall();
        }
        if (!containsComment(cd.bodyStart, cd.bodyEnd)) {
            cd.bits |= ASTNode.UndocumentedEmptyBlock;
        }
    }

    ExplicitConstructorCall explicitConstructorCall = cd.constructorCall;
    if (explicitConstructorCall != null && explicitConstructorCall.sourceEnd == 0) {
        explicitConstructorCall.sourceEnd = cd.sourceEnd;
        explicitConstructorCall.sourceStart = cd.sourceStart;
    }
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.copyinheritance.CopyInheritance.java

License:Open Source License

/**
 * Copy a given method to the tsub-role.
 * Adds the new method to AST and performs initial creation of bindings.
 * @param method  method to copy//from  w ww  .  jav a 2 s. co m
 * @param targetRoleDecl target role class
 */
private static void copyMethod(MethodBinding method, TypeDeclaration targetRoleDecl) {
    boolean wasSynthetic = false;
    ReferenceBinding site = null;

    if ((method.modifiers & AccSynthetic) != 0) {
        wasSynthetic = true;
        // some, but not all, synthetics shall be generated with strong signatures as indicated by 'site':
        if (!SyntheticBaseCallSurrogate.isBaseCallSurrogateName(method.selector))
            site = targetRoleDecl.binding;
        if (SyntheticRoleBridgeMethodBinding.isPrivateBridgeSelector(method.selector))
            return; // will be generated anew
    }

    if (TypeContainerMethod.isTypeContainer(method))
        return; // don't copy these dummy methods

    if (isCreator(method) || CharOperation.equals(IOTConstants._OT_GETBASE, method.selector)
            || CharOperation.prefixEquals(IOTConstants.CAST_PREFIX, method.selector)
            || CharOperation.prefixEquals(IOTConstants.GET_CLASS_PREFIX, method.selector))
        return; // create/getBase/cast/getClass-methods are generated anew in each team.
    // (can only happen in a team that is a role of an outer team.)
    // Note: we don't use AccSynthetic on creators, because
    // Synthetic methods are not read from byte code.
    // FIXME(SH): this last note is not true any more.

    if (targetRoleDecl.isTeam() && (ReflectionGenerator.isReflectionMethod(method)
            || SerializationGenerator.isSerializationMethod(method)))
        return;
    if (MethodModel.isFakedMethod(method))
        return; // will be generated anew (basecall-surrogate, rolefield-bridge)

    if (CharOperation.equals(IOTConstants.MIGRATE_TO_TEAM, method.selector))
        return; // can only be used from the exact team

    // avoid copying twice (see copyGeneratedFeatures()):
    targetRoleDecl.getRoleModel().recordCopiedFeature(method);

    // some useful objects:
    ReferenceBinding srcRole = method.declaringClass;
    TypeDeclaration targetTeamDecl = targetRoleDecl.enclosingType;
    ReferenceBinding srcTeam = TeamModel.getEnclosingTeam(srcRole);
    ReferenceBinding tgtTeam = targetTeamDecl.binding;

    MethodBinding origin = (method.copyInheritanceSrc != null) ? method.copyInheritanceSrc : method;

    AbstractMethodDeclaration methodFound = findMethod(srcTeam, method, tgtTeam, targetRoleDecl);

    if (method.isConstructor()) {
        if (CharOperation.equals(srcRole.compoundName, IOTConstants.ORG_OBJECTTEAMS_TEAM_OTCONFINED)) {
            // must add default constructor explicitly,
            // since if we would copy the one from Team.__OT__Confined,
            // it would invoke the wrong super().
            ConstructorDeclaration ctor = targetRoleDecl.createDefaultConstructor(true, true);
            targetRoleDecl.binding.resolveGeneratedMethod(ctor, false, origin);
            return;
        } else if (targetRoleDecl.getRoleModel()._refinesExtends) {
            // even if we can't copy the ctor, we need to mark
            // the current ctor as overriding the tsuper version.
            if (methodFound != null)
                methodFound.binding.addOverriddenTSuper(method);
            return; // extends is covariantly redefined, don't copy ctor!
            // attempts to invoke this tsuper ctor are caught in ExplicitConstructorCall.resolve().
        }
    }

    // If method already exists in subteam,
    // + adjust method in subteam to the more general signature (here)
    // + extend copy with marker arg (below).
    // else give an exact copy.
    boolean reuseFoundMethod = false;
    if (methodFound != null) {
        // do not touch broken methods:
        // (methodFound might have no binding,
        //  e.g., due to a missing import of return-type)
        if (methodFound.binding == null)
            return;

        if (methodFound.binding.copyInheritanceSrc == origin)
            return; // diamond inheritance: copying the same method from two different sources

        // tsuper method trumps previously inferred callout:
        if (methodFound.isMappingWrapper == WrapperKind.CALLOUT) {
            MethodModel model = methodFound.model;
            if (model != null && model._inferredCallout != null) {
                // reset callout related stuff:
                methodFound.isMappingWrapper = WrapperKind.NONE;
                model._inferredCallout = null;
                methodFound.statements = null;
                // setup as a copied method:
                methodFound.isCopied = true;
                methodFound.binding.copyInheritanceSrc = method;
                methodFound.sourceMethodBinding = method;
                reuseFoundMethod = true;
            }
        }

        // do this in any case so that ConstantPoolObjectMapper won't fail:
        methodFound.binding.addOverriddenTSuper(method);

        // abstract methods have no byte code to copy;
        // new method silently replaces abstract version:
        if (method.isAbstract()) {
            weakenSignature(methodFound, method);
            return;
        }

        if (method.isFinal()) {
            methodFound.scope.problemReporter().finalMethodCannotBeOverridden(methodFound.binding, method);
            return;
        }
        weakenSignature(methodFound, method);
        MethodBinding foundSrc = methodFound.binding.copyInheritanceSrc;
        if (foundSrc != null && foundSrc != origin) {
            // found a copied method which has a different origin, choose the more specific:
            // if incommensurable prefer the new copy (assuming it comes from *implicit* super team)
            if (!TeamModel.isMoreSpecificThan(foundSrc.declaringClass, origin.declaringClass)) {
                // more specific method overwrites previous linkage:
                methodFound.binding.setCopyInheritanceSrc(origin);
                methodFound.sourceMethodBinding = origin;
                methodFound.isTSuper = TSuperHelper.isTSuper(method);
                // TODO(SH): also update CopyInheritanceSrc-Attribute!
                if (!method.isAbstract()) {
                    // not abstract any more, joining abstract and implemented methods:
                    methodFound.modifiers &= ~(AccAbstract | AccSemicolonBody);
                    methodFound.binding.modifiers &= ~(AccAbstract | AccSemicolonBody);
                    // TODO(SH): might need multiple copyInheritanceSrc!
                    // TODO(SH) need to adjust copiedInContext?
                }
            }
            return;
        }
    }
    AstGenerator gen = new AstGenerator(targetRoleDecl.sourceStart, targetRoleDecl.sourceEnd);
    gen.replaceableEnclosingClass = tgtTeam;
    final AbstractMethodDeclaration newMethodDecl;
    if (methodFound != null && reuseFoundMethod) {
        newMethodDecl = methodFound;
    } else {
        newMethodDecl = AstConverter.createMethod(method, site, targetRoleDecl.compilationResult,
                DecapsulationState.REPORTED, gen);

        if (methodFound != null)
            TSuperHelper.addMarkerArg(newMethodDecl, srcTeam);

        if (newMethodDecl.isConstructor()) {
            // comments (SH):
            // other phases may depend on this field (constructorCall) being set,
            // although it carries no real information.
            ConstructorDeclaration cd = (ConstructorDeclaration) newMethodDecl;
            cd.constructorCall = SuperReference.implicitSuperConstructorCall();

            if (Lifting.isLiftingCtor(method) && method.parameters[0].isRole()) {
                // if baseclass is implicitely redefined use the strong type:
                ReferenceBinding newBase = targetRoleDecl.binding.baseclass();
                if (TypeBinding.notEquals(newBase, method.parameters[0]))
                    newMethodDecl.arguments[0].type = gen.baseclassReference(newBase);
            }
        }

        AstEdit.addMethod(targetRoleDecl, newMethodDecl, wasSynthetic, false/*addToFront*/, origin);
    }
    if (method.isPrivate()) {
        newMethodDecl.binding.modifiers |= ExtraCompilerModifiers.AccLocallyUsed; // don't warn unused copied method
    }

    newMethodDecl.binding.copiedInContext = tgtTeam.enclosingType();
    MethodModel newModel = MethodModel.getModel(newMethodDecl);
    newModel.addAttribute(CopyInheritanceSourceAttribute.copyInherSrcAttribute(origin, newModel));
    if (wasSynthetic)
        targetRoleDecl.getRoleModel().addSyntheticMethodMapping(method, newMethodDecl.binding);

    if (method.isAnyCallin() && !method.isCallin()) // callin wrapper
        newMethodDecl.isMappingWrapper = WrapperKind.CALLIN;

    if (methodFound == null) {
        // copy down some more properties:
        if (TSuperHelper.isTSuper(method))
            newMethodDecl.isTSuper = true;
        if (method.model != null && method.model.callinFlags != 0)
            MethodModel.addCallinFlag(newMethodDecl, method.model.callinFlags);
        if (method.isAnyCallin()) {
            TypeBinding inheritedSrcReturn = MethodModel.getReturnType(method);
            if (inheritedSrcReturn.isRole())
                inheritedSrcReturn = RoleTypeCreator.maybeWrapUnqualifiedRoleType(inheritedSrcReturn,
                        targetRoleDecl.binding);
            MethodModel.saveReturnType(newMethodDecl.binding, inheritedSrcReturn);
        } else {
            if (!method.isPublic() // non-public source-level class method?
                    && !method.isConstructor() && !targetRoleDecl.isInterface()
                    && !CharOperation.prefixEquals(IOTConstants.OT_DOLLAR_NAME, method.selector)) {
                MethodModel.getModel(newMethodDecl).storeModifiers(newMethodDecl.modifiers);
            }
        }
        // more checking: abstract method in non-abstract class?
        if (newMethodDecl.isAbstract() && (targetRoleDecl.modifiers & ClassFileConstants.AccAbstract) == 0) {
            targetRoleDecl.scope.problemReporter().setRechecker(new IProblemRechecker() {
                public boolean shouldBeReported(IrritantSet[] foundIrritants) {
                    if (newMethodDecl.isAbstract()) // implemented by callout?
                        return true;
                    return false; // false alarm
                }
            }).abstractMethodMustBeImplemented(targetRoleDecl.binding, newMethodDecl.binding);
        }
    }
}