下面列出了怎么用org.eclipse.jdt.core.dom.NormalAnnotation的API类实例代码及写法,或者点击链接到github查看源代码。
public UMLAnnotation(CompilationUnit cu, String filePath, Annotation annotation) {
this.typeName = annotation.getTypeName().getFullyQualifiedName();
this.locationInfo = new LocationInfo(cu, filePath, annotation, CodeElementType.ANNOTATION);
if(annotation instanceof SingleMemberAnnotation) {
SingleMemberAnnotation singleMemberAnnotation = (SingleMemberAnnotation)annotation;
this.value = new AbstractExpression(cu, filePath, singleMemberAnnotation.getValue(), CodeElementType.SINGLE_MEMBER_ANNOTATION_VALUE);
}
else if(annotation instanceof NormalAnnotation) {
NormalAnnotation normalAnnotation = (NormalAnnotation)annotation;
List<MemberValuePair> pairs = normalAnnotation.values();
for(MemberValuePair pair : pairs) {
AbstractExpression value = new AbstractExpression(cu, filePath, pair.getValue(), CodeElementType.NORMAL_ANNOTATION_MEMBER_VALUE_PAIR);
memberValuePairs.put(pair.getName().getIdentifier(), value);
}
}
}
private static Annotation findExistingAnnotation(List<? extends ASTNode> modifiers) {
for (int i= 0, len= modifiers.size(); i < len; i++) {
Object curr= modifiers.get(i);
if (curr instanceof NormalAnnotation || curr instanceof SingleMemberAnnotation) {
Annotation annotation= (Annotation) curr;
ITypeBinding typeBinding= annotation.resolveTypeBinding();
if (typeBinding != null) {
if ("java.lang.SuppressWarnings".equals(typeBinding.getQualifiedName())) { //$NON-NLS-1$
return annotation;
}
} else {
String fullyQualifiedName= annotation.getTypeName().getFullyQualifiedName();
if ("SuppressWarnings".equals(fullyQualifiedName) || "java.lang.SuppressWarnings".equals(fullyQualifiedName)) { //$NON-NLS-1$ //$NON-NLS-2$
return annotation;
}
}
}
}
return null;
}
@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;
}
public boolean visit(NormalAnnotation annotation) {
/*
* NormalAnnotation: @ TypeName ( [ MemberValuePair { , MemberValuePair } ] )
*/
activateDiffStyle(annotation);
appendAtSign();
handleExpression(annotation.getTypeName());
appendOpenParenthesis();
for(int i=0; i<annotation.values().size(); i++) {
visit((MemberValuePair) annotation.values().get(i));
if(i < annotation.values().size() - 1) {
appendComma();
}
}
appendClosedParenthesis();
deactivateDiffStyle(annotation);
return false;
}
public void addJsonDeserializeAnnotation(CompilationUnitModificationDomain compilationUnitModificationDomain, TypeDeclaration builderType) {
AST ast = compilationUnitModificationDomain.getAst();
ASTRewrite rewriter = compilationUnitModificationDomain.getAstRewriter();
ListRewrite modifierRewrite = rewriter.getListRewrite(compilationUnitModificationDomain.getOriginalType(), TypeDeclaration.MODIFIERS2_PROPERTY);
NormalAnnotation annotation = createAnnotation(ast, compilationUnitModificationDomain, builderType);
modifierRewrite.insertFirst(annotation, null);
importRepository.addImport(StaticPreferences.JSON_DESERIALIZE_FULLY_QUALIFIED_NAME);
}
private NormalAnnotation createAnnotation(AST ast, CompilationUnitModificationDomain compilationUnitModificationDomain, TypeDeclaration builderType) {
TypeLiteral typeLiteral = createBuilderClassReferenceLiteral(ast, compilationUnitModificationDomain, builderType);
NormalAnnotation jsonDeserializeAnnotation = ast.newNormalAnnotation();
jsonDeserializeAnnotation.setTypeName(ast.newSimpleName(JSON_DESERIALIZE_CLASS_NAME));
MemberValuePair builderAttribute = ast.newMemberValuePair();
builderAttribute.setName(ast.newSimpleName("builder"));
builderAttribute.setValue(typeLiteral);
jsonDeserializeAnnotation.values().add(builderAttribute);
return jsonDeserializeAnnotation;
}
private NormalAnnotation createJsonPojoBuilderAnnotationWithAttributes(AST ast, String buildMethodName, String withMethodPrefix) {
NormalAnnotation annotation = ast.newNormalAnnotation();
annotation.values().add(createAnnotationAttribute(ast, "buildMethodName", buildMethodName));
annotation.values().add(createAnnotationAttribute(ast, "withPrefix", withMethodPrefix));
return annotation;
}
@Override
public void remove(ASTRewrite rewriter, TypeDeclaration mainType) {
if (preferencesManager.getPreferenceValue(ADD_JACKSON_DESERIALIZE_ANNOTATION)) {
((List<IExtendedModifier>) mainType.modifiers())
.stream()
.filter(modifier -> modifier instanceof NormalAnnotation)
.map(modifier -> (NormalAnnotation) modifier)
.filter(annotation -> annotation.getTypeName().toString().equals(JSON_DESERIALIZE_CLASS_NAME))
.filter(annotation -> isBuilderDeserializer(annotation))
.findFirst()
.ifPresent(annotation -> removeAnnotation(annotation, rewriter, mainType));
}
}
private boolean isBuilderDeserializer(NormalAnnotation annotation) {
List<MemberValuePair> values = annotation.values();
if (values.size() != 1) {
return false;
}
return values.get(0)
.getName()
.toString()
.equals("builder");
}
@Override
public void endVisit(NormalAnnotation node) {
if (skipNode(node)) {
return;
}
GenericSequentialFlowInfo info = processSequential(node, node.getTypeName());
process(info, node.values());
}
@Override
public boolean visit(final NormalAnnotation node) {
this.appendToBuffer("@");
node.getTypeName().accept(this);
this.appendToBuffer("(");
this.visitAllSeparatedByComma(node.values());
this.appendToBuffer(")");
return false;
}
/**
* Returns an annotation's value. If the annotation not a single-member
* annotation, this is the value corresponding to the key named "value".
*/
@SuppressWarnings("unchecked")
public static Expression getAnnotationValue(Annotation annotation) {
if (annotation instanceof SingleMemberAnnotation) {
return ((SingleMemberAnnotation) annotation).getValue();
} else if (annotation instanceof NormalAnnotation) {
NormalAnnotation normalAnnotation = (NormalAnnotation) annotation;
for (MemberValuePair pair : (List<MemberValuePair>) normalAnnotation.values()) {
if (pair.getName().getIdentifier().equals("value")) {
return pair.getValue();
}
}
}
return null;
}
@Override
public boolean visit(NormalAnnotation node) {
IAnnotationBinding resolvedAnnotationBinding = node.resolveAnnotationBinding();
processAnnotationBinding(resolvedAnnotationBinding);
// Don't visit this node's children; they don't impact the result
return false;
}
@Override
public void endVisit(NormalAnnotation node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getTypeName());
process(info, node.values());
}
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());
Image image= JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_ANNOTATION, image);
proposals.add(proposal);
}
private void handleModifier(IExtendedModifier extendedModifier) {
if(extendedModifier instanceof Modifier) {
visit((Modifier) extendedModifier);
}
else if(extendedModifier instanceof MarkerAnnotation) {
visit((MarkerAnnotation) extendedModifier);
}
else if(extendedModifier instanceof NormalAnnotation) {
visit((NormalAnnotation) extendedModifier);
}
else if(extendedModifier instanceof SingleMemberAnnotation) {
visit((SingleMemberAnnotation) extendedModifier);
}
}
@Override
public boolean visit(NormalAnnotation node) {
return visitAnnotation(node);
}
private void removeAnnotation(NormalAnnotation annotation, ASTRewrite rewriter, TypeDeclaration mainType) {
ListRewrite modifierRewrite = rewriter.getListRewrite(mainType, TypeDeclaration.MODIFIERS2_PROPERTY);
modifierRewrite.remove(annotation, null);
}
/**
* handles: @ TypeName ( [ MemberValuePair { , MemberValuePair } ] ) see comment
* from {@link #visit(MarkerAnnotation)}
*/
@Override
public boolean visit(NormalAnnotation node) {
addTypeAnnotationSourceAnchor(node);
return true;
}
@Override
public boolean visit(NormalAnnotation node) {
if (node.subtreeMatch(fMatcher, fNodeToMatch))
return matches(node);
return super.visit(node);
}
@Override
public boolean visit(NormalAnnotation node) {
return false;
}
@Override
public void endVisit(NormalAnnotation node) {
endVisitNode(node);
}
@Override
public boolean visit(NormalAnnotation node) {
return visitNode(node);
}
@Override
public boolean visit(NormalAnnotation node) {
typeRefFound(node.getTypeName());
doVisitChildren(node.values());
return false;
}
public boolean visit(NormalAnnotation node) {
if (found(node, node) && this.resolveBinding)
this.foundBinding = node.resolveAnnotationBinding();
return true;
}