use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class ToStringMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.
@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
final JavaType javaType = ToStringMetadata.getJavaType(metadataIdentificationString);
final LogicalPath path = ToStringMetadata.getPath(metadataIdentificationString);
return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JpaEntityMetadata method getAddValueMethod.
/**
* Create add method to handle referenced relation
*
* @param addMethodName
* @param field
* @param cardinality
* @param childType
* @param mappedBy
* @param removeMethodName
* @param importResolver
* @return method metadata or null if method already in class
*/
private MethodMetadata getAddValueMethod(final JavaSymbolName addMethodName, final FieldMetadata field, final Cardinality cardinality, final JavaType childType, final String mappedBy, final JavaSymbolName removeMethodName, ImportRegistrationResolver importResolver) {
// Identify parameters type and name
final List<JavaType> parameterTypes = new ArrayList<JavaType>(1);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(1);
if (cardinality == Cardinality.ONE_TO_ONE) {
parameterTypes.add(childType);
parameterNames.add(field.getFieldName());
} else {
parameterTypes.add(JavaType.iterableOf(childType));
parameterNames.add(new JavaSymbolName(field.getFieldName().getSymbolName() + ADD_PARAMETER_SUFFIX));
}
// See if the type itself declared the method
MethodMetadata existingMethod = getGovernorMethod(addMethodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
final InvocableMemberBodyBuilder builder = new InvocableMemberBodyBuilder();
if (cardinality == Cardinality.ONE_TO_ONE) {
buildAddOneToOneBody(field, mappedBy, parameterNames.get(0), childType, removeMethodName, builder);
} else if (cardinality == Cardinality.ONE_TO_MANY) {
buildAddOneToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
} else {
buildAddManyToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
}
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, addMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, builder).build();
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JpaEntityMetadata method getRemoveMethod.
/**
* Create remove method to handle referenced relation
*
* @param removeMethodName
* @param field
* @param cardinality
* @param childType
* @param mappedBy
* @param importResolver
* @return method metadata or null if method already in class
*/
private MethodMetadata getRemoveMethod(final JavaSymbolName removeMethodName, final FieldMetadata field, final Cardinality cardinality, final JavaType childType, final String mappedBy, ImportRegistrationResolver importResolver) {
// Identify parameters types and names (if any)
final List<JavaType> parameterTypes = new ArrayList<JavaType>(1);
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>(1);
if (cardinality != Cardinality.ONE_TO_ONE) {
parameterTypes.add(JavaType.iterableOf(childType));
parameterNames.add(new JavaSymbolName(field.getFieldName().getSymbolName() + REMOVE_PARAMETER_SUFFIX));
}
// See if the type itself declared the method
MethodMetadata existingMethod = getGovernorMethod(removeMethodName, parameterTypes);
if (existingMethod != null) {
return existingMethod;
}
final InvocableMemberBodyBuilder builder = new InvocableMemberBodyBuilder();
if (cardinality == Cardinality.ONE_TO_ONE) {
buildRemoveOneToOneBody(field, mappedBy, builder);
} else if (cardinality == Cardinality.ONE_TO_MANY) {
buildRemoveOneToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
} else {
// ManyToMany
buildRemoveManyToManyBody(field, mappedBy, parameterNames.get(0), childType, builder, importResolver);
}
return new MethodMetadataBuilder(getId(), Modifier.PUBLIC, removeMethodName, JavaType.VOID_PRIMITIVE, AnnotatedJavaType.convertFromJavaTypes(parameterTypes), parameterNames, builder).build();
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JpaEntityMetadataProviderImpl method getGovernorPhysicalTypeIdentifier.
@Override
protected String getGovernorPhysicalTypeIdentifier(final String metadataIdentificationString) {
final JavaType javaType = getType(metadataIdentificationString);
final LogicalPath path = PhysicalTypeIdentifierNamingUtils.getPath(PROVIDES_TYPE_STRING, metadataIdentificationString);
return PhysicalTypeIdentifier.createIdentifier(javaType, path);
}
use of org.springframework.roo.model.JavaType in project spring-roo by spring-projects.
the class JavaParserUtils method getJavaType.
/**
* Resolves the effective {@link JavaType} a {@link NameExpr} represents.
* <p>
* You should use {@link #getJavaType(CompilationUnitServices, Type, Set)}
* where possible so that type arguments are preserved (a {@link NameExpr}
* does not contain type arguments).
* <p>
* A name expression can be either qualified or unqualified.
* <p>
* If a name expression is qualified and the qualification starts with a
* lowercase letter, that represents the fully-qualified name. If the
* qualification starts with an uppercase letter, the package name is
* prepended to the qualifier.
* <p>
* 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.
*
* @param compilationUnitServices for package management (required)
* @param nameToFind to locate (required)
* @param typeParameters names to consider type parameters (can be null if
* there are none)
* @return the effective Java type (never null)
*/
public static JavaType getJavaType(final CompilationUnitServices compilationUnitServices, final NameExpr nameToFind, final Set<JavaSymbolName> typeParameters) {
Validate.notNull(compilationUnitServices, "Compilation unit services required");
Validate.notNull(nameToFind, "Name to find is required");
final JavaPackage compilationUnitPackage = compilationUnitServices.getCompilationUnitPackage();
if (nameToFind instanceof QualifiedNameExpr) {
final QualifiedNameExpr qne = (QualifiedNameExpr) nameToFind;
// Handle qualified name expressions that are related to inner types
// (eg Foo.Bar)
final NameExpr qneQualifier = qne.getQualifier();
final NameExpr enclosedBy = getNameExpr(compilationUnitServices.getEnclosingTypeName().getSimpleTypeName());
if (isEqual(qneQualifier, enclosedBy)) {
// This qualified name expression is simply an inner type
// reference
final String name = compilationUnitServices.getEnclosingTypeName().getFullyQualifiedTypeName() + "." + nameToFind.getName();
return new JavaType(name, compilationUnitServices.getEnclosingTypeName());
}
// package (ROO-1210)
if (qne.toString().length() > 1 && Character.isUpperCase(qne.toString().charAt(0))) {
// First letter is uppercase, so this likely requires prepending
// of some package name
final ImportDeclaration importDeclaration = getImportDeclarationFor(compilationUnitServices, qne.getQualifier());
if (importDeclaration == null) {
if (!compilationUnitPackage.getFullyQualifiedPackageName().equals("")) {
// package
return new JavaType(compilationUnitServices.getCompilationUnitPackage().getFullyQualifiedPackageName() + "." + qne.toString());
}
} else {
return new JavaType(importDeclaration.getName() + "." + qne.getName());
}
// This name expression (which contains a dot) had its qualifier
// imported, so let's use the import
} else {
// a package
return new JavaType(qne.toString());
}
}
if ("?".equals(nameToFind.getName())) {
return new JavaType(OBJECT.getFullyQualifiedTypeName(), 0, DataType.TYPE, JavaType.WILDCARD_NEITHER_ARG, null);
}
// list
if (typeParameters != null && typeParameters.contains(new JavaSymbolName(nameToFind.getName()))) {
return new JavaType(nameToFind.getName(), 0, DataType.VARIABLE, null, null);
}
// Check if we are looking for the enclosingType itself
final NameExpr enclosingTypeName = getNameExpr(compilationUnitServices.getEnclosingTypeName().getSimpleTypeName());
if (isEqual(enclosingTypeName, nameToFind)) {
return compilationUnitServices.getEnclosingTypeName();
}
// check if the compilation unit itself declares that type
for (final TypeDeclaration internalType : compilationUnitServices.getInnerTypes()) {
final NameExpr nameExpr = getNameExpr(internalType.getName());
if (isEqual(nameExpr, nameToFind)) {
// Found, so now we need to convert the internalType to a proper
// JavaType
final String name = compilationUnitServices.getEnclosingTypeName().getFullyQualifiedTypeName() + "." + nameToFind.getName();
return new JavaType(name);
}
}
final ImportDeclaration importDeclaration = getImportDeclarationFor(compilationUnitServices, nameToFind);
if (importDeclaration == null) {
if (JdkJavaType.isPartOfJavaLang(nameToFind.getName())) {
return new JavaType("java.lang." + nameToFind.getName());
}
final String name = compilationUnitPackage.getFullyQualifiedPackageName().equals("") ? nameToFind.getName() : compilationUnitPackage.getFullyQualifiedPackageName() + "." + nameToFind.getName();
return new JavaType(name);
}
return new JavaType(importDeclaration.getName().toString());
}
Aggregations