use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class ConvertAnonymousToNestedRefactoring method createNewClassInstanceCreation.
private ASTNode createNewClassInstanceCreation(CompilationUnitRewrite rewrite, ITypeBinding[] parameters) {
AST ast = fAnonymousInnerClassNode.getAST();
ClassInstanceCreation newClassCreation = ast.newClassInstanceCreation();
newClassCreation.setAnonymousClassDeclaration(null);
Type type = null;
SimpleName newNameNode = ast.newSimpleName(fClassName);
if (parameters.length > 0) {
final ParameterizedType parameterized = ast.newParameterizedType(ast.newSimpleType(newNameNode));
for (int index = 0; index < parameters.length; index++) parameterized.typeArguments().add(ast.newSimpleType(ast.newSimpleName(parameters[index].getName())));
type = parameterized;
} else
type = ast.newSimpleType(newNameNode);
newClassCreation.setType(type);
copyArguments(rewrite, newClassCreation);
addArgumentsForLocalsUsedInInnerClass(newClassCreation);
addLinkedPosition(KEY_TYPE_NAME, newNameNode, rewrite.getASTRewrite(), true);
return newClassCreation;
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class ExtractToNullCheckedLocalProposal method newType.
/**
* Create a fresh type reference
* @param typeBinding the type we want to refer to
* @param ast AST for creating new nodes
* @param imports use this for optimal type names
* @return a fully features non-null type reference (can be parameterized and/or array).
*/
public static Type newType(ITypeBinding typeBinding, AST ast, ImportRewrite imports) {
// unwrap array type:
int dimensions = typeBinding.getDimensions();
if (dimensions > 0)
typeBinding = typeBinding.getElementType();
// unwrap parameterized type:
ITypeBinding[] typeArguments = typeBinding.getTypeArguments();
typeBinding = typeBinding.getErasure();
// create leaf type:
Type elementType = (typeBinding.isPrimitive()) ? ast.newPrimitiveType(PrimitiveType.toCode(typeBinding.getName())) : ast.newSimpleType(ast.newName(imports.addImport(typeBinding)));
// re-wrap as parameterized type:
if (typeArguments.length > 0) {
ParameterizedType parameterizedType = ast.newParameterizedType(elementType);
for (ITypeBinding typeArgument : typeArguments) parameterizedType.typeArguments().add(newType(typeArgument, ast, imports));
elementType = parameterizedType;
}
// re-wrap as array type:
if (dimensions > 0)
return ast.newArrayType(elementType, dimensions);
else
return elementType;
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class AbstractSerialVersionOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel positionGroups) throws CoreException {
final ASTRewrite rewrite = cuRewrite.getASTRewrite();
VariableDeclarationFragment fragment = null;
for (int i = 0; i < fNodes.length; i++) {
final ASTNode node = fNodes[i];
final AST ast = node.getAST();
fragment = ast.newVariableDeclarationFragment();
fragment.setName(ast.newSimpleName(NAME_FIELD));
final FieldDeclaration declaration = ast.newFieldDeclaration(fragment);
declaration.setType(ast.newPrimitiveType(PrimitiveType.LONG));
declaration.modifiers().addAll(ASTNodeFactory.newModifiers(ast, Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL));
if (!addInitializer(fragment, node))
continue;
if (fragment.getInitializer() != null) {
final TextEditGroup editGroup = createTextEditGroup(FixMessages.SerialVersion_group_description, cuRewrite);
if (node instanceof AbstractTypeDeclaration)
rewrite.getListRewrite(node, ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty()).insertAt(declaration, 0, editGroup);
else if (node instanceof AnonymousClassDeclaration)
rewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
else if (node instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) node;
final ASTNode parent = type.getParent();
if (parent instanceof ClassInstanceCreation) {
final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
final AnonymousClassDeclaration anonymous = creation.getAnonymousClassDeclaration();
if (anonymous != null)
rewrite.getListRewrite(anonymous, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY).insertAt(declaration, 0, editGroup);
}
} else
Assert.isTrue(false);
addLinkedPositions(rewrite, fragment, positionGroups);
}
final String comment = CodeGeneration.getFieldComment(fUnit, declaration.getType().toString(), NAME_FIELD, StubUtility.getLineDelimiterUsed(fUnit));
if (comment != null && comment.length() > 0) {
final Javadoc doc = (Javadoc) rewrite.createStringPlaceholder(comment, ASTNode.JAVADOC);
declaration.setJavadoc(doc);
}
}
if (fragment == null)
return;
positionGroups.setEndPosition(rewrite.track(fragment));
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class Java50Fix method createRawTypeReferenceOperations.
private static SimpleType createRawTypeReferenceOperations(CompilationUnit compilationUnit, IProblemLocation[] locations, List<CompilationUnitRewriteOperation> operations) {
if (hasFatalError(compilationUnit))
return null;
List<SimpleType> result = new ArrayList<SimpleType>();
for (int i = 0; i < locations.length; i++) {
IProblemLocation problem = locations[i];
if (isRawTypeReferenceProblem(problem.getProblemId())) {
ASTNode node = problem.getCoveredNode(compilationUnit);
if (node instanceof ClassInstanceCreation) {
Type rawReference = (Type) node.getStructuralProperty(ClassInstanceCreation.TYPE_PROPERTY);
if (isRawTypeReference(rawReference)) {
result.add((SimpleType) rawReference);
}
} else if (node instanceof SimpleName) {
ASTNode rawReference = node.getParent();
if (isRawTypeReference(rawReference)) {
ASTNode parent = rawReference.getParent();
if (!(parent instanceof ArrayType || parent instanceof ParameterizedType))
result.add((SimpleType) rawReference);
}
} else if (node instanceof MethodInvocation) {
MethodInvocation invocation = (MethodInvocation) node;
SimpleType rawReference = getRawReference(invocation, compilationUnit);
if (rawReference != null) {
result.add(rawReference);
}
}
}
}
if (result.size() == 0)
return null;
SimpleType[] types = result.toArray(new SimpleType[result.size()]);
operations.add(new AddTypeParametersOperation(types));
return types[0];
}
use of org.eclipse.jdt.core.dom.ParameterizedType in project che by eclipse.
the class PotentialProgrammingProblemsFix method getSelectedName.
private static SimpleName getSelectedName(CompilationUnit compilationUnit, IProblemLocation problem) {
final ASTNode selection = problem.getCoveredNode(compilationUnit);
if (selection == null)
return null;
Name name = null;
if (selection instanceof SimpleType) {
name = ((SimpleType) selection).getName();
} else if (selection instanceof NameQualifiedType) {
name = ((NameQualifiedType) selection).getName();
} else if (selection instanceof QualifiedType) {
name = ((QualifiedType) selection).getName();
} else if (selection instanceof ParameterizedType) {
final ParameterizedType type = (ParameterizedType) selection;
final Type raw = type.getType();
if (raw instanceof SimpleType)
name = ((SimpleType) raw).getName();
else if (raw instanceof NameQualifiedType)
name = ((NameQualifiedType) raw).getName();
else if (raw instanceof QualifiedType)
name = ((QualifiedType) raw).getName();
} else if (selection instanceof Name) {
name = (Name) selection;
}
if (name == null)
return null;
if (name.isSimpleName()) {
return (SimpleName) name;
} else {
return ((QualifiedName) name).getName();
}
}
Aggregations