use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class WsEndpointsMetadata method getEndpointMethod.
/**
* This method obtains the method that will be used to registered the endpoint
*
* @param endpoint JavaType with the information about the related endpoint to
* be registered
*
* @return MethodMetadata with the necessary information about the new method
*/
private MethodMetadata getEndpointMethod(JavaType endpoint) {
// Check if already exists
if (endpointMethods.get(endpoint) != null) {
return endpointMethods.get(endpoint);
}
// Generating method body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// EndpointImpl endpoint = new EndpointImpl(this.bus, new ENDPOINT(this.SERVICENAME));
bodyBuilder.appendFormalLine("%s endpoint = new EndpointImpl(%s(), new %s(%s()));", getNameOfJavaType(new JavaType("org.apache.cxf.jaxws.EndpointImpl")), getAccessorMethod(getBusField()).getMethodName(), getNameOfJavaType(endpoint), getAccessorMethod(getServiceField(getServiceFromEndpoint(endpoint))).getMethodName());
// endpoint.setFeatures(Arrays.asList(new TraceeCxfFeature(), new LoggingFeature()));
bodyBuilder.appendFormalLine("endpoint.setFeatures(%s.asList(new %s(), new %s()));", getNameOfJavaType(JavaType.ARRAYS), getNameOfJavaType(new JavaType("io.tracee.binding.cxf.TraceeCxfFeature")), getNameOfJavaType(new JavaType("org.apache.cxf.feature.LoggingFeature")));
// endpoint.publish("/SEI");
bodyBuilder.appendFormalLine("endpoint.publish(\"/%s\");", getSeiFromEndpoint(endpoint).getSimpleTypeName());
// return endpoint;
bodyBuilder.appendFormalLine("return endpoint;");
MethodMetadataBuilder endpointMethod = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, new JavaSymbolName(StringUtils.uncapitalize(endpoint.getSimpleTypeName())), JavaType.ENDPOINT, bodyBuilder);
endpointMethod.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.BEAN));
endpointMethods.put(endpoint, endpointMethod.build());
return endpointMethod.build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ClasspathOperationsImpl method createConstructor.
@Override
public void createConstructor(final JavaType name, final Set<String> fields) {
final ClassOrInterfaceTypeDetails javaTypeDetails = typeLocationService.getTypeDetails(name);
Validate.notNull(javaTypeDetails, "The type specified, '%s', doesn't exist", name.getFullyQualifiedTypeName());
final String declaredByMetadataId = PhysicalTypeIdentifier.createIdentifier(name, pathResolver.getFocusedPath(Path.SRC_MAIN_JAVA));
final List<FieldMetadata> constructorFields = new ArrayList<FieldMetadata>();
final List<? extends FieldMetadata> declaredFields = javaTypeDetails.getDeclaredFields();
if (fields != null) {
for (final String field : fields) {
declared: for (final FieldMetadata declaredField : declaredFields) {
if (field.equals(declaredField.getFieldName().getSymbolName())) {
constructorFields.add(declaredField);
break declared;
}
}
}
if (constructorFields.isEmpty()) {
// class, so return without creating any constructor
throw new IllegalArgumentException(String.format("The set of fields provided for the constructor does not exist in the class '%s'", name));
}
}
// Search for an existing constructor
final List<JavaType> parameterTypes = new ArrayList<JavaType>();
for (final FieldMetadata fieldMetadata : constructorFields) {
parameterTypes.add(fieldMetadata.getFieldType());
}
final ConstructorMetadata result = javaTypeDetails.getDeclaredConstructor(parameterTypes);
if (result != null) {
// Found an existing constructor on this class
throw new IllegalArgumentException(String.format("The class '%s' already has a constructor method with the same arguments and it cannot " + "be created. Use '--force' parameter to overrite it.", name));
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
bodyBuilder.appendFormalLine("super();");
for (final FieldMetadata field : constructorFields) {
final String fieldName = field.getFieldName().getSymbolName();
bodyBuilder.appendFormalLine("this." + fieldName + " = " + fieldName + ";");
parameterNames.add(field.getFieldName());
}
// Create the constructor
final ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(declaredByMetadataId);
constructorBuilder.setModifier(Modifier.PUBLIC);
constructorBuilder.setParameterTypes(AnnotatedJavaType.convertFromJavaTypes(parameterTypes));
constructorBuilder.setParameterNames(parameterNames);
constructorBuilder.setBodyBuilder(bodyBuilder);
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(javaTypeDetails);
cidBuilder.addConstructor(constructorBuilder);
typeManagementService.createOrUpdateTypeOnDisk(cidBuilder.build());
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class EntityProjectionMetadata method getConstructor.
/**
* Builds constructor for initializing <code>final</code> fields.
*
* @return ConstructorMetadataBuilder for adding constructor to ITD
*/
private ConstructorMetadataBuilder getConstructor() {
ConstructorMetadataBuilder constructorBuilder = new ConstructorMetadataBuilder(getId());
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
for (FieldMetadata field : fields) {
// Add all fields excluding Serializable field
if (!field.getFieldName().equals(serialField)) {
String fieldName = field.getFieldName().getSymbolName();
constructorBuilder.addParameter(fieldName, field.getFieldType());
bodyBuilder.appendFormalLine(String.format("this.%s = %s;", fieldName, fieldName));
}
}
constructorBuilder.setModifier(Modifier.PUBLIC);
constructorBuilder.setBodyBuilder(bodyBuilder);
return constructorBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class EqualsMetadata method generateHashCodeMethod.
/**
* Generate hashCode method
*
* @param metadataId
* @param annotationValues
* @param locatedFields
* @param importRegistrationResolver
* @return
*/
protected static MethodMetadataBuilder generateHashCodeMethod(String metadataId, EqualsAnnotationValues annotationValues, List<FieldMetadata> locatedFields, ImportRegistrationResolver importRegistrationResolver) {
// Create the method
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
StringBuilder builder = null;
if (annotationValues.isJpaEntity()) {
builder = getJpaEntityHashCodeMethodReturnStatment();
} else {
builder = getDefaultHashCodeMethodReturnStatment(annotationValues, locatedFields, importRegistrationResolver);
}
bodyBuilder.appendFormalLine(builder.toString());
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(metadataId, Modifier.PUBLIC, HASH_CODE_METHOD_NAME, INT_PRIMITIVE, bodyBuilder);
if (annotationValues.isJpaEntity()) {
CommentStructure commentStructure = new CommentStructure();
commentStructure.addComment(new JavadocComment("This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able ".concat(IOUtils.LINE_SEPARATOR).concat("to identify the entity in collections after a new id is assigned to the entity, following the article in ").concat(IOUtils.LINE_SEPARATOR).concat("https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/")), CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(commentStructure);
}
return methodBuilder;
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class EqualsMetadata method getJpaEntityEqualsMethodBody.
/**
* Returns the specific `equals` method body defined for JPA entity classes.
*
* @return {@link InvocableMemberBodyBuilder}
*/
private static InvocableMemberBodyBuilder getJpaEntityEqualsMethodBody(JavaType target, JavaSymbolName identifierAccesor, ImportRegistrationResolver importRegistrationResolver) {
String typeName = target.getSimpleTypeName();
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// if (this == obj) {
// return true;
// }
bodyBuilder.appendFormalLine("if (this == " + OBJECT_NAME + ") {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return true;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// // instanceof is false if the instance is null
// if (!(obj instanceof Pet)) {
// return false;
// }
bodyBuilder.appendFormalLine("// instanceof is false if the instance is null");
bodyBuilder.appendFormalLine("if (!(" + OBJECT_NAME + " instanceof " + typeName + ")) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return false;");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// return getId() != null && Objects.equals(getId(), ((Pet) obj).getId());
bodyBuilder.appendFormalLine("return %1$s() != null && %2$s.equals(%1$s(), ((%3$s) %4$s).%1$s());", identifierAccesor, JavaType.OBJECTS.getNameIncludingTypeParameters(false, importRegistrationResolver), typeName, OBJECT_NAME);
return bodyBuilder;
}
Aggregations