List of usage examples for org.eclipse.jdt.internal.compiler.ast AbstractMethodDeclaration resolve
public void resolve(ClassScope upperScope)
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 . j a va2 s.c o m*/ * 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); } }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.transformer.SerializationGenerator.java
License:Open Source License
/** * Fill in the statement of a previously generated restoreRole() method. * @param teamType type declaration assumeably holding an empty restoreRole method. * @param caches all declarations of role caches of this team type. */// w w w. j a v a2s . c o m public static void fillRestoreRole(TypeDeclaration teamType, FieldDeclaration[] caches) { AbstractMethodDeclaration restoreMethod = TypeAnalyzer.findMethodDecl(teamType, RESTORE_ROLE, 2); if (restoreMethod == null) { return; } boolean superIsTeam = teamType.binding.superclass.isTeam(); AstGenerator gen = new AstGenerator(restoreMethod); // re-use position Statement[] statements = new Statement[caches.length + (superIsTeam ? 1 : 0)]; // find the matching cache for argument Class clazz: 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 (!cacheTypeRef.resolvedType.isParameterizedType() || ((ParameterizedTypeBinding) cacheTypeRef.resolvedType).arguments.length != 2) { if (teamType.scope.environment().globalOptions.complianceLevel < ClassFileConstants.JDK1_5) { restoreMethod.statements = new Statement[] { gen.emptyStatement() }; restoreMethod.tagAsHavingErrors(); return; // incompatible compliance level, assume errors have been reported. } throw new InternalCompilerError("Unexpected resolved cache type " + cacheTypeRef.resolvedType); //$NON-NLS-1$ } // reconstruct a type reference from the resolved cache type ParameterizedTypeBinding oldBinding = (ParameterizedTypeBinding) cacheTypeRef.resolvedType; 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( // if (Role.class.isAssignableFrom(clazz)) { ... gen.messageSend(gen.classLiteralAccess(gen.typeReference(roleBinding)), IS_ASSIGNABLE_FROM, new Expression[] { gen.singleNameReference(CLASS_ARG_NAME) }), gen.block(new Statement[] { // Role castedRole = (Role) role; gen.localVariable(CASTED_ROLE, roleBinding, gen.castExpression(gen.singleNameReference(ROLE_ARG_NAME), gen.typeReference(roleBinding), CastExpression.RAW)), // Base base = role._OT$getBase(); gen.localVariable(IOTConstants.BASE, gen.baseclassReference(roleBinding.baseclass(), true /*erase*/), gen.messageSend(gen.singleNameReference(CASTED_ROLE), IOTConstants._OT_GETBASE, null/*arguments*/)), // <roleCache[i]>.put(base, castedRole); gen.messageSend(gen.singleNameReference(caches[i].name), PUT, new Expression[] { gen.baseNameReference(IOTConstants.BASE), gen.singleNameReference(CASTED_ROLE) }), // ((IBoundBase)base)._OT$addRole(castedRole); // OTDYN: Slightly different methods depending on the weaving strategy: teamType.scope.compilerOptions().weavingScheme == WeavingScheme.OTDRE ? gen.messageSend( gen.castExpression(gen.singleNameReference(IOTConstants.BASE), gen.qualifiedTypeReference( IOTConstants.ORG_OBJECTTEAMS_IBOUNDBASE2), CastExpression.RAW), IOTConstants.ADD_REMOVE_ROLE, new Expression[] { gen.singleNameReference(CASTED_ROLE), gen.booleanLiteral(true) }) : gen.messageSend( gen.castExpression(gen.singleNameReference(IOTConstants.BASE), gen.qualifiedTypeReference( IOTConstants.ORG_OBJECTTEAMS_IBOUNDBASE), CastExpression.RAW), IOTConstants.ADD_ROLE, new Expression[] { gen.singleNameReference(CASTED_ROLE) }), // return; // don't consult further caches gen.returnStatement(null) })); } if (superIsTeam) { // if no suitable cache found so far: // super.restoreRole(clazz, role); statements[caches.length] = gen.messageSend(gen.superReference(), RESTORE_ROLE, new Expression[] { gen.singleNameReference(CLASS_ARG_NAME), gen.singleNameReference(ROLE_ARG_NAME) }); } restoreMethod.setStatements(statements); if (StateMemento.hasMethodResolveStarted(teamType.binding)) restoreMethod.resolve(teamType.scope); }