List of usage examples for org.eclipse.jdt.internal.compiler.ast SwitchStatement SwitchStatement
SwitchStatement
From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java
License:Open Source License
@Override public ASTNode visitSwitch(final lombok.ast.Switch node, final Void p) { final SwitchStatement switchStatement = new SwitchStatement(); switchStatement.expression = build(node.getExpression()); List<Statement> caseStatements = new ArrayList<Statement>(); for (lombok.ast.Case caze : node.getCases()) { final CaseStatement caseStatement = new CaseStatement( caze.getPattern() == null ? null : build(caze.getPattern(), Expression.class), 0, 0); setGeneratedByAndCopyPos(caseStatement, source, posHintOf(node)); caseStatements.add(caseStatement); caseStatements.addAll(build(caze.getStatements(), Statement.class)); }// w ww. ja v a 2 s .c o m switchStatement.statements = caseStatements.toArray(new Statement[caseStatements.size()]); return switchStatement; }
From source file:lombok.eclipse.handlers.singulars.EclipseJavaUtilListSingularizer.java
License:Open Source License
@Override public void appendBuildCode(SingularData data, EclipseNode builderType, List<Statement> statements, char[] targetVariableName) { if (useGuavaInstead(builderType)) { guavaListSetSingularizer.appendBuildCode(data, builderType, statements, targetVariableName); return;/*from w ww . j a va 2 s .c o m*/ } List<Statement> switchContents = new ArrayList<Statement>(); /* case 0: (empty) break; */ { switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '0' }, null), 0, 0)); MessageSend invoke = new MessageSend(); invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0); invoke.selector = "emptyList".toCharArray(); switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0)); switchContents.add(new BreakStatement(null, 0, 0)); } /* case 1: (singleton) break; */ { switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '1' }, null), 0, 0)); FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L); thisDotField.receiver = new ThisReference(0, 0); MessageSend thisDotFieldGet0 = new MessageSend(); thisDotFieldGet0.receiver = thisDotField; thisDotFieldGet0.selector = new char[] { 'g', 'e', 't' }; thisDotFieldGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) }; Expression[] args = new Expression[] { thisDotFieldGet0 }; MessageSend invoke = new MessageSend(); invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0); invoke.selector = "singletonList".toCharArray(); invoke.arguments = args; switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0)); switchContents.add(new BreakStatement(null, 0, 0)); } /* default: Create by passing builder field to constructor. */ { switchContents.add(new CaseStatement(null, 0, 0)); Expression argToUnmodifiable; /* new j.u.ArrayList<Generics>(this.pluralName); */ { FieldReference thisDotPluralName = new FieldReference(data.getPluralName(), 0L); thisDotPluralName.receiver = new ThisReference(0, 0); TypeReference targetTypeExpr = new QualifiedTypeReference(JAVA_UTIL_ARRAYLIST, NULL_POSS); targetTypeExpr = addTypeArgs(1, false, builderType, targetTypeExpr, data.getTypeArgs()); AllocationExpression constructorCall = new AllocationExpression(); constructorCall.type = targetTypeExpr; constructorCall.arguments = new Expression[] { thisDotPluralName }; argToUnmodifiable = constructorCall; } /* pluralname = Collections.unmodifiableList(-newlist-); */ { MessageSend unmodInvoke = new MessageSend(); unmodInvoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0); unmodInvoke.selector = "unmodifiableList".toCharArray(); unmodInvoke.arguments = new Expression[] { argToUnmodifiable }; switchContents .add(new Assignment(new SingleNameReference(data.getPluralName(), 0), unmodInvoke, 0)); } } SwitchStatement switchStat = new SwitchStatement(); switchStat.statements = switchContents.toArray(new Statement[switchContents.size()]); switchStat.expression = getSize(builderType, data.getPluralName(), true); TypeReference localShadowerType = new QualifiedTypeReference(Eclipse.fromQualifiedName(data.getTargetFqn()), NULL_POSS); localShadowerType = addTypeArgs(1, false, builderType, localShadowerType, data.getTypeArgs()); LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0); varDefStat.type = localShadowerType; statements.add(varDefStat); statements.add(switchStat); }
From source file:lombok.eclipse.handlers.singulars.EclipseJavaUtilSingularizer.java
License:Open Source License
protected List<Statement> createJavaUtilSetMapInitialCapacitySwitchStatements(SingularData data, EclipseNode builderType, boolean mapMode, String emptyCollectionMethod, String singletonCollectionMethod, String targetType) { List<Statement> switchContents = new ArrayList<Statement>(); char[] keyName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName(); if (emptyCollectionMethod != null) { // case 0: (empty); break; switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '0' }, null), 0, 0)); /* pluralName = java.util.Collections.emptyCollectionMethod(); */ { MessageSend invoke = new MessageSend(); invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0); invoke.selector = emptyCollectionMethod.toCharArray(); switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0)); }/*from w w w . java2 s.co m*/ switchContents.add(new BreakStatement(null, 0, 0)); } if (singletonCollectionMethod != null) { // case 1: (singleton); break; switchContents.add(new CaseStatement(makeIntLiteral(new char[] { '1' }, null), 0, 0)); /* !mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName.get(0)); mapMode: pluralName = java.util.Collections.singletonCollectionMethod(this.pluralName$key.get(0), this.pluralName$value.get(0)); */ { FieldReference thisDotKey = new FieldReference(keyName, 0L); thisDotKey.receiver = new ThisReference(0, 0); MessageSend thisDotKeyGet0 = new MessageSend(); thisDotKeyGet0.receiver = thisDotKey; thisDotKeyGet0.selector = new char[] { 'g', 'e', 't' }; thisDotKeyGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) }; Expression[] args; if (mapMode) { char[] valueName = (new String(data.getPluralName()) + "$value").toCharArray(); FieldReference thisDotValue = new FieldReference(valueName, 0L); thisDotValue.receiver = new ThisReference(0, 0); MessageSend thisDotValueGet0 = new MessageSend(); thisDotValueGet0.receiver = thisDotValue; thisDotValueGet0.selector = new char[] { 'g', 'e', 't' }; thisDotValueGet0.arguments = new Expression[] { makeIntLiteral(new char[] { '0' }, null) }; args = new Expression[] { thisDotKeyGet0, thisDotValueGet0 }; } else { args = new Expression[] { thisDotKeyGet0 }; } MessageSend invoke = new MessageSend(); invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0); invoke.selector = singletonCollectionMethod.toCharArray(); invoke.arguments = args; switchContents.add(new Assignment(new SingleNameReference(data.getPluralName(), 0), invoke, 0)); } switchContents.add(new BreakStatement(null, 0, 0)); } { // default: switchContents.add(new CaseStatement(null, 0, 0)); switchContents.addAll(createJavaUtilSimpleCreationAndFillStatements(data, builderType, mapMode, false, true, emptyCollectionMethod == null, targetType)); } SwitchStatement switchStat = new SwitchStatement(); switchStat.statements = switchContents.toArray(new Statement[switchContents.size()]); switchStat.expression = getSize(builderType, keyName, true); TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()), NULL_POSS); localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs()); LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0); varDefStat.type = localShadowerType; return Arrays.asList(varDefStat, switchStat); }
From source file:org.eclipse.jdt.internal.compiler.parser.Parser.java
License:Open Source License
protected void consumeStatementSwitch() { // SwitchStatement ::= 'switch' OpenBlock '(' Expression ')' SwitchBlock //OpenBlock just makes the semantic action blockStart() //the block is inlined but a scope need to be created //if some declaration occurs. int length;//from w w w. ja v a 2 s . c om SwitchStatement switchStatement = new SwitchStatement(); this.expressionLengthPtr--; switchStatement.expression = this.expressionStack[this.expressionPtr--]; if ((length = this.astLengthStack[this.astLengthPtr--]) != 0) { this.astPtr -= length; System.arraycopy(this.astStack, this.astPtr + 1, switchStatement.statements = new Statement[length], 0, length); } switchStatement.explicitDeclarations = this.realBlockStack[this.realBlockPtr--]; pushOnAstStack(switchStatement); switchStatement.blockStart = this.intStack[this.intPtr--]; switchStatement.sourceStart = this.intStack[this.intPtr--]; switchStatement.sourceEnd = this.endStatementPosition; if (length == 0 && !containsComment(switchStatement.blockStart, switchStatement.sourceEnd)) { switchStatement.bits |= ASTNode.UndocumentedEmptyBlock; } }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.mappings.CallinImplementorDyn.java
License:Open Source License
private void generateDispatchMethod(char[] methodName, final boolean isReplace, final boolean isAfter, final List<CallinMappingDeclaration> callinDecls, final TeamModel aTeam) { // FIXME(SH): once we know that Team has empty implementations (and checked cases involving team inheritance) // we probably want to avoid generating empty methods here. final TypeDeclaration teamDecl = aTeam.getAst(); if (teamDecl == null) return;//from w w w . j a va 2s. c o m final AstGenerator gen = new AstGenerator(teamDecl); gen.replaceableEnclosingClass = teamDecl.binding; // public void _OT$callBefore (IBoundBase2 base, int boundMethodId, int callinId, Object[] args) // public void _OT$callAfter (IBoundBase2 base, int boundMethodId, int callinId, Object[] args, Object result) // public void _OT$callReplace (IBoundBase2 base, Team[] teams, int index, int boundMethodId, int[] callinIds, Object[] args) int length = 4; if (isReplace) length = 6; else if (isAfter) length = 5; Argument[] arguments = new Argument[length]; int a = 0; arguments[a++] = gen.argument(_BASE$, gen.qualifiedTypeReference(IOTConstants.ORG_OBJECTTEAMS_IBOUNDBASE2)); if (isReplace) arguments[a++] = gen.argument(TEAMS, gen.qualifiedArrayTypeReference(IOTConstants.ORG_OBJECTTEAMS_ITEAM, 1)); if (isReplace) arguments[a++] = gen.argument(INDEX, gen.typeReference(TypeBinding.INT)); arguments[a++] = isReplace ? gen.argument(CALLIN_ID, gen.createArrayTypeReference(TypeBinding.INT, 1)) : gen.argument(CALLIN_ID, gen.typeReference(TypeBinding.INT)); arguments[a++] = gen.argument(BOUND_METHOD_ID, gen.typeReference(TypeBinding.INT)); arguments[a++] = gen.argument(ARGUMENTS, gen.qualifiedArrayTypeReference(TypeConstants.JAVA_LANG_OBJECT, 1)); if (isAfter) arguments[a++] = gen.argument(_OT_RESULT, gen.qualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT)); TypeReference returnTypeRef = isReplace ? gen.qualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT) : gen.typeReference(TypeBinding.VOID); final MethodDeclaration callMethod = gen.method(teamDecl.compilationResult, AccPublic, returnTypeRef, methodName, arguments); callMethod.isMappingWrapper = AbstractMethodDeclaration.WrapperKind.CALLIN; AstEdit.addMethod(teamDecl, callMethod); MethodModel.addCallinFlag(callMethod, IOTConstants.CALLIN_FLAG_WRAPPER); callMethod.model._declaringMappings = callinDecls; MethodModel.getModel(callMethod).setStatementsGenerator(new AbstractStatementsGenerator() { protected boolean generateStatements(AbstractMethodDeclaration methodDecl) { // into head of tryStats we generate local vars to be shared by case statements: List<Statement> tryStats = new ArrayList<Statement>(); SwitchStatement switchStat = new SwitchStatement(); switchStat.expression = isReplace ? gen.arrayReference(gen.singleNameReference(CALLIN_ID), gen.singleNameReference(INDEX)) // switch(callinId[index]) { ... : gen.singleNameReference(CALLIN_ID); // switch(callinId) { ... // statements for the body of the switchStatement: List<Statement> statements = new ArrayList<Statement>(); int callinIdCount = teamDecl.getTeamModel().getCallinIdCount(); // callinIds not handled here will be handled using a super-call. boolean[] handledCallinIds = new boolean[callinIdCount]; // do we need to catch LiftingFailedException? boolean canLiftingFail = false; // one case block per callin mapping: for (CallinMappingDeclaration callinDecl : callinDecls) { if (callinDecl.ignoreFurtherInvestigation || RoleModel.isRoleWithBaseProblem(callinDecl.scope.referenceType())) continue; if (!callinDecl.hasParsedParamMappings) // during reconcile we may not be interested in this level of detail (e.g., of a role file) continue; gen.retargetFrom(callinDecl); // one case label per bound base method: for (MethodSpec baseSpec : callinDecl.baseMethodSpecs) { statements.add(gen.caseStatement(gen.intLiteral(baseSpec.callinID))); // case <baseMethod.callinId>: handledCallinIds[baseSpec.callinID] = true; PredicateGenerator predGen = new PredicateGenerator(callinDecl.binding._declaringRoleClass, callinDecl.isReplaceCallin()); TypeBinding baseReturn = baseSpec.resolvedType(); boolean isStaticRoleMethod = callinDecl.getRoleMethod().isStatic(); ReferenceBinding roleType = callinDecl.scope.enclosingSourceType(); if (roleType.isGenericType()) // cannot handle generic role in this generated code roleType = (ReferenceBinding) callinDecl.scope.environment().convertToRawType(roleType, false); MethodBinding roleMethodBinding = callinDecl.getRoleMethod(); boolean needLiftedRoleVar = !isStaticRoleMethod && roleType.isCompatibleWith(roleMethodBinding.declaringClass); List<Statement> blockStatements = new ArrayList<Statement>(); // do we need to expose _OT$result as result? char[] resultName = null; if (callinDecl.callinModifier == TerminalTokens.TokenNameafter && (callinDecl.mappings != null || callinDecl.predicate != null) && baseReturn != TypeBinding.VOID) { resultName = RESULT; callinDecl.resultVar = gen.localBaseVariable(RESULT, baseReturn, // BaseReturnType result = (BaseReturnType)_OT$result; gen.createCastOrUnboxing(gen.singleNameReference(_OT_RESULT), baseReturn, true/*baseAccess*/)); blockStatements.add(callinDecl.resultVar); } // expose casted _base$ as "base": blockStatements.add(gen.localVariable(IOTConstants.BASE, gen.alienScopeTypeReference(gen.baseTypeReference(roleType.baseclass()), callinDecl.scope), gen.castExpression(gen.baseNameReference(_BASE$), gen.alienScopeTypeReference(gen.baseTypeReference(roleType.baseclass()), callinDecl.scope), CastExpression.RAW))); // -------------- base predicate check ------- boolean hasBasePredicate = false; for (MethodSpec baseMethodSpec : callinDecl.baseMethodSpecs) { // FIXME: check this inner loop, outer already loops over baseMethods!! char[] resultName2 = null; if (callinDecl.callinModifier == TerminalTokens.TokenNameafter && baseMethodSpec.resolvedType() != TypeBinding.VOID) { resultName2 = IOTConstants.RESULT; } // FIXME(SH): only call predidate for the current base method (from BoundMethodID?) Statement predicateCheck = predGen.createBasePredicateCheck(callinDecl, baseMethodSpec, resultName2, gen); if (predicateCheck != null) { blockStatements.add(predicateCheck); // if (!base$when(baseArg,...)) throw new LiftingVetoException(); hasBasePredicate = true; } } Expression resetFlag = CallinImplementor.setExecutingCallin(roleType.roleModel, blockStatements); // boolean _OT$oldIsExecutingCallin = _OT$setExecutingCallin(true); // ----------- receiver for role method call: ----------- Expression receiver; char[] roleVar = null; if (!isStaticRoleMethod) { if (needLiftedRoleVar) { canLiftingFail |= checkLiftingProblem(teamDecl, callinDecl, roleType); roleVar = (LOCAL_ROLE + statements.size()).toCharArray(); TypeReference roleTypeReference = gen .roleTypeReference(teamDecl.getTeamModel().getTThis(), roleType, 0); blockStatements.add(gen.localVariable(roleVar, // RoleType local$n = this._OT$liftToRoleType((BaseType)base); gen.alienScopeTypeReference(roleTypeReference, callinDecl.scope), ClassFileConstants.AccFinal, Lifting.liftCall(callMethod.scope, gen.thisReference(), gen.baseNameReference(IOTConstants.BASE), callMethod.scope .getType(IOTConstants.ORG_OBJECTTEAMS_IBOUNDBASE2, 3), roleType, false, gen))); receiver = gen.thislikeNameReference(roleVar); // private receiver needs to be casted to the class. } else { // method is from role's enclosing team receiver = gen.qualifiedThisReference(TeamModel .strengthenEnclosing(teamDecl.binding, roleMethodBinding.declaringClass)); } } else { receiver = gen .singleNameReference(callinDecl.getRoleMethod().declaringClass.sourceName()); } int baseArgOffset = 0; if (baseSpec.isCallin()) baseArgOffset += MethodSignatureEnhancer.getEnhancingArgLen(WeavingScheme.OTDRE); if (baseSpec.isStatic() && baseSpec.getDeclaringClass().isRole()) baseArgOffset += 2; // unpack arguments to be used by parameter mappings and base predicate: // ArgTypeN argn = args[n] if (callinDecl.mappings != null || (hasBasePredicate && baseSpec.arguments != null)) { TypeBinding[] baseParams = baseSpec.resolvedParameters(); for (int i = 0; i < baseSpec.arguments.length; i++) { // BaseType baseArg = castAndOrUnbox(arguments[n]); Argument baseArg = baseSpec.arguments[i]; Expression rawArg = gen.arrayReference(gen.singleNameReference(ARGUMENTS), i + baseArgOffset); Expression init = rawArg; if (!baseParams[i].isTypeVariable()) init = gen.createCastOrUnboxing(rawArg, baseParams[i], callinDecl.scope); LocalDeclaration baseArgLocal = gen.localVariable(baseArg.name, gen.alienScopeTypeReference(baseArg.type, callinDecl.scope), init); baseArgLocal.modifiers |= (baseArg.modifiers & ClassFileConstants.AccFinal); if (hasBasePredicate) { // add to front so it is already available for the base predicate check: blockStatements.add(i, baseArgLocal); } else { // otherwise give it a chance for expressions/types that depend on the role instance baseArgLocal.initialization = new PotentialRoleReceiverExpression(init, roleVar, gen.typeReference(roleType)); blockStatements.add(baseArgLocal); } } } // -- assemble arguments: TypeBinding[] roleParams = callinDecl.roleMethodSpec.resolvedParameters(); Expression[] callArgs = new Expression[roleParams.length + (isReplace ? MethodSignatureEnhancer.getEnhancingArgLen(WeavingScheme.OTDRE) : 0)]; int idx = 0; if (isReplace) for (char[] argName : REPLACE_ARG_NAMES) callArgs[idx++] = gen.singleNameReference(argName); // prepare: base, teams, boundMethodId, callinIds, index, arguments ... // prepare parameter mappings: callinDecl.traverse(new ReplaceResultReferenceVisitor(callinDecl), callinDecl.scope.classScope()); boolean hasArgError = false; for (int i = 0; i < roleParams.length; i++) { Expression arg; TypeBinding roleParam = roleParams[i]; if (roleParam.isTypeVariable()) { TypeVariableBinding tvb = (TypeVariableBinding) roleParam; if (tvb.declaringElement instanceof MethodBinding) { if (TypeBinding.equalsEquals( ((MethodBinding) tvb.declaringElement).declaringClass, roleType)) // don't use type variable of target method, see test4140_callinReplaceCompatibility10s() roleParam = roleParam.erasure(); } } TypeReference localTypeRef = null; if (callinDecl.mappings == null) { // ------------ unmapped arguments -------------- arg = gen.arrayReference(gen.singleNameReference(ARGUMENTS), i + baseArgOffset); // prepare: somePreparation(arguments[i]) TypeBinding baseArgType = baseSpec.resolvedParameters()[i]; if (roleParam.isBaseType()) { // this includes intermediate cast to boxed type: arg = gen.createUnboxing(arg, (BaseTypeBinding) roleParam); } else if (baseArgType.isBaseType()) { // Object -> BoxingType arg = gen.castExpression(arg, gen.qualifiedTypeReference( AstGenerator.boxTypeName((BaseTypeBinding) baseArgType)), CastExpression.RAW); } else { // Object -> MyBaseClass ReferenceBinding baseclass = roleType.baseclass(); if (baseclass instanceof DependentTypeBinding && baseArgType instanceof ReferenceBinding) baseArgType = RoleTypeCreator.maybeInstantiateFromPlayedBy(callinDecl.scope, (ReferenceBinding) baseArgType); arg = gen.castExpression(arg, gen.alienScopeTypeReference(gen.typeReference(baseArgType), callinDecl.scope), CastExpression.DO_WRAP); if (!roleParam.leafComponentType().isBaseType() && PotentialLiftExpression .isLiftingRequired(callinDecl.scope, roleParam, baseArgType, arg)) { // lift?(MyBaseClass) Reference liftReceiver = null; // default: let gen find the team if (roleType.isTeam() && TypeBinding.equalsEquals(roleParam.enclosingType(), roleType)) liftReceiver = gen.singleNameReference(roleVar); // lift to inner role arg = gen.potentialLift(liftReceiver, arg, roleParam, isReplace/*reversible*/); localTypeRef = gen.typeReference(roleParam); canLiftingFail |= checkLiftingProblem(teamDecl, callinDecl, (ReferenceBinding) roleParam.leafComponentType()); } } if (localTypeRef == null) localTypeRef = gen.baseclassReference(baseArgType); // unless lifting was required above } else { // ------------ mapped arguments -------------- if (roleParam.isTypeVariable() && ((TypeVariableBinding) roleParam).declaringElement instanceof CallinCalloutBinding) localTypeRef = gen.typeReference(roleParam.erasure()); // cannot explicitly mention this TVB else localTypeRef = gen.typeReference(roleParam); arg = getArgument(callinDecl, // prepare: <mappedArg<n>> (MethodDeclaration) methodDecl, callinDecl.getRoleMethod().parameters, i + idx, baseSpec); if (arg == null) { hasArgError = true; continue; // keep going to find problems with other args, too. } if (Lifting.isLiftToMethodCall(arg)) canLiftingFail |= checkLiftingProblem(teamDecl, callinDecl, roleType); boolean isBaseReference = arg instanceof SingleNameReference && CharOperation .equals(((SingleNameReference) arg).token, IOTConstants.BASE); if (needLiftedRoleVar) arg = new PotentialRoleReceiverExpression(arg, roleVar, gen.typeReference(roleType.getRealClass())); // mapped expression may require casting: "base" reference has static type IBoundBase2 if (isBaseReference) arg = gen.castExpression(arg, gen.typeReference(roleParam), CastExpression.RAW); } char[] localName = (OT_LOCAL + i).toCharArray(); // RoleParamType _OT$local$n = preparedArg<n>; blockStatements.add(gen.localVariable(localName, gen.alienScopeTypeReference(localTypeRef, callinDecl.scope), arg)); callArgs[i + idx] = gen.singleNameReference(localName); // prepare: ... _OT$local$ ... } if (hasArgError) continue; // -- role side predicate: Expression[] predicateArgs = isReplace ? MethodSignatureEnhancer.retrenchBasecallArguments(callArgs, true, WeavingScheme.OTDRE) : callArgs; predicateArgs = maybeAddResultReference(callinDecl, predicateArgs, resultName, gen); Statement rolePredicateCheck = predGen.createPredicateCheck( // if (!when(callArgs)) throw new LiftingVetoException(); callinDecl, callinDecl.scope.referenceType(), receiver, predicateArgs, callArgs, gen); if (rolePredicateCheck != null) // predicateCheck(_OT$role) blockStatements.add(rolePredicateCheck); // -- assemble the method call: // local$n.roleMethod((ArgType0)args[0], .. (ArgTypeN)args[n]); boolean lhsResolvesToTeamMethod = TypeBinding .equalsEquals(callinDecl.getRoleMethod().declaringClass, roleType.enclosingType()); // TODO(SH): more levels MessageSend roleMethodCall = (callinDecl.getRoleMethod().isPrivate() && !lhsResolvesToTeamMethod) ? new PrivateRoleMethodCall(receiver, callinDecl.roleMethodSpec.selector, callArgs, false/*c-t-f*/, callinDecl.scope, roleType, callinDecl.getRoleMethod(), gen) : gen.messageSend(receiver, callinDecl.roleMethodSpec.selector, callArgs); roleMethodCall.isGenerated = true; // for PrivateRoleMethodCall roleMethodCall.isPushedOutRoleMethodCall = true; // -- post processing: Statement[] messageSendStatements; if (isReplace) { Expression result = roleMethodCall; if (baseSpec.returnNeedsTranslation) { // lowering: TypeBinding[]/*role,base*/ returnTypes = getReturnTypes(callinDecl, 0); // who is responsible for lowering: the team or the current role? Expression lowerReceiver = (isRoleOfCurrentRole(roleType, returnTypes[0])) ? gen.singleNameReference(roleVar) : genTeamThis(gen, returnTypes[0]); result = new Lowering().lowerExpression(methodDecl.scope, result, returnTypes[0], returnTypes[1], lowerReceiver, true/*needNullCheck*/, true/*delayedResolve*/); } // possibly convert using result mapping callinDecl.checkResultMapping(); boolean isResultBoxed = baseReturn.isBaseType() && baseReturn != TypeBinding.VOID; if (callinDecl.mappings != null && callinDecl.isResultMapped) { if (isResultBoxed) result = gen.createUnboxing(result, (BaseTypeBinding) baseReturn); Expression mappedResult = new PotentialRoleReceiverExpression( callinDecl.getResultExpression(baseSpec, isResultBoxed, gen/*stepOverGen*/), roleVar, gen.typeReference(roleType.getRealClass())); messageSendStatements = new Statement[] { callinDecl.resultVar = gen.localVariable(IOTConstants.RESULT, baseReturn, // result = (Type)role.roleMethod(args); gen.castExpression(result, gen.typeReference(baseReturn), CastExpression.RAW)), // cast because role return might be generalized gen.returnStatement(mappedResult) // return mappedResult(result); }; } else { if (isResultBoxed) { // $if_need_result_unboxing$ messageSendStatements = new Statement[] { gen.localVariable(IOTConstants.OT_RESULT, // Object _OT$result = role.roleMethod(args); gen.qualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT), result), CallinImplementor.genResultNotProvidedCheck( // if (_OT$result == null) teamDecl.binding.readableName(), // throw new ResultNotProvidedException(..) roleType.readableName(), roleMethodBinding, roleType.baseclass(), baseSpec, gen), gen.returnStatement(gen.singleNameReference(IOTConstants.OT_RESULT)) // return _OT$result; }; } else { // $endif$ messageSendStatements = new Statement[] { gen.returnStatement(result) }; // return role.roleMethod(args); } } } else { messageSendStatements = new Statement[] { roleMethodCall, // role.roleMethod(args); gen.breakStatement() // break; }; } // assemble: // try { roleMessageSend(); } // catch(Exception _OT$caughtException) { throw new SneakyException(_OT$caughtException); } // finally { _OT$setExecutingCallin(_OT$oldIsExecutingCallin); } blockStatements.add( protectRoleMethodCall(messageSendStatements, roleMethodBinding, resetFlag, gen)); statements.add(gen.block(blockStatements.toArray(new Statement[blockStatements.size()]))); // collectively report the problem(s) if (canLiftingFail && callinDecl.rolesWithLiftingProblem != null) for (Map.Entry<ReferenceBinding, Integer> entry : callinDecl.rolesWithLiftingProblem .entrySet()) callinDecl.scope.problemReporter().callinDespiteLiftingProblem(entry.getKey(), entry.getValue(), callinDecl); } } // END for (CallinMappingDeclaration callinDecl : callinDecls) gen.retargetFrom(teamDecl); boolean needSuperCall = false; // do we have a relevant super team, which possibly defines more callins? ReferenceBinding superTeam = aTeam.getBinding().superclass(); if (superTeam != null && superTeam.isTeam() && superTeam.id != IOTConstants.T_OrgObjectTeamsTeam) { // callinIds to be handled by super call? for (int i = 0; i < callinIdCount; i++) if (!handledCallinIds[i]) { statements.add(gen.caseStatement(gen.intLiteral(i))); // case callinIdOfSuper: needSuperCall = true; } if (!isReplace) needSuperCall = true; // a super call might become necessary after the fact when this dispatch method // is copy-inherited to a tsub-team, because the tsub-team may have a super // with more callins, see test1111_roleInheritsCallinFromTsupers1. // TODO: can we safely handle this for the replace-case, too?? // (replace needs to "return _OT$callNext();" in the default branch, see below). // See https://bugs.eclipse.org/433123 } if (needSuperCall) { if (!isReplace) statements.add(gen.caseStatement(null)); // default label char[] selector; char[][] argNames; if (isReplace) { selector = OT_CALL_REPLACE; argNames = REPLACE_ARG_NAMES; } else if (isAfter) { selector = OT_CALL_AFTER; argNames = AFTER_ARG_NAMES; } else { selector = OT_CALL_BEFORE; argNames = BEFORE_ARG_NAMES; } Expression[] superCallArgs = new Expression[argNames.length]; for (int idx = 0; idx < argNames.length; idx++) superCallArgs[idx] = gen.singleNameReference(argNames[idx]); // if we have a tsuper team which a corresponding dispatch method that one takes precedence: MessageSend superCall = aTeam.hasTSuperTeamMethod(selector) ? gen.tsuperMessageSend(gen.thisReference(), selector, superCallArgs) : gen.messageSend(gen.superReference(), selector, superCallArgs); if (isReplace) statements.add(gen.returnStatement(superCall)); // return super._OT$callReplace(..); else statements.add(superCall); // super._OT$callBefore/After(..); } Statement catchStatement1 = gen.emptyStatement(); Statement catchStatement2 = gen.emptyStatement(); if (isReplace) { // default: callNext: Expression[] callArgs = new Expression[REPLACE_ARG_NAMES.length + 1]; for (int idx = 0; idx < REPLACE_ARG_NAMES.length; idx++) callArgs[idx] = gen.singleNameReference(REPLACE_ARG_NAMES[idx]); callArgs[callArgs.length - 1] = gen.nullLiteral(); // no explicit baseCallArguments statements.add(gen.caseStatement(null)); // default: statements.add(gen.returnStatement( // return _OT$callNext(..); gen.messageSend(gen.qualifiedThisReference(aTeam.getBinding()), OT_CALL_NEXT, callArgs))); catchStatement1 = gen.returnStatement(gen .messageSend(gen.qualifiedThisReference(aTeam.getBinding()), OT_CALL_NEXT, callArgs)); catchStatement2 = gen.returnStatement(gen .messageSend(gen.qualifiedThisReference(aTeam.getBinding()), OT_CALL_NEXT, callArgs)); } // ==== overall assembly: ==== switchStat.statements = statements.toArray(new Statement[statements.size()]); Argument[] exceptionArguments; Statement[][] exceptionStatementss; if (canLiftingFail) { exceptionArguments = new Argument[] { gen.argument("ex".toCharArray(), //$NON-NLS-1$ gen.qualifiedTypeReference(IOTConstants.ORG_OBJECTTEAMS_LIFTING_VETO)), gen.argument("ex".toCharArray(), //$NON-NLS-1$ gen.qualifiedTypeReference(IOTConstants.O_O_LIFTING_FAILED_EXCEPTION)) }; exceptionStatementss = new Statement[][] { { catchStatement1 }, { catchStatement2 } }; } else { exceptionArguments = new Argument[] { gen.argument("ex".toCharArray(), //$NON-NLS-1$ gen.qualifiedTypeReference(IOTConstants.ORG_OBJECTTEAMS_LIFTING_VETO)) }; exceptionStatementss = new Statement[][] { { catchStatement1 } }; } tryStats.add(switchStat); methodDecl.statements = new Statement[] { gen.tryCatch(tryStats.toArray(new Statement[tryStats.size()]), // expected exception is ignored, do nothing (before/after) or proceed to callNext (replace) exceptionArguments, exceptionStatementss) }; methodDecl.hasParsedStatements = true; return true; } }); }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.mappings.CallinImplementorDyn.java
License:Open Source License
private void generateCallNext(final List<CallinMappingDeclaration> callinDecls, final TeamModel aTeam) { // public Object _OT$callNext(IBoundBase2 baze, Team[] teams, int idx, int[] callinIds, int boundMethodId, Object[] args, Object[] baseCallArgs) final TypeDeclaration teamDecl = aTeam.getAst(); if (teamDecl == null) return;//from w ww. j a va 2s . c o m final AstGenerator gen = new AstGenerator(teamDecl); Argument[] args = new Argument[] { gen.argument(IOTConstants.BASE, gen.qualifiedTypeReference(IOTConstants.ORG_OBJECTTEAMS_IBOUNDBASE2)), gen.argument(TEAMS, gen.qualifiedArrayTypeReference(IOTConstants.ORG_OBJECTTEAMS_ITEAM, 1)), gen.argument(INDEX, gen.typeReference(TypeBinding.INT)), gen.argument(CALLIN_ID, gen.createArrayTypeReference(TypeBinding.INT, 1)), gen.argument(BOUND_METHOD_ID, gen.typeReference(TypeBinding.INT)), gen.argument(ARGUMENTS, gen.qualifiedArrayTypeReference(TypeConstants.JAVA_LANG_OBJECT, 1)), gen.argument(BASE_CALL_ARGS, gen.qualifiedArrayTypeReference(TypeConstants.JAVA_LANG_OBJECT, 1)) }; // super call directly passes all these args through: Expression[] superArgs = new Expression[args.length]; for (int i = 0; i < args.length; i++) superArgs[i] = gen.singleNameReference(args[i].name); MethodDeclaration decl = gen.method(teamDecl.compilationResult, AccPublic, gen.qualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT), OT_CALL_NEXT, args); // the payload: reverse parameter mappings: SwitchStatement swStat = new SwitchStatement(); swStat.expression = gen.arrayReference(gen.singleNameReference(CALLIN_ID), gen.singleNameReference(INDEX)); // switch(callinId[index]) { ... List<Statement> swStatements = new ArrayList<Statement>(); for (CallinMappingDeclaration mapping : callinDecls) { List<Statement> caseBlockStats = new ArrayList<Statement>(); int nLabels = 0; for (MethodSpec baseSpec : mapping.baseMethodSpecs) caseBlockStats.add(gen.caseStatement(gen.intLiteral(baseSpec.getCallinId(aTeam)))); // case bseSpecCallinId: int nRoleArgs = mapping.getRoleMethod().getSourceParamLength(); TypeBinding[] roleParams = mapping.getRoleMethod().getSourceParameters(); for (int i = 0; i < roleParams.length; i++) if (roleParams[i].isRole() && TeamModel.isTeamContainingRole(teamDecl.binding, (ReferenceBinding) roleParams[i])) roleParams[i] = TeamModel.strengthenRoleType(teamDecl.binding, roleParams[i]); List<Statement> repackingStats = new ArrayList<Statement>(); if (mapping.positions != null) { int[] poss = mapping.positions; nLabels = caseBlockStats.size(); int argOffset = 0; // some methods have their real arguments at an offset MethodSpec baseSpec = mapping.baseMethodSpecs[0]; // TODO(SH): check all base methods?? if (baseSpec.isCallin()) argOffset += 6; if (baseSpec.isStatic() && baseSpec.getDeclaringClass().isRole()) argOffset += 2; for (int i = 0; i < poss.length; i++) // arguments[basepos] = baseCallArguments[i] if (poss[i] > 0) { // FIXME(SH): this is cheating: should obtain translation info from actual // parameter mapping (see cast in test432_expressionInReplaceParameterMapping11) TypeBinding roleSideParameter = roleParams[i]; // FIXME(SH): per basemethod: TypeBinding baseSideParameter = mapping.baseMethodSpecs[0].resolvedParameters()[poss[i] - 1]; Expression roleSideArgument = gen.arrayReference(gen.singleNameReference(BASE_CALL_ARGS), i);// ... baseCallArguments[i] ... if (TypeBinding.notEquals(roleSideParameter, baseSideParameter)) roleSideArgument = gen.resolvedCastExpression(roleSideArgument, roleSideParameter, CastExpression.RAW); TypeBinding roleSideLeaf = roleSideParameter.leafComponentType(); TypeBinding baseSideLeaf = baseSideParameter.leafComponentType(); if (roleSideLeaf.isRole() && ((ReferenceBinding) roleSideLeaf).baseclass().isCompatibleWith(baseSideLeaf)) roleSideArgument = new PotentialLowerExpression(roleSideArgument, baseSideParameter, gen.thisReference()); repackingStats.add(gen.assignment(gen.arrayReference(gen.singleNameReference(ARGUMENTS), // arguments[p] = baseCallArguments[i]; poss[i] - 1 + argOffset), // 0 represents result roleSideArgument)); } } else if (nRoleArgs > 0) { for (int i = 0; i < nRoleArgs; i++) { Expression basecallArg = gen.arrayReference(gen.singleNameReference(BASE_CALL_ARGS), i); if (mapping.baseMethodSpecs[0].argNeedsTranslation(i)) { // FIXME(SH): per basemethod! basecallArg = new PotentialLowerExpression( gen.castExpression(basecallArg, gen.typeReference(roleParams[i]), CastExpression.RAW), mapping.baseMethodSpecs[0].resolvedParameters()[i], // FIXME(SH): per basemethod! gen.qualifiedThisReference(teamDecl.binding)); } repackingStats.add(gen.assignment(gen.arrayReference(gen.singleNameReference(ARGUMENTS), i), // arguments[i] = lower?(baseCallArguments[i]) basecallArg)); } } caseBlockStats.add(gen.ifStatement(gen.nullCheck(gen.singleNameReference(BASE_CALL_ARGS)), // if (baseCallArgs == null) {} { arguments[i] = ...; ... } gen.emptyStatement(), gen.block(repackingStats.toArray(new Statement[repackingStats.size()])))); Expression result = genSuperCallNext(gen, teamDecl.binding, superArgs); // return cast+lift?(super._OT$callNext(..)); if (mapping.baseMethodSpecs[0].returnNeedsTranslation) { // FIXME(SH): per basemethod! // lifting: TypeBinding[]/*role,base*/ returnTypes = getReturnTypes(mapping, 0); // who is responsible for lifting: the team or the current role? ReferenceBinding currentRole = mapping.scope.enclosingReceiverType(); Expression liftReceiver = (isRoleOfCurrentRole(currentRole, returnTypes[0])) ? Lifting.liftCall(mapping.scope, gen.thisReference(), gen.castExpression(gen.singleNameReference(IOTConstants.BASE), gen.typeReference(currentRole.baseclass()), CastExpression.RAW), currentRole.baseclass(), currentRole, false) // TODO: might want to extend the signature of callNext to pass the current role to avoid this lifting? : genTeamThis(gen, returnTypes[0]); result = Lifting.liftCall(mapping.scope, liftReceiver, gen.castExpression(result, gen.baseclassReference(returnTypes[1]), CastExpression.RAW), returnTypes[1], returnTypes[0], false, gen); } caseBlockStats.add(gen.returnStatement(result)); if (caseBlockStats.size() > nLabels) { // any action added ? swStatements.addAll(caseBlockStats); } } // } // end-switch if (swStatements.size() == 0) return; // don't add useless method swStat.statements = swStatements.toArray(new Statement[swStatements.size()]); decl.statements = new Statement[] { swStat, gen.returnStatement(genSuperCallNext(gen, teamDecl.binding, superArgs)) // delegate with unchanged arguments/return }; decl.hasParsedStatements = true; AstEdit.addMethod(teamDecl, decl); }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.mappings.CallinImplementorDyn.java
License:Open Source License
private void generateCallOrigStatic(List<CallinMappingDeclaration> callinDecls, TeamModel aTeam) { // public Object _OT$callOrigStatic(int callinId, int boundMethodId, Object[] args) // this team method delegates to the corresponding _OT$callOrigStatic(int,Object[]) // of the appropriate base classes. final TypeDeclaration teamDecl = aTeam.getAst(); if (teamDecl == null) return;// w w w . j a va 2 s .co m final AstGenerator gen = new AstGenerator(teamDecl); Argument[] args = new Argument[] { gen.argument(CALLIN_ID, gen.typeReference(TypeBinding.INT)), gen.argument(BOUND_METHOD_ID, gen.typeReference(TypeBinding.INT)), gen.argument(ARGUMENTS, gen.qualifiedArrayTypeReference(TypeConstants.JAVA_LANG_OBJECT, 1)) }; Expression[] passThroughArgs = new Expression[] { gen.singleNameReference(BOUND_METHOD_ID), gen.singleNameReference(ARGUMENTS) }; MethodDeclaration decl = gen.method(teamDecl.compilationResult, AccPublic, gen.qualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT), OT_CALL_ORIG_STATIC, args); SwitchStatement swStat = new SwitchStatement(); swStat.expression = gen.singleNameReference(CALLIN_ID); // switch(callinId) { ... List<Statement> swStatements = new ArrayList<Statement>(); for (CallinMappingDeclaration mapping : callinDecls) { for (MethodSpec baseSpec : mapping.baseMethodSpecs) { MethodBinding baseMethod = baseSpec.resolvedMethod; if (baseMethod.isStatic()) { ReferenceBinding baseClass = mapping.scope.enclosingReceiverType().baseclass(); swStatements.add(gen.caseStatement(gen.intLiteral(baseSpec.getCallinId(aTeam)))); // case baseSpecCallinId: Expression result = gen.fakeMessageSend(gen.baseTypeReference(baseClass), // return BaseClass._OT$callOrigStatic(boundMethodId, args); OT_CALL_ORIG_STATIC, passThroughArgs, baseMethod.declaringClass, mapping.scope.getJavaLangObject()); swStatements.add(gen.returnStatement(result)); } } } // } // end-switch if (swStatements.size() == 0) return; // don't add useless method swStat.statements = swStatements.toArray(new Statement[swStatements.size()]); decl.statements = new Statement[] { swStat, gen.returnStatement(gen.nullLiteral()) // shouldn't happen }; decl.hasParsedStatements = true; AstEdit.addMethod(teamDecl, decl); }
From source file:org.nabucco.framework.mda.template.java.extract.statement.JavaAstStatementExtractorVisitor.java
License:Open Source License
@Override public boolean visit(SwitchStatement switchStatement, BlockScope scope) { SwitchStatement switchCopy = new SwitchStatement(); switchCopy.expression = copy(switchStatement.expression, scope); switchCopy.cases = copy(switchStatement.cases, scope); switchCopy.statements = copy(switchStatement.statements, scope); this.statement = switchCopy; return false; }