use of com.github.antlrjavaparser.api.expr.NameExpr in project spring-roo by spring-projects.
the class JavaParserUtils method getNameExpr.
/**
* Obtains the name expression ({@link NameExpr}) for the passed
* {@link AnnotationExpr}, which is the annotation's type.
*
* @param annotationExpr to retrieve the type name from (required)
* @return the name (never null)
*/
public static NameExpr getNameExpr(final AnnotationExpr annotationExpr) {
Validate.notNull(annotationExpr, "Annotation expression required");
if (annotationExpr instanceof MarkerAnnotationExpr) {
final MarkerAnnotationExpr a = (MarkerAnnotationExpr) annotationExpr;
final NameExpr nameToFind = a.getName();
Validate.notNull(nameToFind, "Unable to determine annotation name from '%s'", annotationExpr);
return nameToFind;
} else if (annotationExpr instanceof SingleMemberAnnotationExpr) {
final SingleMemberAnnotationExpr a = (SingleMemberAnnotationExpr) annotationExpr;
final NameExpr nameToFind = a.getName();
Validate.notNull(nameToFind, "Unable to determine annotation name from '%s'", annotationExpr);
return nameToFind;
} else if (annotationExpr instanceof NormalAnnotationExpr) {
final NormalAnnotationExpr a = (NormalAnnotationExpr) annotationExpr;
final NameExpr nameToFind = a.getName();
Validate.notNull(nameToFind, "Unable to determine annotation name from '%s'", annotationExpr);
return nameToFind;
}
throw new UnsupportedOperationException("Unknown annotation expression type '" + annotationExpr.getClass().getName() + "'");
}
use of com.github.antlrjavaparser.api.expr.NameExpr in project spring-roo by spring-projects.
the class JavaParserUtils method getJavaType.
/**
* Resolves the effective {@link JavaType} a
* {@link ClassOrInterfaceDeclaration} represents, including any type
* parameters.
*
* @param compilationUnitServices for package management (required)
* @param typeDeclaration the type declaration to resolve (required)
* @return the effective Java type (never null)
*/
public static JavaType getJavaType(final CompilationUnitServices compilationUnitServices, final TypeDeclaration typeDeclaration) {
Validate.notNull(compilationUnitServices, "Compilation unit services required");
Validate.notNull(typeDeclaration, "Type declaration required");
// Convert the ClassOrInterfaceDeclaration name into a JavaType
final NameExpr nameExpr = getNameExpr(typeDeclaration.getName());
final JavaType effectiveType = getJavaType(compilationUnitServices, nameExpr, null);
final List<JavaType> parameterTypes = new ArrayList<JavaType>();
if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
final ClassOrInterfaceDeclaration clazz = (ClassOrInterfaceDeclaration) typeDeclaration;
// Populate JavaType with type parameters
final List<TypeParameter> typeParameters = clazz.getTypeParameters();
if (typeParameters != null) {
final Set<JavaSymbolName> locatedTypeParameters = new HashSet<JavaSymbolName>();
for (final TypeParameter candidate : typeParameters) {
final JavaSymbolName currentTypeParam = new JavaSymbolName(candidate.getName());
locatedTypeParameters.add(currentTypeParam);
JavaType javaType = null;
if (candidate.getTypeBound() == null) {
javaType = new JavaType(OBJECT.getFullyQualifiedTypeName(), 0, DataType.TYPE, currentTypeParam, null);
} else {
final ClassOrInterfaceType cit = candidate.getTypeBound().get(0);
javaType = JavaParserUtils.getJavaTypeNow(compilationUnitServices, cit, locatedTypeParameters);
javaType = new JavaType(javaType.getFullyQualifiedTypeName(), javaType.getArray(), javaType.getDataType(), currentTypeParam, javaType.getParameters());
}
parameterTypes.add(javaType);
}
}
}
return new JavaType(effectiveType.getFullyQualifiedTypeName(), effectiveType.getArray(), effectiveType.getDataType(), null, parameterTypes);
}
use of com.github.antlrjavaparser.api.expr.NameExpr in project spring-roo by spring-projects.
the class JavaParserUtils method getResolvedName.
public static ClassOrInterfaceType getResolvedName(final JavaType target, final JavaType current, final CompilationUnit compilationUnit) {
final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(target, compilationUnit.getImports(), current);
final ClassOrInterfaceType resolvedName = JavaParserUtils.getClassOrInterfaceType(nameExpr);
if (current.getParameters() != null && current.getParameters().size() > 0) {
resolvedName.setTypeArgs(new ArrayList<Type>());
for (final JavaType param : current.getParameters()) {
resolvedName.getTypeArgs().add(getResolvedName(target, param, compilationUnit));
}
}
return resolvedName;
}
use of com.github.antlrjavaparser.api.expr.NameExpr in project spring-roo by spring-projects.
the class JavaParserUtils method getResolvedName.
public static Type getResolvedName(final JavaType target, final JavaType current, final CompilationUnitServices compilationUnit) {
final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(target, compilationUnit.getImports(), current);
final ClassOrInterfaceType resolvedName = JavaParserUtils.getClassOrInterfaceType(nameExpr);
if (current.getParameters() != null && current.getParameters().size() > 0) {
resolvedName.setTypeArgs(new ArrayList<Type>());
for (final JavaType param : current.getParameters()) {
resolvedName.getTypeArgs().add(getResolvedName(target, param, compilationUnit));
}
}
if (current.getArray() > 0) {
// Primitives includes array declaration in resolvedName
if (!current.isPrimitive()) {
return new ReferenceType(resolvedName, current.getArray());
}
}
return resolvedName;
}
use of com.github.antlrjavaparser.api.expr.NameExpr in project spring-roo by spring-projects.
the class JavaParserAnnotationMetadataBuilder method addAnnotationToList.
/**
* Facilitates the addition of the annotation to the presented type.
*
* @param compilationUnitServices to use (required)
* @param annotations to add to the end of (required)
* @param annotation to add (required)
*/
public static void addAnnotationToList(final CompilationUnitServices compilationUnitServices, final List<AnnotationExpr> annotations, final AnnotationMetadata annotation) {
Validate.notNull(compilationUnitServices, "Compilation unit services required");
Validate.notNull(annotations, "Annotations required");
Validate.notNull(annotation, "Annotation metadata required");
// ROO-3678: Add-on which include new annotation should be the responsible to check if annotation
// exists, not JavaParser.
/*// Create a holder for the annotation we're going to create
boolean foundExisting = false;
// Search for an existing annotation of this type
for (final AnnotationExpr candidate : annotations) {
NameExpr existingName = null;
if (candidate instanceof NormalAnnotationExpr) {
existingName = ((NormalAnnotationExpr) candidate).getName();
}
else if (candidate instanceof MarkerAnnotationExpr) {
existingName = ((MarkerAnnotationExpr) candidate).getName();
}
else if (candidate instanceof SingleMemberAnnotationExpr) {
existingName = ((SingleMemberAnnotationExpr) candidate)
.getName();
}
// Convert the candidate annotation type's into a JavaType
final JavaType javaType = JavaParserUtils.getJavaType(
compilationUnitServices, existingName, null);
if (annotation.getAnnotationType().equals(javaType)) {
foundExisting = true;
break;
}
}
Validate.isTrue(!foundExisting,
"Found an existing annotation for type '%s'",
annotation.getAnnotationType());*/
// Import the annotation type, if needed
final NameExpr nameToUse = JavaParserUtils.importTypeIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), annotation.getAnnotationType());
// Create member-value pairs in accordance with Java Parser requirements
final List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
for (final JavaSymbolName attributeName : annotation.getAttributeNames()) {
final AnnotationAttributeValue<?> value = annotation.getAttribute(attributeName);
Validate.notNull(value, "Unable to acquire value '%s' from annotation", attributeName);
final MemberValuePair memberValuePair = convert(value, compilationUnitServices);
// "Member value pair should have been set");
if (memberValuePair != null) {
memberValuePairs.add(memberValuePair);
}
}
// Create the AnnotationExpr; it varies depending on how many
// member-value pairs we need to present
AnnotationExpr annotationExpression = null;
if (memberValuePairs.isEmpty()) {
annotationExpression = new MarkerAnnotationExpr(nameToUse);
} else if (memberValuePairs.size() == 1 && (memberValuePairs.get(0).getName() == null || "value".equals(memberValuePairs.get(0).getName()))) {
final Expression toUse = JavaParserUtils.importExpressionIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), memberValuePairs.get(0).getValue());
annotationExpression = new SingleMemberAnnotationExpr(nameToUse, toUse);
} else {
// We have a number of pairs being presented
annotationExpression = new NormalAnnotationExpr(nameToUse, new ArrayList<MemberValuePair>());
}
// Add our AnnotationExpr to the actual annotations that will eventually
// be flushed through to the compilation unit
JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(annotationExpression, annotation.getCommentStructure());
annotations.add(annotationExpression);
// Add member-value pairs to our AnnotationExpr
if (!memberValuePairs.isEmpty()) {
// SingleMemberAnnotationExpr
if (annotationExpression instanceof MarkerAnnotationExpr) {
final MarkerAnnotationExpr mae = (MarkerAnnotationExpr) annotationExpression;
annotations.remove(mae);
if (memberValuePairs.size() == 1 && (memberValuePairs.get(0).getName() == null || "value".equals(memberValuePairs.get(0).getName()))) {
final Expression toUse = JavaParserUtils.importExpressionIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), memberValuePairs.get(0).getValue());
annotationExpression = new SingleMemberAnnotationExpr(nameToUse, toUse);
JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(annotationExpression, annotation.getCommentStructure());
annotations.add(annotationExpression);
} else {
// We have a number of pairs being presented
annotationExpression = new NormalAnnotationExpr(nameToUse, new ArrayList<MemberValuePair>());
JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(annotationExpression, annotation.getCommentStructure());
annotations.add(annotationExpression);
}
}
if (annotationExpression instanceof SingleMemberAnnotationExpr) {
// Potentially upgrade this expression to a NormalAnnotationExpr
final SingleMemberAnnotationExpr smae = (SingleMemberAnnotationExpr) annotationExpression;
if (memberValuePairs.size() == 1 && memberValuePairs.get(0).getName() == null || memberValuePairs.get(0).getName().equals("value") || memberValuePairs.get(0).getName().equals("")) {
// They specified only a single member-value pair, and it is
// the default anyway, so we need not do anything except
// update the value
final Expression toUse = JavaParserUtils.importExpressionIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), memberValuePairs.get(0).getValue());
smae.setMemberValue(toUse);
return;
}
// There is > 1 expression, or they have provided some sort of
// non-default value, so it's time to upgrade the expression
// (whilst retaining any potentially existing expression values)
final Expression existingValue = smae.getMemberValue();
annotationExpression = new NormalAnnotationExpr(smae.getName(), new ArrayList<MemberValuePair>());
((NormalAnnotationExpr) annotationExpression).getPairs().add(new MemberValuePair("value", existingValue));
}
Validate.isInstanceOf(NormalAnnotationExpr.class, annotationExpression, "Attempting to add >1 annotation member-value pair requires an existing normal annotation expression");
final List<MemberValuePair> annotationPairs = ((NormalAnnotationExpr) annotationExpression).getPairs();
annotationPairs.clear();
for (final MemberValuePair pair : memberValuePairs) {
final Expression toUse = JavaParserUtils.importExpressionIfRequired(compilationUnitServices.getEnclosingTypeName(), compilationUnitServices.getImports(), pair.getValue());
pair.setValue(toUse);
annotationPairs.add(pair);
}
}
}
Aggregations