下面列出了怎么用org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration的API类实例代码及写法,或者点击链接到github查看源代码。
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
switch (declNode.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.FIELD_DECLARATION:
return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
case ASTNode.SINGLE_VARIABLE_DECLARATION:
return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.TYPE_DECLARATION:
return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ENUM_DECLARATION:
return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ANNOTATION_TYPE_DECLARATION:
return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ENUM_CONSTANT_DECLARATION:
return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
default:
throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
}
}
@Override
public boolean visit(final AnnotationTypeMemberDeclaration node) {
Javadoc _javadoc = node.getJavadoc();
boolean _tripleNotEquals = (_javadoc != null);
if (_tripleNotEquals) {
node.getJavadoc().accept(this);
}
this.appendModifiers(node, node.modifiers());
node.getType().accept(this);
this.appendSpaceToBuffer();
node.getName().accept(this);
Expression _default = node.getDefault();
boolean _tripleNotEquals_1 = (_default != null);
if (_tripleNotEquals_1) {
this.appendToBuffer(" = ");
node.getDefault().accept(this);
}
this.appendLineWrapToBuffer();
return false;
}
private static int getNodeStartPosition(final ASTNode node) {
if (node instanceof MethodDeclaration) {
MethodDeclaration decl = (MethodDeclaration) node;
return decl.isConstructor() ? decl.getName().getStartPosition() : decl.getReturnType2().getStartPosition();
} else if (node instanceof FieldDeclaration) {
return ((FieldDeclaration) node).getType().getStartPosition();
} else if (node instanceof AbstractTypeDeclaration) {
return ((AbstractTypeDeclaration) node).getName().getStartPosition();
} else if (node instanceof AnnotationTypeMemberDeclaration) {
return ((AnnotationTypeMemberDeclaration) node).getName().getStartPosition();
} else if (node instanceof EnumConstantDeclaration) {
return ((EnumConstantDeclaration) node).getName().getStartPosition();
} else if (node instanceof PackageDeclaration) {
return ((PackageDeclaration) node).getName().getStartPosition();
}
/* TODO: Initializer */
return node.getStartPosition();
}
private ListRewrite evaluateListRewrite(ASTRewrite rewrite, ASTNode declNode) {
switch (declNode.getNodeType()) {
case ASTNode.METHOD_DECLARATION:
return rewrite.getListRewrite(declNode, MethodDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.FIELD_DECLARATION:
return rewrite.getListRewrite(declNode, FieldDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.VARIABLE_DECLARATION_EXPRESSION:
return rewrite.getListRewrite(declNode, VariableDeclarationExpression.MODIFIERS2_PROPERTY);
case ASTNode.VARIABLE_DECLARATION_STATEMENT:
return rewrite.getListRewrite(declNode, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
case ASTNode.SINGLE_VARIABLE_DECLARATION:
return rewrite.getListRewrite(declNode, SingleVariableDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.TYPE_DECLARATION:
return rewrite.getListRewrite(declNode, TypeDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ENUM_DECLARATION:
return rewrite.getListRewrite(declNode, EnumDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ANNOTATION_TYPE_DECLARATION:
return rewrite.getListRewrite(declNode, AnnotationTypeDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ENUM_CONSTANT_DECLARATION:
return rewrite.getListRewrite(declNode, EnumConstantDeclaration.MODIFIERS2_PROPERTY);
case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
return rewrite.getListRewrite(declNode, AnnotationTypeMemberDeclaration.MODIFIERS2_PROPERTY);
default:
throw new IllegalArgumentException("node has no modifiers: " + declNode.getClass().getName()); //$NON-NLS-1$
}
}
private Method convert(AnnotationTypeMemberDeclaration memberDeclaration) {
MethodDescriptor methodDescriptor =
JdtUtils.createMethodDescriptor(memberDeclaration.resolveBinding());
return newMethodBuilder(methodDescriptor)
.setSourcePosition(getSourcePosition(memberDeclaration))
.build();
}
@Override
public void endVisit(AnnotationTypeMemberDeclaration node) {
if (skipNode(node)) {
return;
}
GenericSequentialFlowInfo info = processSequential(node, node.getType(), node.getDefault());
info.setNoReturn();
}
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fInvocationNode);
ASTNode typeDecl= astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl= null;
if (typeDecl != null) {
newTypeDecl= typeDecl;
} else {
astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration newAnnotationTypeDecl= (AnnotationTypeDeclaration) newTypeDecl;
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
AnnotationTypeMemberDeclaration newStub= getStub(rewrite, newAnnotationTypeDecl);
List<BodyDeclaration> members= newAnnotationTypeDecl.bodyDeclarations();
int insertIndex= members.size();
ListRewrite listRewriter= rewrite.getListRewrite(newAnnotationTypeDecl, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY);
listRewriter.insertAt(newStub, insertIndex, null);
return rewrite;
}
return null;
}
private int evaluateModifiers(AnnotationTypeDeclaration targetTypeDecl) {
List<BodyDeclaration> methodDecls= targetTypeDecl.bodyDeclarations();
for (int i= 0; i < methodDecls.size(); i++) {
Object curr= methodDecls.get(i);
if (curr instanceof AnnotationTypeMemberDeclaration) {
return ((AnnotationTypeMemberDeclaration) curr).getModifiers();
}
}
return 0;
}
@Override
public boolean preVisit2(ASTNode node) {
if (node instanceof MethodDeclaration || node instanceof Initializer || node instanceof AnnotationTypeMemberDeclaration) {
classes++;
tryStack.push(throw2);
throw2 = 0;
}
return true;
}
@Override
public void endVisit(AnnotationTypeMemberDeclaration node) {
if (maxThrow < throw2)
maxThrow = throw2;
if (minThrow > throw2)
minThrow = throw2;
throw2 = tryStack.pop();
}
@Override
public boolean preVisit2(ASTNode node) {
if (node instanceof MethodDeclaration || node instanceof Initializer || node instanceof AnnotationTypeMemberDeclaration) {
classes++;
tryStack.push(try2);
try2 = 0;
}
return true;
}
@Override
public void endVisit(AnnotationTypeMemberDeclaration node) {
if (maxTry < try2)
maxTry = try2;
if (minTry > try2)
minTry = try2;
try2 = tryStack.pop();
}
@Override
public boolean preVisit2(ASTNode node) {
if (node instanceof MethodDeclaration || node instanceof Initializer
|| node instanceof AnnotationTypeMemberDeclaration) {
classes++;
tryStack.push(locals2);
locals2 = 0;
}
return true;
}
@Override
public void endVisit(AnnotationTypeMemberDeclaration node) {
if (maxLocals < locals2)
maxLocals = locals2;
if (minLocals > locals2)
minLocals = locals2;
locals2 = tryStack.pop();
}
@Override
public boolean preVisit2(ASTNode node) {
if (node instanceof MethodDeclaration || node instanceof Initializer || node instanceof AnnotationTypeMemberDeclaration) {
classes++;
conditionStack.push(condition2);
condition2 = 0;
}
return true;
}
@Override
public void endVisit(AnnotationTypeMemberDeclaration node) {
if (maxFields < condition2)
maxFields = condition2;
if (minFields > condition2)
minFields = condition2;
condition2 = conditionStack.pop();
}
@Override
public void endVisit(AnnotationTypeMemberDeclaration node) {
methods++;
if (maxThis < this2)
maxThis = this2;
if (minThis > this2)
minThis = this2;
this2 = thisStack.pop();
}
@Override
public boolean visit(AnnotationTypeMemberDeclaration node) {
AnnotationTypeAttribute attribute = importer.ensureAnnotationTypeAttributeFromDeclaration(node);
attribute.setIsStub(false);
importer.ensureCommentFromBodyDeclaration(attribute, node);
return super.visit(node);
}
@Override
public void endVisit(AnnotationTypeMemberDeclaration node) {
if (skipNode(node))
return;
GenericSequentialFlowInfo info= processSequential(node, node.getType(), node.getDefault());
info.setNoReturn();
}
public boolean resolveInVariableInitializer() {
if (fInVariableInitializerRequested)
return fInVariableInitializer;
fInVariableInitializerRequested= true;
resolveSelectedNodes();
ASTNode node= getStartNode();
ASTNode last= null;
while (node != null) {
int nodeType= node.getNodeType();
if (node instanceof AbstractTypeDeclaration) {
fInVariableInitializer= false;
break;
} else if (nodeType == ASTNode.ANONYMOUS_CLASS_DECLARATION) {
fInVariableInitializer= false;
break;
} else if (nodeType == ASTNode.VARIABLE_DECLARATION_FRAGMENT &&
((VariableDeclarationFragment)node).getInitializer() == last) {
fInVariableInitializer= true;
break;
} else if (nodeType == ASTNode.SINGLE_VARIABLE_DECLARATION &&
((SingleVariableDeclaration)node).getInitializer() == last) {
fInVariableInitializer= true;
break;
} else if (nodeType == ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION &&
((AnnotationTypeMemberDeclaration)node).getDefault() == last) {
fInVariableInitializer= true;
break;
}
last= node;
node= node.getParent();
}
return fInVariableInitializer;
}
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fInvocationNode);
ASTNode typeDecl= astRoot.findDeclaringNode(fSenderBinding);
ASTNode newTypeDecl= null;
if (typeDecl != null) {
newTypeDecl= typeDecl;
} else {
astRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
newTypeDecl= astRoot.findDeclaringNode(fSenderBinding.getKey());
}
createImportRewrite(astRoot);
if (newTypeDecl instanceof AnnotationTypeDeclaration) {
AnnotationTypeDeclaration newAnnotationTypeDecl= (AnnotationTypeDeclaration) newTypeDecl;
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
AnnotationTypeMemberDeclaration newStub= getStub(rewrite, newAnnotationTypeDecl);
List<BodyDeclaration> members= newAnnotationTypeDecl.bodyDeclarations();
int insertIndex= members.size();
ListRewrite listRewriter= rewrite.getListRewrite(newAnnotationTypeDecl, AnnotationTypeDeclaration.BODY_DECLARATIONS_PROPERTY);
listRewriter.insertAt(newStub, insertIndex, null);
return rewrite;
}
return null;
}
private int evaluateModifiers(AnnotationTypeDeclaration targetTypeDecl) {
List<BodyDeclaration> methodDecls= targetTypeDecl.bodyDeclarations();
for (int i= 0; i < methodDecls.size(); i++) {
Object curr= methodDecls.get(i);
if (curr instanceof AnnotationTypeMemberDeclaration) {
return ((AnnotationTypeMemberDeclaration) curr).getModifiers();
}
}
return 0;
}
private String getSignature(AnnotationTypeMemberDeclaration node) {
StringBuffer buffer= new StringBuffer();
buffer.append(node.getName().toString());
buffer.append('(');
buffer.append(')');
return buffer.toString();
}
@Override
public boolean visit(AnnotationTypeMemberDeclaration node) {
AbstractAnnotationMember annoMember = new AbstractAnnotationMember(
node.getType().resolveBinding().getQualifiedName(), node.getName().getIdentifier());
annoMember.setDefaultExpression(node.getDefault() != null ? node.getDefault().toString() : null);
annotationMembers.add(annoMember);
return true;
}
public static boolean isEqualAnnotationMember(AnnotationTypeMemberDeclaration member1, AnnotationTypeMemberDeclaration member2){
if(!member1.getName().toString().equals(member2.getName().toString())){
return false;
}
return true;
}
public static String getSufixJavadoc(final AnnotationTypeMemberDeclaration annotationMember){
return ((annotationMember != null) && (annotationMember.getJavadoc() != null) && (!annotationMember.getJavadoc().equals("")))? "" : " WITHOUT JAVADOC";
}
private void convertTypeBody(Type type, List<BodyDeclaration> bodyDeclarations) {
TypeDeclaration currentTypeDeclaration = type.getDeclaration();
propagateCapturesFromSupertype(currentTypeDeclaration);
for (BodyDeclaration bodyDeclaration : bodyDeclarations) {
if (bodyDeclaration instanceof FieldDeclaration) {
FieldDeclaration fieldDeclaration = (FieldDeclaration) bodyDeclaration;
type.addFields(convert(fieldDeclaration));
} else if (bodyDeclaration instanceof MethodDeclaration) {
MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDeclaration;
type.addMethod(convert(methodDeclaration));
} else if (bodyDeclaration instanceof AnnotationTypeMemberDeclaration) {
AnnotationTypeMemberDeclaration memberDeclaration =
(AnnotationTypeMemberDeclaration) bodyDeclaration;
type.addMethod(convert(memberDeclaration));
} else if (bodyDeclaration instanceof Initializer) {
Initializer initializer = (Initializer) bodyDeclaration;
Block block = convert(initializer.getBody());
if (JdtUtils.isStatic(initializer)) {
type.addStaticInitializerBlock(block);
} else {
type.addInstanceInitializerBlock(block);
}
} else if (bodyDeclaration instanceof AbstractTypeDeclaration) {
// Nested class
AbstractTypeDeclaration nestedTypeDeclaration = (AbstractTypeDeclaration) bodyDeclaration;
convert(nestedTypeDeclaration);
} else {
throw internalCompilerError(
"Unexpected type for BodyDeclaration: %s, in type: %s",
bodyDeclaration.getClass().getName(), type.getDeclaration().getQualifiedSourceName());
}
}
for (Variable capturedVariable : getCapturedVariables(currentTypeDeclaration)) {
FieldDescriptor fieldDescriptor =
AstUtils.getFieldDescriptorForCapture(type.getTypeDescriptor(), capturedVariable);
type.addField(
Field.Builder.from(fieldDescriptor)
.setCapturedVariable(capturedVariable)
.setSourcePosition(type.getSourcePosition())
.setNameSourcePosition(capturedVariable.getSourcePosition())
.build());
}
if (type.getDeclaration().isCapturingEnclosingInstance()) {
// add field for enclosing instance.
type.addField(
0,
Field.Builder.from(
AstUtils.getFieldDescriptorForEnclosingInstance(
type.getTypeDescriptor(),
type.getEnclosingTypeDeclaration().toUnparameterizedTypeDescriptor()))
.setSourcePosition(type.getSourcePosition())
.build());
}
}
@Override
public boolean consumes(SemanticToken token) {
StructuralPropertyDescriptor location = token.getNode().getLocationInParent();
return location == MethodDeclaration.NAME_PROPERTY || location == AnnotationTypeMemberDeclaration.NAME_PROPERTY;
}
public static AnnotationTypeMemberDeclaration getAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException {
return ASTNodes.getParent(getNameNode(iMethod, cuNode), AnnotationTypeMemberDeclaration.class);
}
@Override
public boolean visit(AnnotationTypeMemberDeclaration node) {
thisStack.push(this2);
this2 = 0;
return true;
}