Example usage for org.eclipse.jdt.internal.compiler.ast FieldDeclaration resolve

List of usage examples for org.eclipse.jdt.internal.compiler.ast FieldDeclaration resolve

Introduction

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

Prototype

public void resolve(MethodScope initializationScope) 

Source Link

Usage

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.lifting.LiftingEnvironment.java

License:Open Source License

private static void fillInitCaches(TypeDeclaration teamType, FieldDeclaration[] caches) {
    /*/*from  w ww .ja v a2  s. com*/
     * generate:
     * {
     *       // for each cache declared in this team:
     *       if (_OT$cache<x> == null) {
     *          _OT$cache<c> = new WeakHashMap<Bx,Rx>();
     *      }
     *      // Note: no super call, super team's ctor is already responsible for invoking its (private) initCaches
     * }
     */
    AbstractMethodDeclaration initMethod = TypeAnalyzer.findMethodDecl(teamType, IOTConstants.OT_INIT_CACHES,
            0);
    if (initMethod == null) {
        // not yet generated? (maybe no bound roles/caches at that time).
        if (teamType.getTeamModel().caches.length == 0)
            return;
        initMethod = generateInitCaches(teamType);
    }
    AstGenerator gen = new AstGenerator(initMethod); // re-use position
    Statement[] statements = new Statement[caches.length + 1];
    for (int i = 0; i < caches.length; i++) {
        // FIXME(SH): unclear if needed after allowing generated qualified role type referneces:
        TypeReference cacheTypeRef = caches[i].type; // robustness, but with wrong source position
        if (caches[i].type.resolvedType instanceof ParameterizedTypeBinding) {
            // reconstruct a type reference from the resolved cache type
            ParameterizedTypeBinding oldBinding = (ParameterizedTypeBinding) cacheTypeRef.resolvedType;
            if (oldBinding.arguments.length == 2) {
                ReferenceBinding roleBinding = (ReferenceBinding) oldBinding.arguments[1];
                // respect different status for base/role types (scope, decapsulation).
                cacheTypeRef = gen.getCacheTypeReference(teamType.scope, roleBinding.roleModel);
            }
        }
        statements[i] = gen.ifStatement(
                new EqualExpression(gen.singleNameReference(caches[i].name), gen.nullLiteral(),
                        OperatorIds.EQUAL_EQUAL),
                gen.block(new Statement[] { gen.assignment(gen.singleNameReference(caches[i].name),
                        gen.allocation(cacheTypeRef, new Expression[0])) }));

    }
    statements[caches.length] = gen.returnStatement(gen.booleanLiteral(true));
    initMethod.setStatements(statements);
    // Serialization:
    SerializationGenerator.fillRestoreRole(teamType, caches);

    // ===== also add the field that triggers invocation of this method from all constructors: =====

    //save source positions from AstGenerator (ike)
    SourcePosition savePos = gen.getSourcePosition();
    try {
        //set STEP_OVER source positions (ike)
        gen.setSourcePosition(new StepOverSourcePosition());

        ThisReference thisReference = gen.thisReference();
        thisReference.resolvedType = teamType.binding.getRealClass(); // avoid wrapping of nested team
        thisReference.constant = Constant.NotAConstant;

        FieldDeclaration trigger = gen.field(ClassFileConstants.AccPrivate | ClassFileConstants.AccSynthetic,
                gen.singleTypeReference("boolean".toCharArray()), //$NON-NLS-1$
                IOTConstants.CACHE_INIT_TRIGGERER,
                gen.messageSend(thisReference, IOTConstants.OT_INIT_CACHES, new Expression[0]));
        AstEdit.addField(teamType, trigger, true, false/*typeProblem*/, true/*addToFront*/); // ensure this field's init is generated first into the constructor
        trigger.binding.modifiers |= ExtraCompilerModifiers.AccLocallyUsed; //  prevent 'unused' warning;

        // resolve them both:
        if (StateMemento.hasMethodResolveStarted(teamType.binding)) {
            initMethod.resolve(teamType.scope);
            trigger.resolve(teamType.initializerScope);
        }

    } finally {
        //restore source postions (ike)
        gen.setSourcePosition(savePos);
    }
}