use of com.github.javaparser.ast.body.AnnotationDeclaration in project javaparser by javaparser.
the class CompilationUnit method addAnnotationDeclaration.
/**
* Add an annotation declaration to the types of this compilation unit
*
* @param name the annotation name
* @param modifiers the modifiers (like Modifier.PUBLIC)
* @return the newly created class
*/
public AnnotationDeclaration addAnnotationDeclaration(String name, Modifier... modifiers) {
AnnotationDeclaration annotationDeclaration = new AnnotationDeclaration(Arrays.stream(modifiers).collect(Collectors.toCollection(() -> EnumSet.noneOf(Modifier.class))), name);
getTypes().add(annotationDeclaration);
return annotationDeclaration;
}
use of com.github.javaparser.ast.body.AnnotationDeclaration in project gradle by gradle.
the class SinceAnnotationMissingRule method maybeViolation.
@Override
public Violation maybeViolation(final JApiCompatibility member) {
String className = null;
GenericVisitorAdapter<Object, Void> visitor = null;
if (member instanceof JApiMethod && !isOverride((JApiMethod) member)) {
final JApiMethod method = (JApiMethod) member;
if (isDeprecated(method)) {
return null;
}
className = method.getjApiClass().getFullyQualifiedName();
visitor = new GenericVisitorAdapter<Object, Void>() {
@Override
public Object visit(ClassOrInterfaceDeclaration classDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(classDeclaration.getName(), toSimpleName(method.getjApiClass().getFullyQualifiedName()), classDeclaration)) {
return new Object();
}
return super.visit(classDeclaration, arg);
}
@Override
public Object visit(AnnotationDeclaration annotationDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(annotationDeclaration.getName(), toSimpleName(method.getjApiClass().getFullyQualifiedName()), annotationDeclaration)) {
return new Object();
}
return super.visit(annotationDeclaration, arg);
}
@Override
public Object visit(EnumDeclaration enumDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(enumDeclaration.getName(), toSimpleName(method.getjApiClass().getFullyQualifiedName()), enumDeclaration)) {
return new Object();
}
return super.visit(enumDeclaration, arg);
}
@Override
public Object visit(MethodDeclaration methodDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(methodDeclaration.getName(), method.getName(), methodDeclaration)) {
return new Object();
}
return null;
}
};
} else if (member instanceof JApiField) {
final JApiField field = (JApiField) member;
if (isDeprecated(field)) {
return null;
}
className = field.getjApiClass().getFullyQualifiedName();
visitor = new GenericVisitorAdapter<Object, Void>() {
@Override
public Object visit(ClassOrInterfaceDeclaration classDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(classDeclaration.getName(), toSimpleName(field.getjApiClass().getFullyQualifiedName()), classDeclaration)) {
return new Object();
}
return super.visit(classDeclaration, arg);
}
@Override
public Object visit(AnnotationDeclaration annotationDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(annotationDeclaration.getName(), toSimpleName(field.getjApiClass().getFullyQualifiedName()), annotationDeclaration)) {
return new Object();
}
return super.visit(annotationDeclaration, arg);
}
@Override
public Object visit(EnumDeclaration enumDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(enumDeclaration.getName(), toSimpleName(field.getjApiClass().getFullyQualifiedName()), enumDeclaration)) {
return new Object();
}
return super.visit(enumDeclaration, arg);
}
@Override
public Object visit(FieldDeclaration fieldDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(fieldDeclaration.getVariables().get(0).getId().getName(), field.getName(), fieldDeclaration)) {
return new Object();
}
return null;
}
@Override
public Object visit(EnumConstantDeclaration enumConstantDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(enumConstantDeclaration.getName(), field.getName(), enumConstantDeclaration)) {
return new Object();
}
return null;
}
};
} else if (member instanceof JApiConstructor) {
final JApiConstructor constructor = (JApiConstructor) member;
if (isDeprecated(constructor) || isInject(constructor)) {
return null;
}
className = constructor.getjApiClass().getFullyQualifiedName();
visitor = new GenericVisitorAdapter<Object, Void>() {
@Override
public Object visit(ConstructorDeclaration constructorDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(constructorDeclaration.getName(), toSimpleName(constructor.getjApiClass().getFullyQualifiedName()), constructorDeclaration)) {
return new Object();
}
return super.visit(constructorDeclaration, arg);
}
@Override
public Object visit(ClassOrInterfaceDeclaration classDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(classDeclaration.getName(), toSimpleName(constructor.getjApiClass().getFullyQualifiedName()), classDeclaration)) {
return new Object();
}
return super.visit(classDeclaration, arg);
}
@Override
public Object visit(AnnotationDeclaration annotationDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(annotationDeclaration.getName(), toSimpleName(constructor.getjApiClass().getFullyQualifiedName()), annotationDeclaration)) {
return new Object();
}
return super.visit(annotationDeclaration, arg);
}
@Override
public Object visit(EnumDeclaration enumDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(enumDeclaration.getName(), toSimpleName(constructor.getjApiClass().getFullyQualifiedName()), enumDeclaration)) {
return new Object();
}
return super.visit(enumDeclaration, arg);
}
@Override
public Object visit(FieldDeclaration fieldDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(fieldDeclaration.getVariables().get(0).getId().getName(), constructor.getName(), fieldDeclaration)) {
return new Object();
}
return null;
}
@Override
public Object visit(EnumConstantDeclaration enumConstantDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(enumConstantDeclaration.getName(), constructor.getName(), enumConstantDeclaration)) {
return new Object();
}
return null;
}
};
} else if (member instanceof JApiClass) {
final JApiClass clazz = (JApiClass) member;
if (isDeprecated(clazz)) {
return null;
}
className = clazz.getFullyQualifiedName();
visitor = new GenericVisitorAdapter<Object, Void>() {
@Override
public Object visit(ClassOrInterfaceDeclaration classDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(classDeclaration.getName(), toSimpleName(clazz.getFullyQualifiedName()), classDeclaration)) {
return new Object();
}
return super.visit(classDeclaration, arg);
}
@Override
public Object visit(AnnotationDeclaration annotationDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(annotationDeclaration.getName(), toSimpleName(clazz.getFullyQualifiedName()), annotationDeclaration)) {
return new Object();
}
return super.visit(annotationDeclaration, arg);
}
@Override
public Object visit(EnumDeclaration enumDeclaration, Void arg) {
if (matchesNameAndContainsAnnotation(enumDeclaration.getName(), toSimpleName(clazz.getFullyQualifiedName()), enumDeclaration)) {
return new Object();
}
return super.visit(enumDeclaration, arg);
}
};
}
if (className == null) {
return null;
}
try {
Object result = JavaParser.parse(sourceFileFor(className)).accept(visitor, null);
if (result == null) {
return acceptOrReject(member, Violation.error(member, "Is not annotated with @since " + getCurrentVersion()));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
use of com.github.javaparser.ast.body.AnnotationDeclaration in project javaparser by javaparser.
the class CompilationUnitContext method solveType.
@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
if (wrappedNode.getTypes() != null) {
for (TypeDeclaration<?> type : wrappedNode.getTypes()) {
if (type.getName().getId().equals(name)) {
if (type instanceof ClassOrInterfaceDeclaration) {
return SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration((ClassOrInterfaceDeclaration) type));
} else if (type instanceof AnnotationDeclaration) {
return SymbolReference.solved(new JavaParserAnnotationDeclaration((AnnotationDeclaration) type, typeSolver));
} else if (type instanceof EnumDeclaration) {
return SymbolReference.solved(new JavaParserEnumDeclaration((EnumDeclaration) type, typeSolver));
} else {
throw new UnsupportedOperationException(type.getClass().getCanonicalName());
}
}
}
}
// Look in current package
if (this.wrappedNode.getPackageDeclaration().isPresent()) {
String qName = this.wrappedNode.getPackageDeclaration().get().getName().toString() + "." + name;
SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref != null && ref.isSolved()) {
return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
}
} else {
// look for classes in the default package
String qName = name;
SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref != null && ref.isSolved()) {
return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
}
}
if (wrappedNode.getImports() != null) {
int dotPos = name.indexOf('.');
String prefix = null;
if (dotPos > -1) {
prefix = name.substring(0, dotPos);
}
// look into type imports
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if (!importDecl.isAsterisk()) {
String qName = importDecl.getNameAsString();
boolean defaultPackage = !importDecl.getName().getQualifier().isPresent();
boolean found = !defaultPackage && importDecl.getName().getIdentifier().equals(name);
if (!found) {
if (prefix != null) {
found = qName.endsWith("." + prefix);
if (found) {
qName = qName + name.substring(dotPos);
}
}
}
if (found) {
SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref.isSolved()) {
return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
}
}
}
}
// look into type imports on demand
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if (importDecl.isAsterisk()) {
String qName = importDecl.getNameAsString() + "." + name;
SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref.isSolved()) {
return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
}
}
}
}
// Look in the java.lang package
SymbolReference<ResolvedReferenceTypeDeclaration> ref = typeSolver.tryToSolveType("java.lang." + name);
if (ref.isSolved()) {
return SymbolReference.adapt(ref, ResolvedTypeDeclaration.class);
}
// DO NOT look for absolute name if this name is not qualified: you cannot import classes from the default package
if (isQualifiedName(name)) {
return SymbolReference.adapt(typeSolver.tryToSolveType(name), ResolvedTypeDeclaration.class);
} else {
return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class);
}
}
use of com.github.javaparser.ast.body.AnnotationDeclaration in project javaparser by javaparser.
the class LexicalDifferenceCalculatorTest method annotationDeclarationNameExampleModified.
@Test
public void annotationDeclarationNameExampleModified() throws IOException {
considerExample("AnnotationDeclaration_Example1_original");
AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
SimpleName newName = new SimpleName("NewName");
LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.NAME, annotationDeclaration.getName(), newName);
csm.removeIndentationElements();
int i = 0;
assertEquals(new CsmToken(GeneratedJavaParserConstants.AT), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.INTERFACE), csm.elements.get(i++));
assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(newName), csm.elements.get(i++));
assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.LBRACE), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(0)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(1)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(2)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(3)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(4)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(5)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.RBRACE), csm.elements.get(i++));
assertEquals(i, csm.elements.size());
}
use of com.github.javaparser.ast.body.AnnotationDeclaration in project javaparser by javaparser.
the class LexicalDifferenceCalculatorTest method annotationDeclaratioJavadocExampleAddingJavadoc.
@Test
public void annotationDeclaratioJavadocExampleAddingJavadoc() throws IOException {
considerExample("AnnotationDeclaration_Example3_original");
AnnotationDeclaration annotationDeclaration = (AnnotationDeclaration) cu.getType(0);
CsmElement element = ConcreteSyntaxModel.forClass(annotationDeclaration.getClass());
JavadocComment comment = new JavadocComment("Cool this annotation!");
LexicalDifferenceCalculator.CalculatedSyntaxModel csm = new LexicalDifferenceCalculator().calculatedSyntaxModelAfterPropertyChange(element, annotationDeclaration, ObservableProperty.COMMENT, null, comment);
csm.removeIndentationElements();
int i = 0;
assertEquals(new CsmToken(GeneratedJavaParserConstants.PUBLIC), csm.elements.get(i++));
assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.AT), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.INTERFACE), csm.elements.get(i++));
assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getName()), csm.elements.get(i++));
assertEquals(new CsmToken(spaceTokenKind()), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.LBRACE), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(0)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(1)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(2)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(3)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(4)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new LexicalDifferenceCalculator.CsmChild(annotationDeclaration.getMember(5)), csm.elements.get(i++));
assertEquals(new CsmToken(eolTokenKind()), csm.elements.get(i++));
assertEquals(new CsmToken(GeneratedJavaParserConstants.RBRACE), csm.elements.get(i++));
assertEquals(i, csm.elements.size());
}
Aggregations