org.springframework.roo.classpath.javaparser
Class JavaParserUtils

java.lang.Object
  extended by org.springframework.roo.classpath.javaparser.JavaParserUtils

public class JavaParserUtils
extends Object

Assists with the usage of Java Parser.

This class is for internal use by the Java Parser module and should NOT be used by other code.

Since:
1.0
Author:
Ben Alex

Constructor Summary
JavaParserUtils()
           
 
Method Summary
static japa.parser.ast.type.ClassOrInterfaceType getClassOrInterfaceType(japa.parser.ast.expr.NameExpr nameExpr)
          Converts the indicated NameExpr into a ClassOrInterfaceType.
static int getJavaParserModifier(int modifiers)
          Converts a JDK Modifier integer into the equivalent Java Parser modifier.
static org.springframework.roo.model.JavaType getJavaType(CompilationUnitServices compilationUnitServices, japa.parser.ast.body.ClassOrInterfaceDeclaration cid)
          Resolves the effective JavaType a ClassOrInterfaceDeclaration represents, including any type parameters.
static org.springframework.roo.model.JavaType getJavaType(CompilationUnitServices compilationUnitServices, japa.parser.ast.expr.NameExpr nameToFind, Set<org.springframework.roo.model.JavaSymbolName> typeParameters)
          Resolves the effective JavaType a NameExpr represents.
static org.springframework.roo.model.JavaType getJavaType(CompilationUnitServices compilationUnitServices, japa.parser.ast.type.Type type, Set<org.springframework.roo.model.JavaSymbolName> typeParameters)
          Resolves the effective JavaType a Type represents.
static org.springframework.roo.model.JavaType getJavaTypeNow(CompilationUnitServices compilationUnitServices, japa.parser.ast.type.ClassOrInterfaceType cit, Set<org.springframework.roo.model.JavaSymbolName> typeParameters)
          Resolves the effective JavaType a ClassOrInterfaceType represents, including any type arguments.
static int getJdkModifier(int modifiers)
          Converts a Java Parser modifier integer into a JDK Modifier integer.
static japa.parser.ast.expr.NameExpr getNameExpr(japa.parser.ast.expr.AnnotationExpr annotationExpr)
          Obtains the name expression (NameExpr) for the passed AnnotationExpr, which is the annotation's type.
static japa.parser.ast.expr.NameExpr getNameExpr(String className)
          Converts the presented class name into a name expression (either a NamedExpr or QualifiedNameExpr depending on whether a package was presented).
static japa.parser.ast.type.ReferenceType getReferenceType(japa.parser.ast.expr.NameExpr nameExpr)
          Converts the indicated JavaType into a ReferenceType.
static japa.parser.ast.type.Type getType(org.springframework.roo.model.JavaType javaType)
          Given a primitive type, computes the corresponding Java Parser type.
static japa.parser.ast.expr.Expression importExpressionIfRequired(org.springframework.roo.model.JavaType targetType, List<japa.parser.ast.ImportDeclaration> imports, japa.parser.ast.expr.Expression value)
          Recognises Expressions of type FieldAccessExpr and ClassExpr and automatically imports them if required, returning the correct Expression that should subsequently be used.
static japa.parser.ast.type.ReferenceType importParametersForType(org.springframework.roo.model.JavaType targetType, List<japa.parser.ast.ImportDeclaration> imports, org.springframework.roo.model.JavaType typeToImport)
           
static japa.parser.ast.expr.NameExpr importTypeIfRequired(org.springframework.roo.model.JavaType targetType, List<japa.parser.ast.ImportDeclaration> imports, org.springframework.roo.model.JavaType typeToImport)
          Attempts to import the presented JavaType.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavaParserUtils

public JavaParserUtils()
Method Detail

getNameExpr

public static final japa.parser.ast.expr.NameExpr getNameExpr(String className)
Converts the presented class name into a name expression (either a NamedExpr or QualifiedNameExpr depending on whether a package was presented).

Parameters:
className - to convert (required; can be fully qualified or simple name only)
Returns:
a compatible expression (never returns null)

getJdkModifier

public static final int getJdkModifier(int modifiers)
Converts a Java Parser modifier integer into a JDK Modifier integer.

Parameters:
modifiers - the Java Parser int
Returns:
the equivalent JDK int

getJavaParserModifier

public static final int getJavaParserModifier(int modifiers)
Converts a JDK Modifier integer into the equivalent Java Parser modifier.

Parameters:
modifiers - the JDK int
Returns:
the equivalent Java Parser int

getNameExpr

public static final japa.parser.ast.expr.NameExpr getNameExpr(japa.parser.ast.expr.AnnotationExpr annotationExpr)
Obtains the name expression (NameExpr) for the passed AnnotationExpr, which is the annotation's type.

Parameters:
annotationExpr - to retrieve the type name from (required)
Returns:
the name (never null)

getJavaType

public static org.springframework.roo.model.JavaType getJavaType(CompilationUnitServices compilationUnitServices,
                                                                 japa.parser.ast.type.Type type,
                                                                 Set<org.springframework.roo.model.JavaSymbolName> typeParameters)
Resolves the effective JavaType a Type represents. A Type includes low-level types such as void, arrays and primitives.

Parameters:
compilationUnitServices - to use for package resolution (required)
type - to locate (required)
typeParameters - names to consider type parameters (can be null if there are none)
Returns:
the JavaType, with proper indication of primitive and array status (never null)

getJavaType

public static final org.springframework.roo.model.JavaType getJavaType(CompilationUnitServices compilationUnitServices,
                                                                       japa.parser.ast.expr.NameExpr nameToFind,
                                                                       Set<org.springframework.roo.model.JavaSymbolName> typeParameters)
Resolves the effective JavaType a NameExpr represents.

You should use #getJavaType(JavaPackage, List, ClassOrInterfaceType) where possible so that type arguments are preserved (a NameExpr does not contain type arguments).

A name expression can be either qualified or unqualified. If qualified, that represents the fully-qualified name. If a name expression is unqualified, the imports are scanned. If the unqualified name expression is found in the imports, that import declaration represents the fully-qualified name. If the unqualified name expression is not found in the imports, it indicates the name to find is either in the same package as the qualified name expression, or the type relates to a member of java.lang. If part of java.lang, the fully qualified name is treated as part of java.lang. Otherwise the compilation unit package plus unqualified name expression represents the fully qualified name expression.

Parameters:
compilationUnitServices - for package management (required)
nameToFind - to locate (required)
typeParameters - names to consider type parameters (can be null if there are none)
Returns:
the effective Java type (never null)

getReferenceType

public static final japa.parser.ast.type.ReferenceType getReferenceType(japa.parser.ast.expr.NameExpr nameExpr)
Converts the indicated JavaType into a ReferenceType.

Note that no effort is made to manage imports etc.

Parameters:
nameExpr - to convert (required)
Returns:
the corresponding ReferenceType (never null)

getClassOrInterfaceType

public static final japa.parser.ast.type.ClassOrInterfaceType getClassOrInterfaceType(japa.parser.ast.expr.NameExpr nameExpr)
Converts the indicated NameExpr into a ClassOrInterfaceType.

Note that no effort is made to manage imports etc.

Parameters:
nameExpr - to convert (required)
Returns:
the corresponding ClassOrInterfaceType (never null)

getType

public static final japa.parser.ast.type.Type getType(org.springframework.roo.model.JavaType javaType)
Given a primitive type, computes the corresponding Java Parser type.

Presenting a non-primitive type to this method will throw an exception. If you have a non-primitive type, use #importTypeIfRequired(JavaPackage, List, JavaType) and then present the NameExpr it returns to getClassOrInterfaceType(NameExpr).

Parameters:
javaType - a primitive type (required, and must be primitive)
Returns:
the equivalent Java Parser Type

getJavaTypeNow

public static final org.springframework.roo.model.JavaType getJavaTypeNow(CompilationUnitServices compilationUnitServices,
                                                                          japa.parser.ast.type.ClassOrInterfaceType cit,
                                                                          Set<org.springframework.roo.model.JavaSymbolName> typeParameters)
Resolves the effective JavaType a ClassOrInterfaceType represents, including any type arguments.

Parameters:
compilationUnitServices - for package management (required)
cit - the class or interface type to resolve (required)
Returns:
the effective Java type (never null)

getJavaType

public static final org.springframework.roo.model.JavaType getJavaType(CompilationUnitServices compilationUnitServices,
                                                                       japa.parser.ast.body.ClassOrInterfaceDeclaration cid)
Resolves the effective JavaType a ClassOrInterfaceDeclaration represents, including any type parameters.

Parameters:
compilationUnitServices - for package management (required)
cid - the class or interface declaration to resolve (required)
Returns:
the effective Java type (never null)

importParametersForType

public static final japa.parser.ast.type.ReferenceType importParametersForType(org.springframework.roo.model.JavaType targetType,
                                                                               List<japa.parser.ast.ImportDeclaration> imports,
                                                                               org.springframework.roo.model.JavaType typeToImport)

importTypeIfRequired

public static final japa.parser.ast.expr.NameExpr importTypeIfRequired(org.springframework.roo.model.JavaType targetType,
                                                                       List<japa.parser.ast.ImportDeclaration> imports,
                                                                       org.springframework.roo.model.JavaType typeToImport)
Attempts to import the presented JavaType.

Whether imported or not, the method returns a NameExpr suitable for subsequent use when referring to that type.

If an attempt is made to import a java.lang type, it is ignored.

If an attempt is made to import a type without a package, it is ignored.

We import every type usage even if the type usage is within the same package and would theoretically not require an import. This is undertaken so that there is no requirement to separately parse every unqualified type usage within the compilation unit so as to refrain from importing subsequently conflicting types.

Parameters:
targetType - the compilation unit target type (required)
imports - the compilation unit's imports (required)
typeToImport - the type to be imported (required)
Returns:
the name expression to be used when referring to that type (never null)

importExpressionIfRequired

public static final japa.parser.ast.expr.Expression importExpressionIfRequired(org.springframework.roo.model.JavaType targetType,
                                                                               List<japa.parser.ast.ImportDeclaration> imports,
                                                                               japa.parser.ast.expr.Expression value)
Recognises Expressions of type FieldAccessExpr and ClassExpr and automatically imports them if required, returning the correct Expression that should subsequently be used.

Even if an Expression is not resolved by this method into a type and/or imported, the method guarantees to always return an Expression that the caller can subsequently use in place of the passed Expression. In practical terms, the Expression passed to this method will be returned unless the type was already imported, just imported, or represented a java.lang type.

Parameters:
targetType - the compilation unit target type (required)
imports - the existing imports (required)
value - that expression, which need not necessarily be resolvable to a type (required)
Returns:
the expression to now use, as appropriately resolved (never returns null)


Copyright © 2009-2010 VMware, Inc. All Rights Reserved.