use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class IntroduceFactoryRefactoring method setCtorTypeArguments.
/**
* Sets the type being instantiated in the given constructor call, including
* specifying any necessary type arguments.
* @param newCtorCall the constructor call to modify
* @param ctorTypeName the simple name of the type being instantiated
* @param ctorOwnerTypeParameters the formal type parameters of the type being
* instantiated
* @param ast utility object used to create AST nodes
*/
private void setCtorTypeArguments(ClassInstanceCreation newCtorCall, String ctorTypeName, ITypeBinding[] ctorOwnerTypeParameters, AST ast) {
if (// easy, just a simple type
ctorOwnerTypeParameters.length == 0)
newCtorCall.setType(ASTNodeFactory.newType(ast, ctorTypeName));
else {
Type baseType = ast.newSimpleType(ast.newSimpleName(ctorTypeName));
ParameterizedType newInstantiatedType = ast.newParameterizedType(baseType);
List<Type> newInstTypeArgs = newInstantiatedType.typeArguments();
for (int i = 0; i < ctorOwnerTypeParameters.length; i++) {
Type typeArg = ASTNodeFactory.newType(ast, ctorOwnerTypeParameters[i].getName());
newInstTypeArgs.add(typeArg);
}
newCtorCall.setType(newInstantiatedType);
}
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class IntroduceFactoryRefactoring method setMethodReturnType.
/**
* Sets the return type of the factory method, including any necessary type
* arguments. E.g., for constructor <code>Foo()</code> in <code>Foo<T></code>,
* the factory method defines a method type parameter <code><T></code> and
* returns a <code>Foo<T></code>.
* @param newMethod the method whose return type is to be set
* @param retTypeName the simple name of the return type (without type parameters)
* @param ctorOwnerTypeParameters the formal type parameters of the type that the
* factory method instantiates (whose constructor is being encapsulated)
* @param ast utility object used to create AST nodes
*/
private void setMethodReturnType(MethodDeclaration newMethod, String retTypeName, ITypeBinding[] ctorOwnerTypeParameters, AST ast) {
if (ctorOwnerTypeParameters.length == 0)
newMethod.setReturnType2(ast.newSimpleType(ast.newSimpleName(retTypeName)));
else {
Type baseType = ast.newSimpleType(ast.newSimpleName(retTypeName));
ParameterizedType newRetType = ast.newParameterizedType(baseType);
List<Type> newRetTypeArgs = newRetType.typeArguments();
for (int i = 0; i < ctorOwnerTypeParameters.length; i++) {
Type retTypeArg = ASTNodeFactory.newType(ast, ctorOwnerTypeParameters[i].getName());
newRetTypeArgs.add(retTypeArg);
}
newMethod.setReturnType2(newRetType);
}
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class TypeArgumentMismatchSubProcessor method removeMismatchedArguments.
// public static void getTypeParameterMismatchProposals(IInvocationContext context, IProblemLocation problem, Collection proposals) {
// CompilationUnit astRoot= context.getASTRoot();
// ASTNode selectedNode= problem.getCoveredNode(astRoot);
// if (!(selectedNode instanceof SimpleName)) {
// return;
// }
// ASTNode normalizedNode= ASTNodes.getNormalizedNode(selectedNode);
// if (!(normalizedNode instanceof ParameterizedType)) {
// return;
// }
// // waiting for result of https://bugs.eclipse.org/bugs/show_bug.cgi?id=81544
// }
public static void removeMismatchedArguments(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveredNode(context.getASTRoot());
if (!(selectedNode instanceof SimpleName)) {
return;
}
ASTNode normalizedNode = ASTNodes.getNormalizedNode(selectedNode);
if (normalizedNode instanceof ParameterizedType) {
ASTRewrite rewrite = ASTRewrite.create(normalizedNode.getAST());
ParameterizedType pt = (ParameterizedType) normalizedNode;
ASTNode mt = rewrite.createMoveTarget(pt.getType());
rewrite.replace(pt, mt, null);
String label = CorrectionMessages.TypeArgumentMismatchSubProcessor_removeTypeArguments;
Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_TYPE_ARGUMENTS, image);
proposals.add(proposal);
}
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class InferTypeArgumentsRefactoring method rewriteTypeVariable.
private static ParameterizedType rewriteTypeVariable(TypeVariable2 typeCv, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel, boolean leaveUnconstraindRaw, SimpleType[] types) {
ASTNode node = typeCv.getRange().getNode(rewrite.getRoot());
if (node instanceof Name && node.getParent() instanceof Type) {
Type originalType = (Type) node.getParent();
if (types != null && !has(types, originalType))
return null;
// Must rewrite all type arguments in one batch. Do the rewrite when the first one is encountered; skip the others.
Object rewritten = originalType.getProperty(REWRITTEN);
if (rewritten == REWRITTEN)
return null;
originalType.setProperty(REWRITTEN, REWRITTEN);
ArrayList<CollectionElementVariable2> typeArgumentCvs = getTypeArgumentCvs(typeCv, tCModel);
Type[] typeArguments = getTypeArguments(originalType, typeArgumentCvs, rewrite, tCModel, leaveUnconstraindRaw);
if (typeArguments == null)
return null;
Type movingType = (Type) rewrite.getASTRewrite().createMoveTarget(originalType);
ParameterizedType newType = rewrite.getAST().newParameterizedType(movingType);
for (int i = 0; i < typeArguments.length; i++) {
newType.typeArguments().add(typeArguments[i]);
}
rewrite.getASTRewrite().replace(originalType, newType, rewrite.createGroupDescription(RefactoringCoreMessages.InferTypeArgumentsRefactoring_addTypeArguments));
return newType;
} else {
//TODO: other node types?
return null;
}
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class InferTypeArgumentsRefactoring method getTypeArguments.
/**
* @param baseType the base type
* @param typeArgumentCvs type argument constraint variables
* @param rewrite the cu rewrite
* @param tCModel the type constraints model
* @param leaveUnconstraindRaw <code>true</code> to keep unconstrained type references raw,
* <code>false</code> to infer <code><?></code> if possible
* @return the new type arguments, or <code>null</code> iff an argument could not be inferred
*/
private static Type[] getTypeArguments(Type baseType, ArrayList<CollectionElementVariable2> typeArgumentCvs, CompilationUnitRewrite rewrite, InferTypeArgumentsTCModel tCModel, boolean leaveUnconstraindRaw) {
if (typeArgumentCvs.size() == 0)
return null;
Type[] typeArguments = new Type[typeArgumentCvs.size()];
for (int i = 0; i < typeArgumentCvs.size(); i++) {
CollectionElementVariable2 elementCv = typeArgumentCvs.get(i);
Type typeArgument;
TType chosenType = InferTypeArgumentsConstraintsSolver.getChosenType(elementCv);
if (chosenType != null) {
if (chosenType.isWildcardType() && !unboundedWildcardAllowed(baseType))
// can't e.g. write "new ArrayList<?>()".
return null;
if (// workaround for bug 99124
chosenType.isParameterizedType())
chosenType = chosenType.getTypeDeclaration();
BindingKey bindingKey = new BindingKey(chosenType.getBindingKey());
typeArgument = rewrite.getImportRewrite().addImportFromSignature(bindingKey.toSignature(), rewrite.getAST());
ArrayList<CollectionElementVariable2> nestedTypeArgumentCvs = getTypeArgumentCvs(elementCv, tCModel);
//recursion
Type[] nestedTypeArguments = getTypeArguments(typeArgument, nestedTypeArgumentCvs, rewrite, tCModel, leaveUnconstraindRaw);
if (nestedTypeArguments != null) {
ParameterizedType parameterizedType = rewrite.getAST().newParameterizedType(typeArgument);
for (int j = 0; j < nestedTypeArguments.length; j++) parameterizedType.typeArguments().add(nestedTypeArguments[j]);
typeArgument = parameterizedType;
}
} else {
// couldn't infer an element type (no constraints)
if (leaveUnconstraindRaw) {
// every guess could be wrong => leave the whole thing raw
return null;
} else {
if (unboundedWildcardAllowed(baseType)) {
typeArgument = rewrite.getAST().newWildcardType();
} else {
//$NON-NLS-1$
String object = rewrite.getImportRewrite().addImport("java.lang.Object");
typeArgument = (Type) rewrite.getASTRewrite().createStringPlaceholder(object, ASTNode.SIMPLE_TYPE);
}
}
// ASTNode baseTypeParent= baseType.getParent();
// if (baseTypeParent instanceof ClassInstanceCreation) {
// //No ? allowed. Take java.lang.Object.
// typeArgument= rewrite.getAST().newSimpleType(rewrite.getAST().newName(rewrite.getImportRewrite().addImport("java.lang.Object"))); //$NON-NLS-1$
// } else if (baseTypeParent instanceof ArrayCreation || baseTypeParent instanceof InstanceofExpression) {
// //Only ? allowed.
// typeArgument= rewrite.getAST().newWildcardType();
// } else {
// //E.g. field type: can put anything. Choosing ? in order to be most constraining.
// typeArgument= rewrite.getAST().newWildcardType();
// }
}
typeArguments[i] = typeArgument;
}
return typeArguments;
}
Aggregations