use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class ASTNodeFactory method newCreationType.
public static Type newCreationType(AST ast, ITypeBinding typeBinding, ImportRewrite importRewrite, ImportRewriteContext importContext) {
if (typeBinding.isParameterizedType()) {
Type baseType = newCreationType(ast, typeBinding.getTypeDeclaration(), importRewrite, importContext);
ParameterizedType parameterizedType = ast.newParameterizedType(baseType);
for (ITypeBinding typeArgument : typeBinding.getTypeArguments()) {
parameterizedType.typeArguments().add(newCreationType(ast, typeArgument, importRewrite, importContext));
}
return parameterizedType;
} else if (typeBinding.isParameterizedType()) {
Type elementType = newCreationType(ast, typeBinding.getElementType(), importRewrite, importContext);
ArrayType arrayType = ast.newArrayType(elementType, 0);
while (typeBinding.isArray()) {
Dimension dimension = ast.newDimension();
IAnnotationBinding[] typeAnnotations = typeBinding.getTypeAnnotations();
for (IAnnotationBinding typeAnnotation : typeAnnotations) {
dimension.annotations().add(importRewrite.addAnnotation(typeAnnotation, ast, importContext));
}
arrayType.dimensions().add(dimension);
typeBinding = typeBinding.getComponentType();
}
return arrayType;
} else if (typeBinding.isWildcardType()) {
ITypeBinding bound = typeBinding.getBound();
typeBinding = (bound != null) ? bound : typeBinding.getErasure();
return newCreationType(ast, typeBinding, importRewrite, importContext);
} else {
return importRewrite.addImport(typeBinding, ast, importContext);
}
}
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;
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project flux by eclipse.
the class ASTNodes method getQualifiedTypeName.
/**
* Returns the (potentially qualified) name of a type, followed by array dimensions.
* Skips type arguments and type annotations.
*
* @param type a type that has a name
* @return the name, followed by array dimensions
* @since 3.10
*/
public static String getQualifiedTypeName(Type type) {
final StringBuffer buffer = new StringBuffer();
ASTVisitor visitor = new ASTVisitor() {
@Override
public boolean visit(SimpleType node) {
buffer.append(node.getName().getFullyQualifiedName());
return false;
}
@Override
public boolean visit(QualifiedType node) {
node.getQualifier().accept(this);
buffer.append('.');
buffer.append(node.getName().getIdentifier());
return false;
}
@Override
public boolean visit(NameQualifiedType node) {
buffer.append(node.getQualifier().getFullyQualifiedName());
buffer.append('.');
buffer.append(node.getName().getIdentifier());
return false;
}
@Override
public boolean visit(ParameterizedType node) {
node.getType().accept(this);
return false;
}
@Override
public void endVisit(ArrayType node) {
for (int i = 0; i < node.dimensions().size(); i++) {
//$NON-NLS-1$
buffer.append("[]");
}
}
};
type.accept(visitor);
return buffer.toString();
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class InferTypeArgumentsRefactoring method inferArguments.
public static ParameterizedType[] inferArguments(SimpleType[] types, InferTypeArgumentsUpdate update, InferTypeArgumentsTCModel model, CompilationUnitRewrite rewrite) {
for (int i = 0; i < types.length; i++) {
types[i].setProperty(REWRITTEN, null);
}
List<ParameterizedType> result = new ArrayList<ParameterizedType>();
HashMap<ICompilationUnit, CuUpdate> updates = update.getUpdates();
Set<Entry<ICompilationUnit, CuUpdate>> entrySet = updates.entrySet();
for (Iterator<Entry<ICompilationUnit, CuUpdate>> iter = entrySet.iterator(); iter.hasNext(); ) {
Entry<ICompilationUnit, CuUpdate> entry = iter.next();
rewrite.setResolveBindings(false);
CuUpdate cuUpdate = entry.getValue();
for (Iterator<CollectionElementVariable2> cvIter = cuUpdate.getDeclarations().iterator(); cvIter.hasNext(); ) {
ConstraintVariable2 cv = cvIter.next();
ParameterizedType newNode = rewriteConstraintVariable(cv, rewrite, model, false, types);
if (newNode != null)
result.add(newNode);
}
}
return result.toArray(new ParameterizedType[result.size()]);
}
Aggregations