Example usage for org.eclipse.jdt.internal.compiler.ast TypeDeclaration compilationResult

List of usage examples for org.eclipse.jdt.internal.compiler.ast TypeDeclaration compilationResult

Introduction

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

Prototype

CompilationResult compilationResult

To view the source code for org.eclipse.jdt.internal.compiler.ast TypeDeclaration compilationResult.

Click Source Link

Usage

From source file:com.google.gwt.dev.javac.CompiledClass.java

License:Open Source License

private static ClassFile getClassFile(TypeDeclaration typeDecl, String binaryName) {
    for (ClassFile tryClassFile : typeDecl.compilationResult().getClassFiles()) {
        char[] tryBinaryName = CharOperation.concatWith(tryClassFile.getCompoundName(), '/');
        if (binaryName.equals(String.valueOf(tryBinaryName))) {
            return tryClassFile;
        }// w w  w.  ja v a 2  s.com
    }
    assert false;
    return null;
}

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

License:Open Source License

private MethodDeclaration generateTransformArrayMethod(TypeDeclaration teamType, char[] transformMethodName,
        int arrayDimensions) {
    AstGenerator gen = new AstGenerator(teamType.sourceStart, teamType.sourceEnd);

    MethodDeclaration transformArrayMethod = gen.method(teamType.compilationResult(), 0, // modifiers
            this._requiredType, transformMethodName,
            new Argument[] { gen.argument(ROLE_ARRAY_ARG, gen.typeReference(this._providedType)) });
    decapsulationInput(transformArrayMethod.arguments[0].type);
    decapsulationOutput(transformArrayMethod.returnType);

    // if(r1 == null)return null;
    IfStatement ifStatement = generateIfStatement(0, arrayDimensions);

    //  Base[][][] result = null;
    LocalDeclaration reqArrayDeclaration = gen.localVariable(IOTConstants.OT_RESULT, this._requiredType,
            gen.nullLiteral());/*from  ww w  .  j a  v a  2s  .co m*/
    decapsulationOutput(reqArrayDeclaration.type);

    //  result = new Base[role.length][][];
    Assignment arrayInstantiation = generateArrayInstantiation(0, arrayDimensions, gen);

    // for(...){...}
    ForStatement forStatement = generateForStatement(0, arrayDimensions, gen);

    // return result;
    ReturnStatement returnStatement = gen.returnStatement(gen.singleNameReference(IOTConstants.OT_RESULT));

    transformArrayMethod.setStatements(new Statement[] { ifStatement, reqArrayDeclaration, arrayInstantiation,
            forStatement, returnStatement });

    return transformArrayMethod;
}

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

License:Open Source License

/**
 * This method generates a lift method suitable for lifting an argument typed to a base-bounded type variable (B base R).
 * The generated method will be added to the team type and resolved if suitable (team already in state >= resolving).
 * @param teamDecl/*w  ww .  j a v a  2  s.  c o  m*/
 * @param ref
 * @param roleType
 * @param needMethodBody
 */
public static void genLiftDynamicMethod(TypeDeclaration teamDecl, ASTNode ref, TypeBinding roleType,
        boolean needMethodBody) {
    char[] dynamicLiftingSelector = dynamicLiftSelector(roleType);
    if (teamDecl.binding.getMethods(dynamicLiftingSelector) != Binding.NO_METHODS)
        return; // already present

    if (roleType.isArrayType()) {
        teamDecl.scope.problemReporter().missingImplementation(ref,
                "Generic lifting of array not yet implemented.");
    } else if (roleType.isBaseType()) {
        teamDecl.scope.problemReporter().primitiveTypeNotAllowedForLifting(teamDecl, ref, roleType);
    } else {
        // reference type
        AstGenerator gen = new AstGenerator(ref);
        MethodDeclaration dynLiftMeth = gen.method(teamDecl.compilationResult(),
                ClassFileConstants.AccProtected, roleType.erasure(), dynamicLiftingSelector,
                new Argument[] { gen.argument(IOTConstants.BASE,
                        gen.qualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT)) });

        dynLiftMeth.statements = new Statement[] { gen.emptyStatement() }; // to be replaced below
        int problemId = teamDecl.getTeamModel().canLiftingFail((ReferenceBinding) roleType.erasure());
        if (problemId == 0) {
            // look harder, in "<B base R>" the role R could be unbound
            // yet otherwise unrelated sub-roles could bind to the same base B'
            HashSet<ReferenceBinding> mappedBases = new HashSet<ReferenceBinding>();
            for (ReferenceBinding boundRole : ((ReferenceBinding) roleType).roleModel.getBoundDescendants())
                if (mappedBases.contains(boundRole.baseclass())) {
                    problemId = IProblem.CallinDespiteBindingAmbiguity;
                    break;
                } else {
                    mappedBases.add(boundRole.baseclass());
                }
        }

        if (problemId != 0)
            AstEdit.addException(dynLiftMeth,
                    gen.qualifiedTypeReference(IOTConstants.O_O_LIFTING_FAILED_EXCEPTION), false/*resolve*/);

        dynLiftMeth.hasParsedStatements = true;
        AstEdit.addMethod(teamDecl, dynLiftMeth);
        dynLiftMeth.binding.returnType = ((ReferenceBinding) roleType).getRealType().erasure(); // force erased type after signature resolve
        if (needMethodBody) {
            dynLiftMeth.statements[0] = DeclaredLifting.generateDynamicSwitch(dynLiftMeth.scope,
                    (ReferenceBinding) roleType, problemId, gen);
            if (StateMemento.hasMethodResolveStarted(teamDecl.binding))
                dynLiftMeth.statements[0].resolve(dynLiftMeth.scope);
        }
    }
}

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

License:Open Source License

/**
 * Retreive (or create) a team level method used for casting an expression to a role.
 * After casting also check the containingInstance against the current team.
 * @param teamModel/*w  w w .ja va 2s .co m*/
 * @param roleType
 * @param scope (used only for lookup of j.l.Object)
 * @param searchSuper  should super classes of teamClass be search, too?
 * @param sourceStart
 * @param sourceEnd
 * @return the method
 */
public static MethodBinding getCastMethod(TeamModel teamModel, ReferenceBinding roleType, Scope scope,
        int dimensions, boolean searchSuper, int sourceStart, int sourceEnd) {
    /*
     * protected <role> _OT$castTo$<role> (Object _OT$arg1) {
     *    if (_OT$arg1 == null) return null;
     *      __OT__<role> role = (__OT__<role>) _OT$arg1;
     *    if (role._OT$getTeam() != this)
     *         throw new RuntimeException();
     *    return role;
     * }
     *OR FOR ARRAY:
     * protected <role>[].. _OT$castTo$<role>$<dims> (Object[].. _OT$arg1) {
     *    if (_OT$arg1 == null) return null;
     *      <role>[].. role = (<role>[]..) _OT$arg1;
     *    if (role.length > 0 && ((__OT__<role>)role[0])._OT$getTeam() != this) // TODO(SH): extract through several dims
     *         throw new RuntimeException();
     *    return role;
     * }
     * NOTE(SH): it suffices to check team equivalence for one element, since at this point it
     *           must already be a role-array, which cannot mix roles from different teams ;-)
     */
    boolean shouldWeaken = (teamModel.getState() >= ITranslationStates.STATE_TYPES_ADJUSTED); // weakening for other methods already done?
    MethodBinding superMethod = null;

    roleType = roleType.getRealType();
    char[] methodName = CharOperation.concat(CAST_PREFIX, roleType.sourceName());
    if (dimensions > 0)
        methodName = CharOperation.concat(methodName, String.valueOf(dimensions).toCharArray(), '$');
    ReferenceBinding teamBinding = teamModel.getBinding();
    while (teamBinding != null) {
        MethodBinding[] methods = teamBinding.getMethods(methodName);
        if (methods != null && methods.length == 1) {
            if (TypeBinding.equalsEquals(methods[0].declaringClass, teamModel.getBinding()) || searchSuper)
                return methods[0];
            // go ahead and generate a new method, but use superMethod for weakening after generating:
            superMethod = methods[0];
            break;
        }
        if (!searchSuper && !shouldWeaken)
            break;
        teamBinding = teamBinding.superclass();
    }

    TypeDeclaration teamClass = teamModel.getAst();
    if (teamClass == null) {
        if (true) {// FIXME(SH): team has error?
            MethodBinding castMethod = new MethodBinding(AccPublic, methodName, roleType,
                    new TypeBinding[] { scope.getJavaLangObject() }, null, teamModel.getBinding());
            teamModel.getBinding().addMethod(castMethod);
            return castMethod;
        }
        throw new InternalCompilerError("Required cast method not found."); //$NON-NLS-1$
    }

    AstGenerator gen = new AstGenerator(sourceStart, sourceEnd);

    // --- method header ---
    int modifiers = 0;
    boolean clearPrivateModifier = false;
    if (roleType.isPublic()) {
        modifiers = AccPublic;
    } else {
        // this weird combination allows to return a non-public role and will
        // grant access across packages in the byte code.
        modifiers = AccProtected;
        clearPrivateModifier = true;
        // See also BinaryTypeBinding.resolveTypesFor(MethodBinding) where the Protected flag is restored.
    }
    // args
    char[] argName = OT_DOLLAR_ARG.toCharArray();

    // find the appropriate top-level-super-type:
    ReferenceBinding exprType = teamClass.scope.getJavaLangObject();
    //      if (!roleType.isStrictlyCompatibleWith(exprType)) {
    //         exprType = (ReferenceBinding)teamClass.scope.getType(ORG_OBJECTTEAMS_ICONFINED, 3);
    //         if (!roleType.isCompatibleWith(exprType))
    //            exprType = (ReferenceBinding)teamClass.scope.getType(
    //                  ORG_OBJECTTEAMS_TEAM_DOT_CONFINED,
    //                  4);
    //      }
    TypeReference exprTypeRef = gen.typeReference(exprType);

    MethodDeclaration castMethod = gen.method(teamClass.compilationResult(), modifiers,
            gen.createArrayTypeReference(roleType, dimensions), methodName,
            new Argument[] { gen.argument(argName, exprTypeRef) });
    // see org.eclipse.objectteams.otdt.tests.otjld.regression.ReportedBugs.testB11_sh15():
    // pre-set return type to prevent problems with resolving lateron
    TypeBinding returnType = dimensions == 0 ? roleType
            : scope.environment().createArrayType(roleType, dimensions);
    castMethod.returnType.resolvedType = RoleTypeCreator.maybeWrapUnqualifiedRoleType(returnType, teamBinding);

    // <role> role = (<role>)_OT$arg;
    TypeReference arrayCastType = gen.createArrayTypeReference(roleType, dimensions);
    LocalDeclaration castedLocalVar = gen.localVariable(ROLE, arrayCastType,
            gen.castExpression(gen.singleNameReference(argName), arrayCastType, CastExpression.RAW));

    //STATEMENTS:
    // if (_OT$arg1 == null) return null;
    //AND
    //   if (role._OT$getTeam() != this)
    //      throw new RuntimeException();
    //  OR
    //   if (role.length > 0 && ((<roleClass>)role[0])._OT$getTeam() != this)
    //      throw new RuntimeException();

    Statement nullCheck = gen.ifStatement(gen.nullCheck(gen.singleNameReference(argName)),
            gen.returnStatement(gen.nullLiteral()));

    Expression teamCheckCondition;
    teamCheckCondition = genTeamCheck(gen, OperatorIds.NOT_EQUAL, gen.singleNameReference(ROLE),
            gen.thisReference(), dimensions);

    if (dimensions > 0)
        teamCheckCondition = gen
                .setPos(new AND_AND_Expression(
                        gen.equalExpression(gen.qualifiedNameReference(new char[][] { ROLE, LENGTH }),
                                gen.intLiteral(0), OperatorIds.GREATER),
                        teamCheckCondition, OperatorIds.AND_AND));

    // here we go:
    castMethod.setStatements(new Statement[] { nullCheck, castedLocalVar, gen.ifStatement(teamCheckCondition,
            gen.throwStatement(
                    gen.allocation(gen.qualifiedTypeReference(ROLE_CAST_EXCEPTION), new Expression[0]))),
            // return role;
            gen.returnStatement(gen.singleNameReference(ROLE)) });
    castMethod.isGenerated = true;
    AstEdit.addGeneratedMethod(teamClass, castMethod);
    if (clearPrivateModifier)
        castMethod.binding.tagBits = TagBits.ClearPrivateModifier;

    if (superMethod != null)
        CopyInheritance.weakenSignature(castMethod, superMethod);
    return castMethod.binding;
}

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

License:Open Source License

@Override
public TypeBinding resolveType(ClassScope scope) {
    // `scope` may be stronger then `alienScope`, try it first:
    // (see 1.1.27-otjld-constructor-of-nested-team-2)
    TypeDeclaration referenceContext = scope.referenceContext;
    CheckPoint cp = null;// w  ww.  ja  va 2 s. c om
    if (referenceContext != null)
        cp = referenceContext.compilationResult().getCheckPoint(referenceContext);
    TypeBinding result = super.resolveType(scope);
    if (result != null && result.isValidBinding())
        return result;
    // remove problem binding if any:
    this.resolvedType = null;
    if (cp != null && referenceContext != null) // 2. check redundant via correlation
        referenceContext.compilationResult.rollBack(cp);
    return super.resolveType(this.alienScope.classScope());
}

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

License:Open Source License

@Override
public TypeBinding resolveType(ClassScope scope) {
    TypeDeclaration referenceContext = scope.referenceContext;
    CheckPoint cp = null;/*from w w  w .j  ava  2 s.  c om*/
    if (referenceContext != null)
        cp = referenceContext.compilationResult().getCheckPoint(referenceContext);
    TypeBinding result = super.resolveType(scope);
    if (result != null && result.isValidBinding())
        return result;
    // reset:
    this.resolvedType = null;
    if (cp != null && referenceContext != null) // 2. check redundant via correlation
        referenceContext.compilationResult.rollBack(cp);
    return super.resolveType(this.alienScope.classScope());
}