use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class UnresolvedElementsSubProcessor method getAnnotationMemberProposals.
public static void getAnnotationMemberProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
CompilationUnit astRoot = context.getASTRoot();
ICompilationUnit cu = context.getCompilationUnit();
ASTNode selectedNode = problem.getCoveringNode(astRoot);
Annotation annotation;
String memberName;
if (selectedNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
if (selectedNode.getParent().getLocationInParent() != NormalAnnotation.VALUES_PROPERTY) {
return;
}
annotation = (Annotation) selectedNode.getParent().getParent();
memberName = ((SimpleName) selectedNode).getIdentifier();
} else if (selectedNode.getLocationInParent() == SingleMemberAnnotation.VALUE_PROPERTY) {
annotation = (Annotation) selectedNode.getParent();
//$NON-NLS-1$
memberName = "value";
} else {
return;
}
ITypeBinding annotBinding = annotation.resolveTypeBinding();
if (annotBinding == null) {
return;
}
if (annotation instanceof NormalAnnotation) {
// similar names
IMethodBinding[] otherMembers = annotBinding.getDeclaredMethods();
for (int i = 0; i < otherMembers.length; i++) {
IMethodBinding binding = otherMembers[i];
String curr = binding.getName();
int relevance = NameMatcher.isSimilarName(memberName, curr) ? IProposalRelevance.CHANGE_TO_ATTRIBUTE_SIMILAR_NAME : IProposalRelevance.CHANGE_TO_ATTRIBUTE;
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_UnresolvedElementsSubProcessor_changetoattribute_description, BasicElementLabels.getJavaElementName(curr));
proposals.add(new RenameNodeCorrectionProposal(label, cu, problem.getOffset(), problem.getLength(), curr, relevance));
}
}
if (annotBinding.isFromSource()) {
ICompilationUnit targetCU = ASTResolving.findCompilationUnitForBinding(cu, astRoot, annotBinding);
if (targetCU != null) {
String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_UnresolvedElementsSubProcessor_createattribute_description, BasicElementLabels.getJavaElementName(memberName));
Image image = JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC);
proposals.add(new NewAnnotationMemberProposal(label, targetCU, selectedNode, annotBinding, IProposalRelevance.CREATE_ATTRIBUTE, image));
}
}
}
use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class AddTypeParameterProposal method getRewrite.
@Override
protected ASTRewrite getRewrite() throws CoreException {
ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
ASTNode declNode = null;
if (boundNode != null) {
// is same CU
declNode = boundNode;
createImportRewrite(fAstRoot);
} else {
CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode = newRoot.findDeclaringNode(fBinding.getKey());
createImportRewrite(newRoot);
}
AST ast = declNode.getAST();
TypeParameter newTypeParam = ast.newTypeParameter();
newTypeParam.setName(ast.newSimpleName(fTypeParamName));
if (fBounds != null && fBounds.length > 0) {
List<Type> typeBounds = newTypeParam.typeBounds();
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, getImportRewrite());
for (int i = 0; i < fBounds.length; i++) {
Type newBound = getImportRewrite().addImport(fBounds[i], ast, importRewriteContext);
typeBounds.add(newBound);
}
}
ASTRewrite rewrite = ASTRewrite.create(ast);
ListRewrite listRewrite;
Javadoc javadoc;
List<TypeParameter> otherTypeParams;
if (declNode instanceof TypeDeclaration) {
TypeDeclaration declaration = (TypeDeclaration) declNode;
listRewrite = rewrite.getListRewrite(declaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
otherTypeParams = declaration.typeParameters();
javadoc = declaration.getJavadoc();
} else {
MethodDeclaration declaration = (MethodDeclaration) declNode;
listRewrite = rewrite.getListRewrite(declNode, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
otherTypeParams = declaration.typeParameters();
javadoc = declaration.getJavadoc();
}
listRewrite.insertLast(newTypeParam, null);
if (javadoc != null && otherTypeParams != null) {
ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
Set<String> previousNames = JavadocTagsSubProcessor.getPreviousTypeParamNames(otherTypeParams, null);
String name = '<' + fTypeParamName + '>';
TagElement newTag = ast.newTagElement();
newTag.setTagName(TagElement.TAG_PARAM);
TextElement text = ast.newTextElement();
text.setText(name);
newTag.fragments().add(text);
JavadocTagsSubProcessor.insertTag(tagsRewriter, newTag, previousNames);
}
return rewrite;
}
use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class LocalCorrectionsQuickFixTest17 method testRemoveRedundantTypeArguments2.
@Test
public void testRemoveRedundantTypeArguments2() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.util.HashMap;\n");
buf.append("import java.util.Map;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" Map<String,String> a = new HashMap<String,String>();\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
CompilationUnit astRoot = getASTRoot(cu);
ArrayList proposals = collectCorrections(cu, astRoot);
assertNumberOfProposals(proposals, 3);
assertCorrectLabels(proposals);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.util.HashMap;\n");
buf.append("import java.util.Map;\n");
buf.append("public class E {\n");
buf.append(" void foo() {\n");
buf.append(" Map<String,String> a = new HashMap<>();\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
assertExpectedExistInProposals(proposals, new String[] { expected1 });
}
use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionTryWithResources4.
@Test
public void testUncaughtExceptionTryWithResources4() throws Exception {
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=351464
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.io.FileInputStream;\n");
buf.append("import java.io.IOException;\n");
buf.append("class MyException extends Exception {\n");
buf.append(" static final long serialVersionUID = 1L;\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar() throws MyException {\n");
buf.append(" throw new MyException();\n");
buf.append(" }\n");
buf.append(" void foo(String name, boolean b) throws IOException {\n");
buf.append(" try (FileInputStream fis = new FileInputStream(name)) {\n");
buf.append(" bar();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
CompilationUnit astRoot = getASTRoot(cu);
ArrayList proposals = collectCorrections(cu, astRoot);
assertNumberOfProposals(proposals, 3);
assertCorrectLabels(proposals);
CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
String preview1 = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.io.FileInputStream;\n");
buf.append("import java.io.IOException;\n");
buf.append("class MyException extends Exception {\n");
buf.append(" static final long serialVersionUID = 1L;\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar() throws MyException {\n");
buf.append(" throw new MyException();\n");
buf.append(" }\n");
buf.append(" void foo(String name, boolean b) throws IOException, MyException {\n");
buf.append(" try (FileInputStream fis = new FileInputStream(name)) {\n");
buf.append(" bar();\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
proposal = (CUCorrectionProposal) proposals.get(1);
String preview2 = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.io.FileInputStream;\n");
buf.append("import java.io.IOException;\n");
buf.append("class MyException extends Exception {\n");
buf.append(" static final long serialVersionUID = 1L;\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar() throws MyException {\n");
buf.append(" throw new MyException();\n");
buf.append(" }\n");
buf.append(" void foo(String name, boolean b) throws IOException {\n");
buf.append(" try (FileInputStream fis = new FileInputStream(name)) {\n");
buf.append(" bar();\n");
buf.append(" } catch (MyException e) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected2 = buf.toString();
proposal = (CUCorrectionProposal) proposals.get(2);
String preview3 = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.io.FileInputStream;\n");
buf.append("import java.io.IOException;\n");
buf.append("class MyException extends Exception {\n");
buf.append(" static final long serialVersionUID = 1L;\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar() throws MyException {\n");
buf.append(" throw new MyException();\n");
buf.append(" }\n");
buf.append(" void foo(String name, boolean b) throws IOException {\n");
buf.append(" try (FileInputStream fis = new FileInputStream(name)) {\n");
buf.append(" try {\n");
buf.append(" bar();\n");
buf.append(" } catch (MyException e) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected3 = buf.toString();
assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3 }, new String[] { expected1, expected2, expected3 });
}
use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class LocalCorrectionsQuickFixTest17 method testUncaughtExceptionTryWithResources1.
@Test
public void testUncaughtExceptionTryWithResources1() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuffer buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.io.FileInputStream;\n");
buf.append("class MyException extends Exception {\n");
buf.append(" static final long serialVersionUID = 1L;\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar(int n) throws IllegalArgumentException, MyException {\n");
buf.append(" if (n == 1)\n");
buf.append(" throw new IllegalArgumentException();\n");
buf.append(" else\n");
buf.append(" throw new MyException();\n");
buf.append(" }\n");
buf.append(" void foo(String name, boolean b) {\n");
buf.append(" try (FileInputStream fis = new FileInputStream(name)) {\n");
buf.append(" bar(1);\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
CompilationUnit astRoot = getASTRoot(cu);
//quick fix on 1st problem
ArrayList proposals = collectCorrections(cu, astRoot, 3, 0);
assertNumberOfProposals(proposals, 3);
assertCorrectLabels(proposals);
CUCorrectionProposal proposal = (CUCorrectionProposal) proposals.get(0);
String preview1 = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.io.FileInputStream;\n");
buf.append("import java.io.FileNotFoundException;\n");
buf.append("import java.io.IOException;\n");
buf.append("class MyException extends Exception {\n");
buf.append(" static final long serialVersionUID = 1L;\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar(int n) throws IllegalArgumentException, MyException {\n");
buf.append(" if (n == 1)\n");
buf.append(" throw new IllegalArgumentException();\n");
buf.append(" else\n");
buf.append(" throw new MyException();\n");
buf.append(" }\n");
buf.append(" void foo(String name, boolean b) throws FileNotFoundException, IOException {\n");
buf.append(" try (FileInputStream fis = new FileInputStream(name)) {\n");
buf.append(" bar(1);\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected1 = buf.toString();
proposal = (CUCorrectionProposal) proposals.get(1);
String preview2 = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.io.FileInputStream;\n");
buf.append("import java.io.FileNotFoundException;\n");
buf.append("import java.io.IOException;\n");
buf.append("class MyException extends Exception {\n");
buf.append(" static final long serialVersionUID = 1L;\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar(int n) throws IllegalArgumentException, MyException {\n");
buf.append(" if (n == 1)\n");
buf.append(" throw new IllegalArgumentException();\n");
buf.append(" else\n");
buf.append(" throw new MyException();\n");
buf.append(" }\n");
buf.append(" void foo(String name, boolean b) {\n");
buf.append(" try (FileInputStream fis = new FileInputStream(name)) {\n");
buf.append(" bar(1);\n");
buf.append(" } catch (FileNotFoundException e) {\n");
buf.append(" } catch (IOException e) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected2 = buf.toString();
proposal = (CUCorrectionProposal) proposals.get(2);
String preview3 = getPreviewContent(proposal);
buf = new StringBuffer();
buf.append("package test1;\n");
buf.append("import java.io.FileInputStream;\n");
buf.append("import java.io.FileNotFoundException;\n");
buf.append("import java.io.IOException;\n");
buf.append("class MyException extends Exception {\n");
buf.append(" static final long serialVersionUID = 1L;\n");
buf.append("}\n");
buf.append("public class E {\n");
buf.append(" void bar(int n) throws IllegalArgumentException, MyException {\n");
buf.append(" if (n == 1)\n");
buf.append(" throw new IllegalArgumentException();\n");
buf.append(" else\n");
buf.append(" throw new MyException();\n");
buf.append(" }\n");
buf.append(" void foo(String name, boolean b) {\n");
buf.append(" try (FileInputStream fis = new FileInputStream(name)) {\n");
buf.append(" bar(1);\n");
buf.append(" } catch (FileNotFoundException | IOException e) {\n");
buf.append(" }\n");
buf.append(" }\n");
buf.append("}\n");
String expected3 = buf.toString();
assertEqualStringsIgnoreOrder(new String[] { preview1, preview2, preview3 }, new String[] { expected1, expected2, expected3 });
}
Aggregations