use of org.eclipse.jdt.core.dom.NormalAnnotation in project che by eclipse.
the class MissingAnnotationAttributesProposal method addMissingAtributes.
private void addMissingAtributes(ITypeBinding binding, ListRewrite listRewriter) {
Set<String> implementedAttribs = new HashSet<String>();
if (fAnnotation instanceof NormalAnnotation) {
List<MemberValuePair> list = ((NormalAnnotation) fAnnotation).values();
for (int i = 0; i < list.size(); i++) {
MemberValuePair curr = list.get(i);
implementedAttribs.add(curr.getName().getIdentifier());
}
} else if (fAnnotation instanceof SingleMemberAnnotation) {
//$NON-NLS-1$
implementedAttribs.add("value");
}
ASTRewrite rewriter = listRewriter.getASTRewrite();
AST ast = rewriter.getAST();
ImportRewriteContext context = null;
ASTNode bodyDeclaration = ASTResolving.findParentBodyDeclaration(listRewriter.getParent());
if (bodyDeclaration != null) {
context = new ContextSensitiveImportRewriteContext(bodyDeclaration, getImportRewrite());
}
IMethodBinding[] declaredMethods = binding.getDeclaredMethods();
for (int i = 0; i < declaredMethods.length; i++) {
IMethodBinding curr = declaredMethods[i];
if (!implementedAttribs.contains(curr.getName()) && curr.getDefaultValue() == null) {
MemberValuePair pair = ast.newMemberValuePair();
pair.setName(ast.newSimpleName(curr.getName()));
pair.setValue(newDefaultExpression(ast, curr.getReturnType(), context));
listRewriter.insertLast(pair, null);
//$NON-NLS-1$
addLinkedPosition(rewriter.track(pair.getName()), false, "val_name_" + i);
//$NON-NLS-1$
addLinkedPosition(rewriter.track(pair.getValue()), false, "val_type_" + i);
}
}
}
use of org.eclipse.jdt.core.dom.NormalAnnotation in project che by eclipse.
the class MissingAnnotationAttributesProposal method getRewrite.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
*/
@Override
protected ASTRewrite getRewrite() throws CoreException {
AST ast = fAnnotation.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
createImportRewrite((CompilationUnit) fAnnotation.getRoot());
ListRewrite listRewrite;
if (fAnnotation instanceof NormalAnnotation) {
listRewrite = rewrite.getListRewrite(fAnnotation, NormalAnnotation.VALUES_PROPERTY);
} else {
NormalAnnotation newAnnotation = ast.newNormalAnnotation();
newAnnotation.setTypeName((Name) rewrite.createMoveTarget(fAnnotation.getTypeName()));
rewrite.replace(fAnnotation, newAnnotation, null);
listRewrite = rewrite.getListRewrite(newAnnotation, NormalAnnotation.VALUES_PROPERTY);
}
addMissingAtributes(fAnnotation.resolveTypeBinding(), listRewrite);
return rewrite;
}
use of org.eclipse.jdt.core.dom.NormalAnnotation in project che by eclipse.
the class SuppressWarningsSubProcessor method addRemoveUnusedSuppressWarningProposals.
public static void addRemoveUnusedSuppressWarningProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ASTNode coveringNode = problem.getCoveringNode(context.getASTRoot());
if (!(coveringNode instanceof StringLiteral))
return;
StringLiteral literal = (StringLiteral) coveringNode;
if (coveringNode.getParent() instanceof MemberValuePair) {
coveringNode = coveringNode.getParent();
}
ASTNode parent = coveringNode.getParent();
ASTRewrite rewrite = ASTRewrite.create(coveringNode.getAST());
if (parent instanceof SingleMemberAnnotation) {
rewrite.remove(parent, null);
} else if (parent instanceof NormalAnnotation) {
NormalAnnotation annot = (NormalAnnotation) parent;
if (annot.values().size() == 1) {
rewrite.remove(annot, null);
} else {
rewrite.remove(coveringNode, null);
}
} else if (parent instanceof ArrayInitializer) {
rewrite.remove(coveringNode, null);
} else {
return;
}
String label = Messages.format(CorrectionMessages.SuppressWarningsSubProcessor_remove_annotation_label, literal.getLiteralValue());
//JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ANNOTATION, image);
proposals.add(proposal);
}
use of org.eclipse.jdt.core.dom.NormalAnnotation in project fabric8 by jboss-fuse.
the class AddSwaggerAnnotationMojo method addSwaggerApiAnnotation.
private void addSwaggerApiAnnotation(CompilationUnit unit, AnnotationVisitor visitor, File file, Document document) throws JavaModelException, IllegalArgumentException, MalformedTreeException, BadLocationException, IOException {
AST ast = unit.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
ListRewrite listRewrite = rewriter.getListRewrite(unit, CompilationUnit.TYPES_PROPERTY);
NormalAnnotation normalAnnotation = rewriter.getAST().newNormalAnnotation();
Name name = ast.newName("com.wordnik.swagger.annotations.Api");
normalAnnotation.setTypeName(name);
MemberValuePair memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
StringLiteral stringLiteral = ast.newStringLiteral();
String rootPath = visitor.getRootPath();
rootPath = rootPath.substring(1, rootPath.length() - 1);
if (rootPath.endsWith("/")) {
rootPath = rootPath.substring(0, rootPath.length() - 1);
}
stringLiteral.setLiteralValue(rootPath);
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("description"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue("Operations about " + visitor.getRestServiceClass());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
listRewrite.insertAt(normalAnnotation, 0, null);
for (MethodDeclaration method : visitor.getRestMethod()) {
listRewrite = rewriter.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY);
normalAnnotation = rewriter.getAST().newNormalAnnotation();
name = ast.newName("com.wordnik.swagger.annotations.ApiOperation");
normalAnnotation.setTypeName(name);
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(method.getName().toString());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
Javadoc doc = method.getJavadoc();
String comment = null;
if (doc != null) {
comment = method.getJavadoc().toString();
}
if (comment != null && comment.length() > 0) {
// add notes from method java doc
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("notes"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(comment);
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
}
listRewrite.insertAt(normalAnnotation, 0, null);
listRewrite = rewriter.getListRewrite((ASTNode) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0), SingleVariableDeclaration.MODIFIERS2_PROPERTY);
normalAnnotation = rewriter.getAST().newNormalAnnotation();
name = ast.newName("com.wordnik.swagger.annotations.ApiParam");
normalAnnotation.setTypeName(name);
((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName();
memberValuePair = ast.newMemberValuePair();
memberValuePair.setName(ast.newSimpleName("value"));
stringLiteral = ast.newStringLiteral();
stringLiteral.setLiteralValue(((VariableDeclaration) ((List) method.getStructuralProperty(MethodDeclaration.PARAMETERS_PROPERTY)).get(0)).getName().toString());
memberValuePair.setValue(stringLiteral);
normalAnnotation.values().add(memberValuePair);
listRewrite.insertAt(normalAnnotation, 0, null);
}
TextEdit edits = rewriter.rewriteAST(document, null);
edits.apply(document);
FileUtils.writeStringToFile(file, document.get());
}
use of org.eclipse.jdt.core.dom.NormalAnnotation in project eclipse.jdt.ls by eclipse.
the class UnresolvedElementsSubProcessor method getAnnotationMemberProposals.
public static void getAnnotationMemberProposals(IInvocationContext context, IProblemLocation problem, Collection<CUCorrectionProposal> 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));
proposals.add(new NewAnnotationMemberProposal(label, targetCU, selectedNode, annotBinding, IProposalRelevance.CREATE_ATTRIBUTE));
}
}
}
Aggregations