List of usage examples for org.eclipse.jdt.internal.compiler.lookup ParameterizedTypeBinding ParameterizedTypeBinding
public ParameterizedTypeBinding(ReferenceBinding type, TypeBinding[] arguments, ReferenceBinding enclosingType,
LookupEnvironment environment)
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.mappings.MethodMappingImplementor.java
License:Open Source License
/** If original is a type variable or contains a type variable replace that type variable * with a corresponding type variable from variables. * This method is used to adjust the scope of type variables that originally were * resolved in the method mappings scope, but should be resolved in the wrapper method scope. *///from w w w . j a v a2 s. c o m TypeBinding substituteVariables(TypeBinding original, TypeVariableBinding[] variables) { if (original.isTypeVariable()) { for (int i = 0; i < variables.length; i++) if (CharOperation.equals(original.internalName(), variables[i].sourceName)) return variables[i]; } else if (original.isParameterizedType()) { ParameterizedTypeBinding pt = (ParameterizedTypeBinding) original; TypeBinding[] args = pt.arguments; if (args != null) { int l = args.length; System.arraycopy(args, 0, args = new TypeBinding[l], 0, l); boolean changed = false; for (int i = 0; i < l; i++) { TypeBinding tb = substituteVariables(args[i], variables); if (TypeBinding.notEquals(tb, args[i])) { args[i] = tb; changed = true; } } if (changed) return new ParameterizedTypeBinding((ReferenceBinding) pt.erasure(), args, pt.enclosingType(), pt.environment); } } return original; }
From source file:org.eclipse.objectteams.otdt.internal.core.compiler.statemachine.copyinheritance.CopyInheritance.java
License:Open Source License
/** * For a given role method adjust its signature according to the * tsuper-role version which is provided by its binding. * For each argument type:// w w w.j a v a 2 s .co m * <ul> * <li> if it is a role type, make it refer explicitly to the type in the super team * <li> for each parameter whose type is adjusted insert a new local with the desired * type and cast the provided argument value to this local. * <li> replace arguments in super calls of a constructor by the renamed and * and casted argument. * </ul> * E.g., the constructor * <pre> Role (Role2 r) { * super(r); * print(r); * }</pre> * becomes: * <pre> Role (SuperTeam.Role2 __OT__r) { * super((Role2)__OT__r); * Role2 r = (Role2)__OT__r; * print(r); * </pre> * @param method method to adjust * @param template the tsuper version which should be taken as master for adjustment. * @return has weakening been performed? */ public static boolean weakenSignature(AbstractMethodDeclaration method, MethodBinding template) { MethodBinding binding = method.binding; // no weakening for constructors: if (method.isConstructor()) return false; // do not touch broken methods: if (binding == null || method.hasErrors()) return false; if (MethodModel.hasProblem(template)) { method.tagAsHavingErrors(); // propagate error return false; } MethodScope scope = method.scope; boolean changed = false; // true weakening (with weakened type binding) avoids bridge methods if (TypeBinding.notEquals(method.binding.returnType, template.returnType) && method.binding.returnType instanceof DependentTypeBinding) method.binding.returnType = WeakenedTypeBinding.makeWeakenedTypeBinding( (DependentTypeBinding) method.binding.returnType.leafComponentType(), (ReferenceBinding) template.returnType.leafComponentType(), template.returnType.dimensions()); // liftTo methods have no role arguments if (Lifting.isLiftToMethod(method.binding)) return changed; // Method parameters int paramLen = binding.parameters.length; assert (paramLen == template.parameters.length); if (paramLen == 0) return changed; if (method.isConstructor() && binding.declaringClass.isTeam()) return changed; // already processed by Lifting.prepareArgLifting() (includes a cast). // selectively share type bindings, but not the array: for (int i = 0; i < paramLen; i++) { TypeBinding param = binding.parameters[i]; TypeBinding tmplParam = template.parameters[i]; if (param.isRole()) { binding.parameters[i] = template.parameters[i]; if (param.isParameterizedType() && tmplParam.isParameterizedType()) { // keep old type arguments, just replace the type itself TypeBinding[] args = ((ParameterizedTypeBinding) param).arguments; ReferenceBinding tmplType = ((ParameterizedTypeBinding) tmplParam).genericType(); try { LookupEnvironment env = Config.getLookupEnvironment(); binding.parameters[i] = new ParameterizedTypeBinding(tmplType, args, tmplType.enclosingType(), env); } catch (NotConfiguredException e) { e.logWarning("Cannot create parameterized type"); //$NON-NLS-1$ } } } } // below: treat statements. if (method.isAbstract()) return changed; if (method.isCopied) return changed; // no statements ArrayList<Statement> newLocalStats = new ArrayList<Statement>(); for (int i = 0; i < method.arguments.length; i++) { final Argument argument = method.arguments[i]; char[] oldName = argument.name; if (RoleTypeBinding.isRoleWithExplicitAnchor(argument.type.resolvedType)) continue; TypeReference newType = TypeAnalyzer.weakenTypeReferenceFromBinding(scope, argument.type, argument.binding.type, binding.parameters[i]); if (newType != argument.type) { changed = true; newType.setBaseclassDecapsulation(argument.type.getBaseclassDecapsulation()); // local variable: newLocalStats.add(generateCastedLocal(argument, newType)); // replace arguments in super-constructor call: if (method instanceof ConstructorDeclaration) { ConstructorDeclaration ctor = (ConstructorDeclaration) method; ReplaceSingleNameVisitor.IExpressionProvider provider = new ReplaceSingleNameVisitor.IExpressionProvider() { public Expression newExpression() { return new SingleNameReference(argument.name, argument.sourceStart); } }; if (ctor.constructorCall != null) ctor.constructorCall.traverse(new ReplaceSingleNameVisitor(oldName, provider), null); // no scope } } } if (!binding.declaringClass.isDirectRole() && CharOperation.equals(binding.selector, IOTConstants.INIT_METHOD_NAME)) return changed; // no statements, not casted locals. if (!newLocalStats.isEmpty()) { if (StateHelper.hasState(binding.declaringClass, ITranslationStates.STATE_RESOLVED)) for (Statement localDeclaration : newLocalStats) localDeclaration.resolve(method.scope); // add the local variable declaration statements: MethodModel.prependStatements(method, newLocalStats); } return changed; }