Example usage for org.eclipse.jdt.internal.compiler.lookup LookupEnvironment createParameterizedType

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

Introduction

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

Prototype

public ParameterizedTypeBinding createParameterizedType(ReferenceBinding genericType,
            TypeBinding[] typeArguments, ReferenceBinding enclosingType) 

Source Link

Usage

From source file:com.redhat.ceylon.eclipse.core.model.loader.JDTUtils.java

License:Open Source License

public static ReferenceBinding inferTypeParametersFromSuperClass(ReferenceBinding declaringClass,
        ReferenceBinding superClass, LookupEnvironment lookupEnvironment) {
    if (superClass instanceof RawTypeBinding && declaringClass instanceof ParameterizedTypeBinding) {
        ParameterizedTypeBinding rawSuperType = (ParameterizedTypeBinding) superClass;
        ParameterizedTypeBinding declaringType = (ParameterizedTypeBinding) declaringClass;
        superClass = lookupEnvironment.createParameterizedType(rawSuperType.genericType(),
                declaringType.arguments, rawSuperType.enclosingType());
    }//  ww w .j av a  2 s .  c  o m
    return superClass;
}

From source file:com.redhat.ceylon.eclipse.core.model.mirror.JDTUtils.java

License:Open Source License

public static ReferenceBinding inferTypeParametersFromSuperClass(ReferenceBinding declaringClass,
        ReferenceBinding superClass) {//ww  w  .  j  a  va2  s  . c  om
    if (superClass instanceof RawTypeBinding && declaringClass instanceof ParameterizedTypeBinding) {
        LookupEnvironment lookupEnvironment = superClass.getPackage().environment;
        ParameterizedTypeBinding rawSuperType = (ParameterizedTypeBinding) superClass;
        ParameterizedTypeBinding declaringType = (ParameterizedTypeBinding) declaringClass;
        superClass = lookupEnvironment.createParameterizedType(rawSuperType.genericType(),
                declaringType.arguments, rawSuperType.enclosingType());
    }
    return superClass;
}

From source file:org.eclipse.jdt.internal.compiler.lookup.Scope.java

License:Open Source License

public MethodBinding getStaticFactory(ReferenceBinding allocationType, ReferenceBinding originalEnclosingType,
        TypeBinding[] argumentTypes, final InvocationSite allocationSite) {
    TypeVariableBinding[] classTypeVariables = allocationType.typeVariables();
    int classTypeVariablesArity = classTypeVariables.length;
    MethodBinding[] methods = allocationType.getMethods(TypeConstants.INIT, argumentTypes.length);
    MethodBinding[] staticFactories = new MethodBinding[methods.length];
    int sfi = 0;//  ww w .j  a v a 2  s  .  co  m
    for (int i = 0, length = methods.length; i < length; i++) {
        MethodBinding method = methods[i];
        int paramLength = method.parameters.length;
        boolean isVarArgs = method.isVarargs();
        if (argumentTypes.length != paramLength)
            if (!isVarArgs || argumentTypes.length < paramLength - 1)
                continue; // incompatible
        TypeVariableBinding[] methodTypeVariables = method.typeVariables();
        int methodTypeVariablesArity = methodTypeVariables.length;

        MethodBinding staticFactory = new MethodBinding(method.modifiers | ClassFileConstants.AccStatic,
                TypeConstants.SYNTHETIC_STATIC_FACTORY, null, null, null, method.declaringClass);
        staticFactory.typeVariables = new TypeVariableBinding[classTypeVariablesArity
                + methodTypeVariablesArity];
        final SimpleLookupTable map = new SimpleLookupTable(classTypeVariablesArity + methodTypeVariablesArity);
        // Rename each type variable T of the type to T'
        final LookupEnvironment environment = environment();
        for (int j = 0; j < classTypeVariablesArity; j++) {
            map.put(classTypeVariables[j],
                    staticFactory.typeVariables[j] = new TypeVariableBinding(
                            CharOperation.concat(classTypeVariables[j].sourceName, "'".toCharArray()), //$NON-NLS-1$
                            staticFactory, j, environment));
        }
        // Rename each type variable U of method U to U''.
        for (int j = classTypeVariablesArity, max = classTypeVariablesArity
                + methodTypeVariablesArity; j < max; j++) {
            map.put(methodTypeVariables[j - classTypeVariablesArity],
                    (staticFactory.typeVariables[j] = new TypeVariableBinding(
                            CharOperation.concat(methodTypeVariables[j - classTypeVariablesArity].sourceName,
                                    "''".toCharArray()), //$NON-NLS-1$
                            staticFactory, j, environment)));
        }
        ReferenceBinding enclosingType = originalEnclosingType;
        while (enclosingType != null) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=345968
            if (enclosingType.kind() == Binding.PARAMETERIZED_TYPE) {
                final ParameterizedTypeBinding parameterizedType = (ParameterizedTypeBinding) enclosingType;
                final ReferenceBinding genericType = parameterizedType.genericType();
                TypeVariableBinding[] enclosingClassTypeVariables = genericType.typeVariables();
                int enclosingClassTypeVariablesArity = enclosingClassTypeVariables.length;
                for (int j = 0; j < enclosingClassTypeVariablesArity; j++) {
                    map.put(enclosingClassTypeVariables[j], parameterizedType.arguments[j]);
                }
            }
            enclosingType = enclosingType.enclosingType();
        }
        final Scope scope = this;
        Substitution substitution = new Substitution() {
            public LookupEnvironment environment() {
                return scope.environment();
            }

            public boolean isRawSubstitution() {
                return false;
            }

            public TypeBinding substitute(TypeVariableBinding typeVariable) {
                TypeBinding retVal = (TypeBinding) map.get(typeVariable);
                return retVal != null ? retVal : typeVariable;
            }
        };

        // initialize new variable bounds
        for (int j = 0, max = classTypeVariablesArity + methodTypeVariablesArity; j < max; j++) {
            TypeVariableBinding originalVariable = j < classTypeVariablesArity ? classTypeVariables[j]
                    : methodTypeVariables[j - classTypeVariablesArity];
            TypeBinding substitutedType = (TypeBinding) map.get(originalVariable);
            if (substitutedType instanceof TypeVariableBinding) {
                TypeVariableBinding substitutedVariable = (TypeVariableBinding) substitutedType;
                TypeBinding substitutedSuperclass = Scope.substitute(substitution, originalVariable.superclass);
                ReferenceBinding[] substitutedInterfaces = Scope.substitute(substitution,
                        originalVariable.superInterfaces);
                if (originalVariable.firstBound != null) {
                    substitutedVariable.firstBound = originalVariable.firstBound == originalVariable.superclass
                            ? substitutedSuperclass // could be array type or interface
                            : substitutedInterfaces[0];
                }
                switch (substitutedSuperclass.kind()) {
                case Binding.ARRAY_TYPE:
                    substitutedVariable.superclass = environment.getResolvedType(TypeConstants.JAVA_LANG_OBJECT,
                            null);
                    substitutedVariable.superInterfaces = substitutedInterfaces;
                    break;
                default:
                    if (substitutedSuperclass.isInterface()) {
                        substitutedVariable.superclass = environment
                                .getResolvedType(TypeConstants.JAVA_LANG_OBJECT, null);
                        int interfaceCount = substitutedInterfaces.length;
                        System.arraycopy(substitutedInterfaces, 0,
                                substitutedInterfaces = new ReferenceBinding[interfaceCount + 1], 1,
                                interfaceCount);
                        substitutedInterfaces[0] = (ReferenceBinding) substitutedSuperclass;
                        substitutedVariable.superInterfaces = substitutedInterfaces;
                    } else {
                        substitutedVariable.superclass = (ReferenceBinding) substitutedSuperclass; // typeVar was extending other typeVar which got substituted with interface
                        substitutedVariable.superInterfaces = substitutedInterfaces;
                    }
                }
            }
        }
        TypeVariableBinding[] returnTypeParameters = new TypeVariableBinding[classTypeVariablesArity];
        for (int j = 0; j < classTypeVariablesArity; j++) {
            returnTypeParameters[j] = (TypeVariableBinding) map.get(classTypeVariables[j]);
        }
        staticFactory.returnType = environment.createParameterizedType(allocationType, returnTypeParameters,
                allocationType.enclosingType());
        staticFactory.parameters = Scope.substitute(substitution, method.parameters);
        staticFactory.thrownExceptions = Scope.substitute(substitution, method.thrownExceptions);
        if (staticFactory.thrownExceptions == null) {
            staticFactory.thrownExceptions = Binding.NO_EXCEPTIONS;
        }
        staticFactories[sfi++] = new ParameterizedMethodBinding(
                (ParameterizedTypeBinding) environment.convertToParameterizedType(staticFactory.declaringClass),
                staticFactory);
    }
    if (sfi == 0)
        return null;
    if (sfi != methods.length) {
        System.arraycopy(staticFactories, 0, staticFactories = new MethodBinding[sfi], 0, sfi);
    }
    MethodBinding[] compatible = new MethodBinding[sfi];
    int compatibleIndex = 0;
    for (int i = 0; i < sfi; i++) {
        MethodBinding compatibleMethod = computeCompatibleMethod(staticFactories[i], argumentTypes,
                allocationSite);
        if (compatibleMethod != null) {
            if (compatibleMethod.isValidBinding())
                compatible[compatibleIndex++] = compatibleMethod;
        }
    }

    if (compatibleIndex == 0) {
        return null;
    }
    MethodBinding[] visible = new MethodBinding[compatibleIndex];
    int visibleIndex = 0;
    for (int i = 0; i < compatibleIndex; i++) {
        MethodBinding method = compatible[i];
        if (method.canBeSeenBy(allocationSite, this))
            visible[visibleIndex++] = method;
    }
    if (visibleIndex == 0) {
        return null;
    }
    return visibleIndex == 1 ? visible[0]
            : mostSpecificMethodBinding(visible, visibleIndex, argumentTypes, allocationSite, allocationType);
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.model.RoleModel.java

License:Open Source License

/**
  * Once team and tsuper role are known, record this information.
  *//*from  w  ww . ja v a  2s .  c  om*/
public void connect(TeamModel teamModel, ReferenceBinding tsuperRole) {
    setTeamModel(teamModel);
    ReferenceBinding superTeamBinding = teamModel.getBinding().superclass();
    if (superTeamBinding instanceof ParameterizedTypeBinding
            && !(tsuperRole instanceof ParameterizedTypeBinding)) {
        LookupEnvironment env = Config.getLookupEnvironment();
        if (env != null) {
            tsuperRole = env.createParameterizedType(tsuperRole, null, superTeamBinding);
            RoleModel.setTagBit(tsuperRole, RoleModel.IsViewedAsTSuper);
        }
    }
    boolean tsuperAlreadyPresent = false;
    for (int i = 0; i < this.numTSuperRoles; i++) {
        if (TypeBinding.equalsEquals(this._tsuperRoleBindings[i], tsuperRole)) {
            tsuperAlreadyPresent = true;
            break;
        }
    }
    if (!tsuperAlreadyPresent) {
        if (this.numTSuperRoles == this._tsuperRoleBindings.length)
            System.arraycopy(this._tsuperRoleBindings, 0,
                    this._tsuperRoleBindings = new ReferenceBinding[2 * this.numTSuperRoles], 0,
                    this.numTSuperRoles);
        this._tsuperRoleBindings[this.numTSuperRoles++] = tsuperRole;
        if (getAst() != null && getAst().isInterface())
            TypeLevel.addImplicitInheritance(getAst(), tsuperRole);
        WordValueAttribute.addClassFlags(this, IOTConstants.OT_CLASS_FLAG_HAS_TSUPER);
        if (this._binding != null)
            this._binding.modifiers |= AccOverriding;
    }
    // invoked from copy role we have at least this state:
    this._state.inititalize(ITranslationStates.STATE_ROLES_SPLIT);
}

From source file:org.eclipse.objectteams.otdt.internal.core.compiler.util.RoleTypeCreator.java

License:Open Source License

/** 
 * Decompose a given type into atomic types (taking into account arrays and type arguments)
 * perform a given type substitution for each atomic type and 
 * re-assemble to a type of the same shape as the given type. 
 * @param original      the given type to be substituted
 * @param environment   for creation of parameterized types and array types
 * @param substitution  what to perform on each atomic dependent type
 * @return either null (after an error should have been reported) or the original type or a legal substitution
 *//*from w ww.  j av  a 2s .  c  om*/
public static TypeBinding deepSubstitute(TypeBinding original, LookupEnvironment environment,
        IDependentTypeSubstitution substitution) {
    TypeBinding type = original;
    int dimensions = type.dimensions();
    type = type.leafComponentType();

    TypeBinding[] typeArguments = null;
    boolean hasInstantiated = false;
    if (type.isParameterizedType()) {
        ParameterizedTypeBinding ptb = (ParameterizedTypeBinding) type;
        if (ptb.arguments != null) {
            typeArguments = new TypeBinding[ptb.arguments.length];
            for (int j = 0; j < ptb.arguments.length; j++) {
                TypeBinding givenTypeArgument = ptb.arguments[j];
                typeArguments[j] = deepSubstitute(givenTypeArgument, environment, substitution);
                if (typeArguments[j] == null)
                    typeArguments[j] = givenTypeArgument;
                else if (typeArguments[j] != givenTypeArgument) //$IDENTITY-COMPARISON$
                    hasInstantiated = true;
            }
            if (hasInstantiated)
                type = ptb.genericType();
            else
                typeArguments = null;
        }
    }

    if (type instanceof DependentTypeBinding) {
        TypeBinding substituted = substitution.substitute((DependentTypeBinding) type, typeArguments,
                dimensions);
        if (substituted != type) // includes substituted == null //$IDENTITY-COMPARISON$
            return substituted;
    }
    if (hasInstantiated) {
        TypeBinding parameterized = environment.createParameterizedType((ReferenceBinding) type, typeArguments,
                original.enclosingType());
        if (dimensions == 0)
            return parameterized;
        return environment.createArrayType(parameterized, dimensions);
    }
    return original;
}