use of org.eclipse.jdt.core.dom.SimpleType in project che by eclipse.
the class UnresolvedElementsSubProcessor method addSimilarTypeProposals.
private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
SimilarElement[] elements = SimilarElementsRequestor.findSimilarElement(cu, node, kind);
// try to resolve type in context -> highest severity
String resolvedTypeName = null;
ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
if (binding != null) {
ITypeBinding simpleBinding = binding;
if (simpleBinding.isArray()) {
simpleBinding = simpleBinding.getElementType();
}
simpleBinding = simpleBinding.getTypeDeclaration();
if (!simpleBinding.isRecovered()) {
resolvedTypeName = simpleBinding.getQualifiedName();
CUCorrectionProposal proposal = createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
proposals.add(proposal);
if (proposal instanceof AddImportCorrectionProposal)
proposal.setRelevance(relevance + elements.length + 2);
if (binding.isParameterizedType() && (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType) && !(node.getParent().getParent() instanceof Type)) {
proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
}
}
} else {
ASTNode normalizedNode = ASTNodes.getNormalizedNode(node);
if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
ITypeBinding normBinding = ASTResolving.guessBindingForTypeReference(normalizedNode);
if (normBinding != null && !normBinding.isRecovered()) {
proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
}
}
}
// add all similar elements
for (int i = 0; i < elements.length; i++) {
SimilarElement elem = elements[i];
if ((elem.getKind() & SimilarElementsRequestor.ALL_TYPES) != 0) {
String fullName = elem.getName();
if (!fullName.equals(resolvedTypeName)) {
proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
}
}
}
}
use of org.eclipse.jdt.core.dom.SimpleType in project che by eclipse.
the class Java50Fix method getRawReference.
private static SimpleType getRawReference(SimpleName name, CompilationUnit compilationUnit) {
SimpleName[] names = LinkedNodeFinder.findByNode(compilationUnit, name);
for (int j = 0; j < names.length; j++) {
if (names[j].getParent() instanceof VariableDeclarationFragment) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) names[j].getParent();
if (fragment.getParent() instanceof VariableDeclarationStatement) {
VariableDeclarationStatement statement = (VariableDeclarationStatement) fragment.getParent();
ASTNode result = (ASTNode) statement.getStructuralProperty(VariableDeclarationStatement.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (fragment.getParent() instanceof FieldDeclaration) {
FieldDeclaration declaration = (FieldDeclaration) fragment.getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(FieldDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
} else if (names[j].getParent() instanceof SingleVariableDeclaration) {
SingleVariableDeclaration declaration = (SingleVariableDeclaration) names[j].getParent();
ASTNode result = (ASTNode) declaration.getStructuralProperty(SingleVariableDeclaration.TYPE_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
} else if (names[j].getParent() instanceof MethodDeclaration) {
MethodDeclaration methodDecl = (MethodDeclaration) names[j].getParent();
ASTNode result = (ASTNode) methodDecl.getStructuralProperty(MethodDeclaration.RETURN_TYPE2_PROPERTY);
if (isRawTypeReference(result))
return (SimpleType) result;
}
}
return null;
}
use of org.eclipse.jdt.core.dom.SimpleType in project che by eclipse.
the class Java50Fix method createRawTypeReferenceFix.
public static Java50Fix createRawTypeReferenceFix(CompilationUnit compilationUnit, IProblemLocation problem) {
List<CompilationUnitRewriteOperation> operations = new ArrayList<CompilationUnitRewriteOperation>();
SimpleType node = createRawTypeReferenceOperations(compilationUnit, new IProblemLocation[] { problem }, operations);
if (operations.size() == 0)
return null;
return new Java50Fix(Messages.format(FixMessages.Java50Fix_AddTypeArguments_description, BasicElementLabels.getJavaElementName(node.getName().getFullyQualifiedName())), compilationUnit, operations.toArray(new CompilationUnitRewriteOperation[operations.size()]));
}
use of org.eclipse.jdt.core.dom.SimpleType in project flux by eclipse.
the class ASTNodes method getTypeName.
/**
* Returns the simple name of the type, followed by array dimensions.
* Skips qualifiers, type arguments, and type annotations.
* <p>
* Does <b>not</b> work for WildcardTypes, etc.!
*
* @param type a type that has a simple name
* @return the simple name, followed by array dimensions
* @see #getSimpleNameIdentifier(Name)
* @since 3.10
*/
public static String getTypeName(Type type) {
final StringBuffer buffer = new StringBuffer();
ASTVisitor visitor = new ASTVisitor() {
@Override
public boolean visit(PrimitiveType node) {
buffer.append(node.getPrimitiveTypeCode().toString());
return false;
}
@Override
public boolean visit(SimpleType node) {
buffer.append(getSimpleNameIdentifier(node.getName()));
return false;
}
@Override
public boolean visit(QualifiedType node) {
buffer.append(node.getName().getIdentifier());
return false;
}
@Override
public boolean visit(NameQualifiedType node) {
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.SimpleType in project flux by eclipse.
the class QuickAssistProcessor method getConvertToStringBufferProposal.
private static LinkedCorrectionProposal getConvertToStringBufferProposal(IInvocationContext context, AST ast, InfixExpression oldInfixExpression) {
String bufferOrBuilderName;
ICompilationUnit cu = context.getCompilationUnit();
if (JavaModelUtil.is50OrHigher(cu.getJavaProject())) {
//$NON-NLS-1$
bufferOrBuilderName = "StringBuilder";
} else {
//$NON-NLS-1$
bufferOrBuilderName = "StringBuffer";
}
ASTRewrite rewrite = ASTRewrite.create(ast);
SimpleName existingBuffer = getEnclosingAppendBuffer(oldInfixExpression);
String mechanismName = BasicElementLabels.getJavaElementName(existingBuffer == null ? bufferOrBuilderName : existingBuffer.getIdentifier());
String label = Messages.format(CorrectionMessages.QuickAssistProcessor_convert_to_string_buffer_description, mechanismName);
//Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, cu, rewrite, IProposalRelevance.CONVERT_TO_STRING_BUFFER);
proposal.setCommandId(CONVERT_TO_STRING_BUFFER_ID);
Statement insertAfter;
String bufferName;
//$NON-NLS-1$
String groupID = "nameId";
ListRewrite listRewrite;
Statement enclosingStatement = ASTResolving.findParentStatement(oldInfixExpression);
if (existingBuffer != null) {
if (ASTNodes.isControlStatementBody(enclosingStatement.getLocationInParent())) {
Block newBlock = ast.newBlock();
listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
insertAfter = null;
rewrite.replace(enclosingStatement, newBlock, null);
} else {
listRewrite = rewrite.getListRewrite(enclosingStatement.getParent(), (ChildListPropertyDescriptor) enclosingStatement.getLocationInParent());
insertAfter = enclosingStatement;
}
bufferName = existingBuffer.getIdentifier();
} else {
// create buffer
VariableDeclarationFragment frag = ast.newVariableDeclarationFragment();
// check if name is already in use and provide alternative
List<String> fExcludedVariableNames = Arrays.asList(ASTResolving.getUsedVariableNames(oldInfixExpression));
SimpleType bufferType = ast.newSimpleType(ast.newName(bufferOrBuilderName));
ClassInstanceCreation newBufferExpression = ast.newClassInstanceCreation();
//StubUtility.getVariableNameSuggestions(NamingConventions.VK_LOCAL, cu.getJavaProject(), bufferOrBuilderName, 0, fExcludedVariableNames, true);
String[] newBufferNames = new String[] {};
bufferName = newBufferNames[0];
SimpleName bufferNameDeclaration = ast.newSimpleName(bufferName);
frag.setName(bufferNameDeclaration);
proposal.addLinkedPosition(rewrite.track(bufferNameDeclaration), true, groupID);
for (int i = 0; i < newBufferNames.length; i++) {
proposal.addLinkedPositionProposal(groupID, newBufferNames[i], null);
}
newBufferExpression.setType(bufferType);
frag.setInitializer(newBufferExpression);
VariableDeclarationStatement bufferDeclaration = ast.newVariableDeclarationStatement(frag);
bufferDeclaration.setType(ast.newSimpleType(ast.newName(bufferOrBuilderName)));
insertAfter = bufferDeclaration;
Statement statement = ASTResolving.findParentStatement(oldInfixExpression);
if (ASTNodes.isControlStatementBody(statement.getLocationInParent())) {
Block newBlock = ast.newBlock();
listRewrite = rewrite.getListRewrite(newBlock, Block.STATEMENTS_PROPERTY);
listRewrite.insertFirst(bufferDeclaration, null);
listRewrite.insertLast(rewrite.createMoveTarget(statement), null);
rewrite.replace(statement, newBlock, null);
} else {
listRewrite = rewrite.getListRewrite(statement.getParent(), (ChildListPropertyDescriptor) statement.getLocationInParent());
listRewrite.insertBefore(bufferDeclaration, statement, null);
}
}
List<Expression> operands = new ArrayList<Expression>();
collectInfixPlusOperands(oldInfixExpression, operands);
Statement lastAppend = insertAfter;
for (Iterator<Expression> iter = operands.iterator(); iter.hasNext(); ) {
Expression operand = iter.next();
MethodInvocation appendIncovationExpression = ast.newMethodInvocation();
//$NON-NLS-1$
appendIncovationExpression.setName(ast.newSimpleName("append"));
SimpleName bufferNameReference = ast.newSimpleName(bufferName);
// If there was an existing name, don't offer to rename it
if (existingBuffer == null) {
proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
}
appendIncovationExpression.setExpression(bufferNameReference);
appendIncovationExpression.arguments().add(rewrite.createCopyTarget(operand));
ExpressionStatement appendExpressionStatement = ast.newExpressionStatement(appendIncovationExpression);
if (lastAppend == null) {
listRewrite.insertFirst(appendExpressionStatement, null);
} else {
listRewrite.insertAfter(appendExpressionStatement, lastAppend, null);
}
lastAppend = appendExpressionStatement;
}
if (existingBuffer != null) {
proposal.setEndPosition(rewrite.track(lastAppend));
if (insertAfter != null) {
rewrite.remove(enclosingStatement, null);
}
} else {
// replace old expression with toString
MethodInvocation bufferToString = ast.newMethodInvocation();
//$NON-NLS-1$
bufferToString.setName(ast.newSimpleName("toString"));
SimpleName bufferNameReference = ast.newSimpleName(bufferName);
bufferToString.setExpression(bufferNameReference);
proposal.addLinkedPosition(rewrite.track(bufferNameReference), true, groupID);
rewrite.replace(oldInfixExpression, bufferToString, null);
proposal.setEndPosition(rewrite.track(bufferToString));
}
return proposal;
}
Aggregations