Example usage for org.eclipse.jdt.internal.compiler.lookup FieldBinding FieldBinding

List of usage examples for org.eclipse.jdt.internal.compiler.lookup FieldBinding FieldBinding

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.lookup FieldBinding FieldBinding.

Prototype

public FieldBinding(char[] name, TypeBinding type, int modifiers, ReferenceBinding declaringClass,
            Constant constant) 

Source Link

Usage

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.OTQualifiedAllocationExpression.java

License:Open Source License

/**
* During resolve we make the decision which variant to use.
*//* w w w .j  ava2  s .c o m*/
public TypeBinding resolveType(BlockScope scope) {
    if (this.anonymousType == null && this.creatorCall == null && this.enclosingInstance == null) // special case during code assist
        return super.resolveType(scope);

    CompilationResult compilationResult = scope.referenceContext().compilationResult();
    CheckPoint cp = compilationResult.getCheckPoint(scope.referenceContext());
    this.hasEnclosingInstanceProblem = false;
    if (this.anonymousType == null && this.creatorCall == null) { // no double processing

        if (this.enclosingInstance instanceof CastExpression)
            this.enclosingInstance.bits |= DisableUnnecessaryCastCheck; // will check later on (within super.resolveType())

        TypeBinding enclosingInstanceType = this.enclosingInstance.resolveType(scope);
        this.hasEnclosingInstanceProblem = enclosingInstanceType == null;

        if (!scope.isGeneratedScope() && enclosingInstanceType != null && enclosingInstanceType.isTeam()) // non reference types will trigger error reporting via super.resolveType()
        {
            if (this.enclosingInstance instanceof NameReference) {
                final NameReference anchorRef = (NameReference) this.enclosingInstance;
                if (!((VariableBinding) anchorRef.binding).isFinal()) {

                    // replace non-final anchor with fake-binding,
                    // so that this type is not compatibly to anything else:
                    char[] variableName = ((VariableBinding) anchorRef.binding).name;
                    switch (anchorRef.bits & ASTNode.RestrictiveFlagMASK) {
                    case Binding.LOCAL:
                        final LocalVariableBinding localOrig = (LocalVariableBinding) anchorRef.binding;
                        // mark the original as used before we procede with a fake copy:
                        localOrig.useFlag = LocalVariableBinding.USED;
                        anchorRef.binding = new LocalVariableBinding(variableName, enclosingInstanceType,
                                ClassFileConstants.AccFinal, false) {
                            @Override
                            public int problemId() {
                                return IProblem.AnchorNotFinal;
                            }
                        };
                        this.preGenerateTask = new Runnable() {
                            public void run() {
                                // need to transfer this info from the real local to the fake one (don't have that info yet):
                                ((LocalVariableBinding) anchorRef.binding).resolvedPosition = localOrig.resolvedPosition;
                            }
                        };
                        break;
                    case Binding.FIELD:
                        anchorRef.binding = new FieldBinding(variableName, enclosingInstanceType,
                                ClassFileConstants.AccFinal, scope.referenceType().binding,
                                Constant.NotAConstant) {
                            @Override
                            public int problemId() {
                                return IProblem.AnchorNotFinal;
                            }
                        };
                        break;
                    default:
                        throw new InternalCompilerError("Unexpected bits, neither local nor field " //$NON-NLS-1$
                                + anchorRef.bits + ": " + anchorRef); //$NON-NLS-1$
                    }
                }
            }

            if (this.type.getTypeName().length > 1) {
                scope.problemReporter().roleCreationNotRelativeToEnclosingTeam(this);
                return null;
            }

            // now it's finally time to create the alternate version:
            this.creatorCall = CopyInheritance.createConstructorMethodInvocationExpression(scope, this);
            if (this.creatorCall == null)
                return null;
        }
    }
    if (this.creatorCall == null) {
        TypeBinding typ = super.resolveType(scope);
        if (typ == null || typ instanceof PolyTypeBinding)
            return typ;

        if (!this.hasEnclosingInstanceProblem) { // more checks only if no error already
            // if enclosing is a role request a cast to the class part as required by the inner constructor
            if (this.enclosingInstance != null) {
                TypeBinding enclosingType = this.enclosingInstance.resolvedType;
                if (enclosingType instanceof ReferenceBinding
                        && ((ReferenceBinding) enclosingType).isDirectRole())
                    this.enclosingInstanceCast = ((ReferenceBinding) enclosingType).getRealClass();
            }
            ReferenceBinding superType = null;
            if (this.resolvedType instanceof ReferenceBinding)
                superType = ((ReferenceBinding) this.resolvedType).superclass();
            if (superType != null && (superType instanceof RoleTypeBinding)) {
                RoleTypeBinding superRole = (RoleTypeBinding) superType;
                if (superRole.hasExplicitAnchor())
                    scope.problemReporter().extendingExternalizedRole(superRole, this.type);
            }
        }
    } else { // === with creatorCall ===

        this.constant = Constant.NotAConstant;

        this.resolvedType = this.creatorCall.resolveType(scope);
        // when creating role nested instance, no cast of enclosing role needed in this branch,
        // because creator call is routed via the interface of the enclosing role.
        if (this.resolvedType != null) {
            if (((ReferenceBinding) this.resolvedType).isAbstract()) {
                if (!((ReferenceBinding) enclosingInstance().resolvedType).isAbstract())
                    scope.problemReporter().abstractRoleIsRelevant(this,
                            (ReferenceBinding) this.creatorCall.resolvedType);
            }
            if (this.resolvedType.isValidBinding()) {
                // FIXME(SH): remove cast unwrapping
                Expression createExpr = this.creatorCall;
                while (createExpr instanceof CastExpression) // may have been wrapped using CollectedReplacementsTransformer
                    createExpr = ((CastExpression) createExpr).expression;

                this.binding = ((MessageSend) createExpr).binding; // store the method binding

                // using lift-ctor in a qualified way? (OTJDL 2.4.1(a))
                ReferenceBinding role = (ReferenceBinding) this.resolvedType;
                MethodBinding creator = this.binding;
                if (creator != null) {
                    MethodBinding ctor = role.getExactConstructor(creator.parameters);
                    if (Lifting.isLiftToConstructor(ctor, role))
                        scope.problemReporter().qualifiedUseOfLiftingConstructor(ctor, this.creatorCall);
                }
            }
        }
    }
    return this.resolvedType;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.ast.TypeValueParameter.java

License:Open Source License

/** Resolve this parameter to a (resolved) field binding. */
private FieldBinding resolveAsValueParameter(ClassScope scope) {
    this.type.resolveType(scope);
    this.fieldBinding = new FieldBinding(this.name, this.type.resolvedType,
            ClassFileConstants.AccFinal | ClassFileConstants.AccPublic | ClassFileConstants.AccValueParam,
            scope.enclosingSourceType(), Constant.NotAConstant);
    FieldModel.getModel(this.fieldBinding).addAttribute(new AnchorUsageRanksAttribute(this.fieldBinding));
    return this.fieldBinding;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.bytecode.ConstantPoolObjectReader.java

License:Open Source License

/** retrieve a class or array binding from the constant pool. */
private TypeBinding decodeClassEntry(int index) {
    int start = getConstantPoolStartPosition(index);
    assert (u1At(start) == ClassTag);
    int name_index = u2At(start + 1);
    char[] name_str = getUtf8(name_index);

    // first check scalar class type:
    int dims = 0;
    while (name_str[dims] == '[')
        dims++;//from ww  w  . jav a2  s  .  c o m
    if (dims == 0) {
        // no '[' prefix -> plain class name, must not have 'L' prefix nor ';' suffix:
        if (name_str[0] == 'L' || name_str[name_str.length - 1] == ';')
            throw new IncompatibleBytecodeException(name_str, 0);
        return getClassBinding(index);
    }

    // follows: decoding of array, leaf component type needs 'L' for reference bindings:
    TypeBinding typeBinding = this._environment.getTypeFromSignature(name_str, 0, -1, false,
            this._srcModel.getBinding(), null, TypeAnnotationWalker.EMPTY_ANNOTATION_WALKER);
    if (!typeBinding.leafComponentType().isBaseType()) {
        ReferenceBinding referenceBinding = (ReferenceBinding) typeBinding.leafComponentType();
        if (referenceBinding instanceof UnresolvedReferenceBinding) {
            ReferenceBinding localType = findLocalType(CharOperation.subarray(name_str, dims, -1));
            if (localType != null)
                referenceBinding = localType;
            else
                referenceBinding = (ReferenceBinding) BinaryTypeBinding.resolveType(referenceBinding,
                        this._environment, false);
        }
        // check whether this type is actually an anchored type:
        CPTypeAnchorAttribute typeAnchors = this._srcModel.getTypeAnchors();
        if (typeAnchors != null) {
            char[] anchorPath = typeAnchors.getPath(index);
            if (anchorPath != null) {
                // create a RTB using a dummy field:
                ReferenceBinding teamType = referenceBinding.enclosingType();
                FieldBinding anchor = new FieldBinding(anchorPath, teamType, AccFinal,
                        this._srcModel.getBinding(), Constant.NotAConstant);
                return anchor.getRoleTypeBinding(referenceBinding, dims);
            }
        }
    }
    return typeBinding;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.bytecode.ConstantPoolObjectReader.java

License:Open Source License

private ReferenceBinding getClassBinding(int index) {
    int start = getConstantPoolStartPosition(index);
    assert (u1At(start) == ClassTag);
    int name_index = u2At(start + 1);
    char[] name_str = getUtf8(name_index);
    ReferenceBinding referenceBinding = this._environment.getTypeFromConstantPoolName(name_str, 0, -1, false,
            null);/*  w w w. ja v a2 s  .c o m*/
    if (referenceBinding instanceof UnresolvedReferenceBinding) {
        ReferenceBinding localType = findLocalType(name_str);
        if (localType != null)
            return localType;
        referenceBinding = (ReferenceBinding) BinaryTypeBinding.resolveType(referenceBinding, this._environment,
                false);
    }
    // check whether this type is actually an anchored type:
    CPTypeAnchorAttribute typeAnchors = this._srcModel.getTypeAnchors();
    if (typeAnchors != null) {
        char[] anchorPath = typeAnchors.getPath(index);
        if (anchorPath != null) {
            // create a RTB using a dummy field:
            ReferenceBinding teamType = referenceBinding.enclosingType();
            FieldBinding anchor = new FieldBinding(anchorPath, teamType, AccFinal, this._srcModel.getBinding(),
                    Constant.NotAConstant);
            referenceBinding = (ReferenceBinding) anchor.getRoleTypeBinding(referenceBinding, 0);
        }
    }
    return referenceBinding;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.bytecode.ConstantPoolObjectReader.java

License:Open Source License

public static FieldBinding findFieldByBinding(ReferenceBinding clazz, FieldBinding refField) {
    char[] thisPrefix = TypeConstants.SYNTHETIC_ENCLOSING_INSTANCE_PREFIX;
    char[] classPrefix = TypeConstants.SYNTHETIC_CLASS;
    char[] name = refField.name;
    if (CharOperation.prefixEquals(thisPrefix, name)) {
        int distance = refField.declaringClass.depth() - ((ReferenceBinding) refField.type).depth();
        if (clazz instanceof NestedTypeBinding) {
            {//w  w  w .  java 2 s  .c om
                NestedTypeBinding mtb = (NestedTypeBinding) clazz;
                ReferenceBinding dstFieldType = clazz;
                while (distance-- > 0) {
                    dstFieldType = dstFieldType.enclosingType();
                }
                return mtb.addSyntheticFieldForInnerclass(dstFieldType);
            }
        } else { // BinaryTypeBinding
            int depth = clazz.depth() - distance;
            name = CharOperation.concat(thisPrefix, String.valueOf(depth).toCharArray());
        }
    } else if (CharOperation.prefixEquals(classPrefix, name)) {
        // assume the synthetic field is present, just create a new binding.
        return new FieldBinding(refField.name, refField.type, 0, clazz, null);
    }
    // directly find field in clazz or superclass(es)
    ReferenceBinding current = clazz;
    while (current != null) {
        FieldBinding f = current.getField(name, true);
        if (f != null)
            return f;
        current = current.superclass();
    }
    return null;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.lookup.TeamAnchor.java

License:Open Source License

/**
 * Try to interpret existingAnchor relative to receiver,
 * composing a new anchor path from receiver and existingAnchor.
 * @param site           type where resolving takes places,
 *                    an implicit this of this type can always be assumed as an implicit anchor.
  *                       FIXME(SH): make sure we always have that instance (static scopes??)
 * @param existingAnchor//w w  w  .  j a v a 2 s.  c  o m
 * @param receiver
 *
 * @return a team anchor or null
 */
public static ITeamAnchor maybeImproveAnchor(ReferenceBinding site, ITeamAnchor existingAnchor,
        Expression receiver) {
    TeamAnchor receiverVar = null;
    ITeamAnchor fallback = null;
    if (receiver instanceof CastExpression)
        receiver = ((CastExpression) receiver).expression;
    if (receiver instanceof NameReference) {
        Binding bind = ((NameReference) receiver).binding;
        if (bind instanceof VariableBinding)
            receiverVar = (VariableBinding) bind;
    } else if (receiver instanceof AllocationExpression) {
        // need to record that this anchor is unique (not compatible to any other anchor).
        fallback = receiverVar = new FieldBinding(
                ("'non-final anchor " + receiver.toString() + '\'').toCharArray(), //$NON-NLS-1$
                receiver.resolvedType, // type
                ClassFileConstants.AccFinal, site, // declaring class
                Constant.NotAConstant) {
            @Override
            public int problemId() {
                return IProblem.AnchorNotFinal;
            }
        };
    } else {
        TypeBinding receiverType = receiver.resolvedType;
        if (receiverType != null && RoleTypeBinding.isRoleWithExplicitAnchor(receiverType)) {
            receiverVar = (TeamAnchor) ((IRoleTypeBinding) receiverType).getAnchor();
        }
    }
    if (receiverVar != null) {
        ITeamAnchor[] prefix = existingAnchor.getBestNamePath();
        ITeamAnchor previous = null;
        if (prefix != null && prefix.length > 0)
            previous = prefix[prefix.length - 1];
        if (previous instanceof FieldBinding) {
            if (DependentTypeBinding.isDependentTypeOf(receiverVar.type, previous)) {
                DependentTypeBinding depReceiver = (DependentTypeBinding) receiverVar.type;
                return existingAnchor.replaceFirst(depReceiver._teamAnchor);
            }
            FieldBinding previousField = (FieldBinding) previous;
            ReferenceBinding currentType = null;
            boolean firstImplicitlyReachable = false;
            if (receiverVar instanceof FieldBinding)
                currentType = ((FieldBinding) receiverVar).declaringClass;
            else if (receiverVar instanceof LocalVariableBinding)
                currentType = ((LocalVariableBinding) receiverVar).declaringScope.enclosingSourceType();
            while (currentType != null) {
                if (currentType.isCompatibleWith(previousField.declaringClass)) {
                    firstImplicitlyReachable = true;
                    break;
                    // reachable without navigating receiverVar
                }
                currentType = currentType.enclosingType();
            }
            currentType = receiverVar.leafReferenceType();
            while (currentType != null) {
                if (currentType.isCompatibleWith(previousField.declaringClass)) {
                    if (firstImplicitlyReachable) {
                        if (fallback != null)
                            return fallback; // if we detected non-finalness above, now is the time to report this back
                    } else {
                        return existingAnchor.setPathPrefix(receiverVar);
                    }
                }
                currentType = currentType.enclosingType();
                fallback = null; // don't use any more after we have traveled out
            }
            if (firstImplicitlyReachable)
                return existingAnchor; // didn't improve but improvement wasn't necessary
        }
        return null;
    }
    // did not improve
    return null;
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.StandardElementGenerator.java

License:Open Source License

private static void checkCreateFakedStrongBaseField(ReferenceBinding superRole, ReferenceBinding roleClass,
        ReferenceBinding baseTypeBinding) {
    if (TypeBinding.equalsEquals(superRole.baseclass, baseTypeBinding))
        return; // not strengthening
    ReferenceBinding nextSuper = superRole.superclass();
    while (nextSuper.isRole() && nextSuper.roleModel.isBound()) {
        superRole = nextSuper;//from   w w  w .java  2s  .  c  o  m
        nextSuper = nextSuper.superclass();
    }
    // create faked base field with exact type information:
    FieldBinding fakeStrongBaseField = new FieldBinding(_OT_BASE, baseTypeBinding,
            AccPublic | AccFinal | AccSynthetic, roleClass, Constant.NotAConstant);
    fakeStrongBaseField.tagBits |= TagBits.IsFakedField;
    SourceTypeBinding roleSourceClass = (SourceTypeBinding) roleClass;
    FieldModel model = FieldModel.getModel(fakeStrongBaseField);
    model.actualDeclaringClass = superRole;
    FieldBinding superBaseField = superRole.getField(IOTConstants._OT_BASE, false);
    if (superBaseField != null)
        fakeStrongBaseField.shareBestName(superBaseField);
    roleSourceClass.addField(fakeStrongBaseField);
}