use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class WsEndpointsMetadataProviderImpl method getMetadata.
@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) {
// Getting annotated class details
ClassOrInterfaceTypeDetails cid = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails();
AnnotationMetadata wsEndpointsAnnotation = cid.getAnnotation(ROO_WS_ENDPOINTS);
// Getting endpoints from annotation
AnnotationAttributeValue<?> currentEndpoints = wsEndpointsAnnotation.getAttribute("endpoints");
Validate.notNull(currentEndpoints, "ERROR: You must provide a valid endpoint list to be able to register endpoints.");
List<?> values = (List<?>) currentEndpoints.getValue();
Iterator<?> valuesIt = values.iterator();
Map<JavaType, JavaType> endpointsAndSeis = new TreeMap<JavaType, JavaType>();
Map<JavaType, JavaType> endpointsAndServices = new TreeMap<JavaType, JavaType>();
while (valuesIt.hasNext()) {
ClassAttributeValue endpointAttr = (ClassAttributeValue) valuesIt.next();
JavaType endpoint = endpointAttr.getValue();
// Getting endpoint details
ClassOrInterfaceTypeDetails endpointDetails = getTypeLocationService().getTypeDetails(endpoint);
// Use SeiImplMetadata to obtain necessary information about the endpoint
final String endpointMetadataId = SeiImplMetadata.createIdentifier(endpoint, PhysicalTypeIdentifier.getPath(endpointDetails.getDeclaredByMetadataId()));
final SeiImplMetadata endpointMetadata = (SeiImplMetadata) getMetadataService().get(endpointMetadataId);
JavaType sei = endpointMetadata.getSei();
JavaType service = endpointMetadata.getService();
// Saving the related sei
endpointsAndSeis.put(endpoint, sei);
// Saving the related service
endpointsAndServices.put(endpoint, service);
}
// Getting profile from annotation
String profile = "";
AnnotationAttributeValue<String> profileAttr = wsEndpointsAnnotation.getAttribute("profile");
if (profileAttr != null && StringUtils.isNotEmpty(profileAttr.getValue())) {
profile = profileAttr.getValue();
}
return new WsEndpointsMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, endpointsAndSeis, endpointsAndServices, profile);
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class WsOperationsImpl method addWsClient.
@Override
public void addWsClient(String wsdlLocation, String endPoint, JavaType configClass, SoapBindingType bindingType, String serviceUrl, String profile) {
Validate.notEmpty(wsdlLocation, "ERROR: Provide a valid wsdlLocation");
Validate.notEmpty(endPoint, "ERROR: Provide a valid endPoint");
Validate.notNull(configClass, "ERROR: Provide a valid configClass");
// Check if the configClass is located in an application module
if (!isLocatedInApplicationModule(configClass)) {
LOGGER.log(Level.INFO, "ERROR: The provided config class is not located in an application module.");
return;
}
// Getting module from the .wsdl file
final Pom wsdlModule = getModuleFromWsdlLocation(wsdlLocation);
final String wsdlModuleName = wsdlModule.getModuleName();
// Getting the wsdlName without the module
final String wsdlName = getWsdlNameWithoutModule(wsdlLocation);
// Getting wsdl absolute path from the provided wsdlLocation
final String wsdlPath = getWsdlAbsolutePathFromWsdlName(wsdlLocation);
// Check if provided .wsdl exists
Validate.isTrue(getFileManager().exists(wsdlPath), "ERROR: You must provide an existing .wsdl file.");
// the configClass module and the .wsdl module
if (wsdlModuleName != configClass.getModule()) {
getProjectOperations().addDependency(configClass.getModule(), new Dependency(wsdlModule.getGroupId(), wsdlModule.getArtifactId(), null));
}
// Check if provided configClass exists or should be generated
boolean isNewConfigClass = false;
ClassOrInterfaceTypeDetails configClassDetails = getTypeLocationService().getTypeDetails(configClass);
if (configClassDetails == null) {
isNewConfigClass = true;
}
// If it have it, it should match with the provided one the provided one
if (!isNewConfigClass) {
MemberDetails configClassMemberDetails = getMemberDetailsScanner().getMemberDetails(getClass().getName(), configClassDetails);
AnnotationMetadata configurationAnnotation = configClassMemberDetails.getAnnotation(SpringJavaType.CONFIGURATION);
if (configurationAnnotation == null) {
LOGGER.log(Level.INFO, "ERROR: The provided class is not annotated with @Configuration so is not possible to include Web Service client configuration on it." + "Specify other configuration class that contains @Configuration annotation or specify a not existing class to generate it.");
return;
}
if (StringUtils.isNotEmpty(profile)) {
AnnotationMetadata profileAnnotation = configClassMemberDetails.getAnnotation(SpringJavaType.PROFILE);
if (profileAnnotation != null) {
String profiles = (String) profileAnnotation.getAttribute("value").getValue();
String[] definedProfiles = profiles.split(",");
boolean profileExists = false;
for (String definedProfile : definedProfiles) {
if (definedProfile.equals(profile)) {
profileExists = true;
}
}
if (!profileExists) {
LOGGER.log(Level.INFO, "ERROR: The provided configuration class doesn't work in the provided profile. " + "Use a different configuration class or use a different profile.");
return;
}
}
}
}
// Obtain the service URL from the provided .wsdl file if empty
if (StringUtils.isEmpty(serviceUrl)) {
serviceUrl = getServiceUrlForEndpointFromWsdlFile(endPoint, wsdlPath);
Validate.notEmpty(serviceUrl, "ERROR: It has not been possible to obtain the URL of the service from the provided .wsdl file. Indicate some serviceUrl using --serviceUrl parameter");
}
// Obtain the binding type from the provided .wsdl file if empty
if (bindingType == null) {
bindingType = getBindingTypeFromWsdlFile(wsdlPath);
Validate.notNull(bindingType, "ERROR: It has not been possible to obtain the BindingType of the service from the provided .wsdl file. Indicate an specific BindingType using --binding parameter");
}
// Always is necessary to obtain the targetNameSpace from the provided .wsdl file
String targetNameSpace = getTargetNameSpaceFromWsdlFile(wsdlPath);
Validate.notEmpty(targetNameSpace, "ERROR: It has not been possible to obtain the targetNamespace of the service from the provided .wsdl file. Check if your .wsdl file has the correct format.");
// Include necessary dependencies and plugins
includeDependenciesAndPluginsForWsClient(wsdlName, wsdlModuleName);
// Include the necessary properties using the provided profile
getApplicationConfigService().addProperty(configClass.getModule(), "url/".concat(endPoint), serviceUrl, profile, true);
// Generating the new configuration class if not exists
// If provided class already exists, update it
ClassOrInterfaceTypeDetailsBuilder cidBuilder = null;
if (!isNewConfigClass) {
// Obtain builder from the existing class
cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(configClassDetails);
// Check if already have @RooWsClients annotation
AnnotationMetadataBuilder wsClientsAnnotation = cidBuilder.getDeclaredTypeAnnotation(RooJavaType.ROO_WS_CLIENTS);
if (wsClientsAnnotation != null) {
// Update the existing one
AnnotationAttributeValue<?> existingEndPoints = wsClientsAnnotation.build().getAttribute("endpoints");
List<?> values = (List<?>) existingEndPoints.getValue();
if (values != null) {
// Check if the provided endpoint exists yet in this config class
Iterator<?> it = values.iterator();
boolean alreadyManaged = false;
while (it.hasNext()) {
NestedAnnotationAttributeValue existingEndPoint = (NestedAnnotationAttributeValue) it.next();
String existingEndpointName = (String) existingEndPoint.getValue().getAttribute("endpoint").getValue();
if (existingEndpointName.equals(endPoint)) {
alreadyManaged = true;
}
}
// If endpoint already exists, show an error indicating that this endpoint is already managed
if (alreadyManaged) {
LOGGER.log(Level.INFO, "ERROR: The provided endpoint is already defined in the provided configuration class. " + "Specify some different configuration class.");
return;
} else {
// Update existing annotation with the new endPoint
Iterator<?> iterator = values.iterator();
List<AnnotationAttributeValue<?>> endpoints = new ArrayList<AnnotationAttributeValue<?>>();
while (iterator.hasNext()) {
NestedAnnotationAttributeValue existingEndPoint = (NestedAnnotationAttributeValue) iterator.next();
String existingEndpointName = (String) existingEndPoint.getValue().getAttribute("endpoint").getValue();
String existingEndpointNameSpace = (String) existingEndPoint.getValue().getAttribute("targetNamespace").getValue();
EnumDetails existingType = (EnumDetails) existingEndPoint.getValue().getAttribute("binding").getValue();
// Create @RooWsClient annotation
NestedAnnotationAttributeValue existingEndpoint = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getWsClientAnnotation(existingEndpointName, existingEndpointNameSpace, existingType).build());
endpoints.add(existingEndpoint);
}
// Create @RooWsClient annotation
NestedAnnotationAttributeValue newEndpoint = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getWsClientAnnotation(endPoint, targetNameSpace, bindingType).build());
endpoints.add(newEndpoint);
ArrayAttributeValue<AnnotationAttributeValue<?>> newEndpoints = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("endpoints"), endpoints);
wsClientsAnnotation.addAttribute(newEndpoints);
}
}
} else {
// If not exists, add it with the new elements
wsClientsAnnotation = new AnnotationMetadataBuilder(new JavaType(RooWsClients.class));
// Create @RooWsClient annotation
List<AnnotationAttributeValue<?>> endpoints = new ArrayList<AnnotationAttributeValue<?>>();
NestedAnnotationAttributeValue newEndpoint = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getWsClientAnnotation(endPoint, targetNameSpace, bindingType).build());
endpoints.add(newEndpoint);
ArrayAttributeValue<AnnotationAttributeValue<?>> newEndpoints = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("endpoints"), endpoints);
wsClientsAnnotation.addAttribute(newEndpoints);
if (StringUtils.isNotEmpty(profile)) {
wsClientsAnnotation.addStringAttribute("profile", profile);
}
// Include new @RooWsClients annotation
cidBuilder.addAnnotation(wsClientsAnnotation);
}
} else {
// Create new configuration class
final String configClassIdentifier = getPathResolver().getCanonicalPath(configClass.getModule(), Path.SRC_MAIN_JAVA, configClass);
final String mid = PhysicalTypeIdentifier.createIdentifier(configClass, getPathResolver().getPath(configClassIdentifier));
cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(mid, Modifier.PUBLIC, configClass, PhysicalTypeCategory.CLASS);
// Create new @RooWsClients annotation
AnnotationMetadataBuilder wsClientsAnnotation = new AnnotationMetadataBuilder(new JavaType(RooWsClients.class));
// Create @RooWsClient annotation
List<AnnotationAttributeValue<?>> endpoints = new ArrayList<AnnotationAttributeValue<?>>();
NestedAnnotationAttributeValue newEndpoint = new NestedAnnotationAttributeValue(new JavaSymbolName("value"), getWsClientAnnotation(endPoint, targetNameSpace, bindingType).build());
endpoints.add(newEndpoint);
ArrayAttributeValue<AnnotationAttributeValue<?>> newEndpoints = new ArrayAttributeValue<AnnotationAttributeValue<?>>(new JavaSymbolName("endpoints"), endpoints);
wsClientsAnnotation.addAttribute(newEndpoints);
if (StringUtils.isNotEmpty(profile)) {
wsClientsAnnotation.addStringAttribute("profile", profile);
}
// Include new @RooWsClients annotation
cidBuilder.addAnnotation(wsClientsAnnotation);
}
getTypeManagementService().createOrUpdateTypeOnDisk(cidBuilder.build());
// Compile project to be able to generate necessary resources.
// Is necessary to create new thread and wat for it.
Thread generateSourcesThread = new Thread() {
public void run() {
try {
Thread.sleep(1000);
final StringBuilder sb = new StringBuilder();
sb.append(LINE_SEPARATOR);
sb.append(LINE_SEPARATOR);
sb.append("##########################################################").append(LINE_SEPARATOR);
sb.append("##########################################################").append(LINE_SEPARATOR);
sb.append("################# Generating client sources ##############").append(LINE_SEPARATOR);
sb.append("##########################################################").append(LINE_SEPARATOR);
sb.append("##########################################################").append(LINE_SEPARATOR);
sb.append("#").append(LINE_SEPARATOR);
sb.append("# Please wait...").append(LINE_SEPARATOR);
sb.append("# Don't execute any command until this operation finishes.").append(LINE_SEPARATOR);
sb.append("#").append(LINE_SEPARATOR);
sb.append(LINE_SEPARATOR);
sb.append(LINE_SEPARATOR);
LOGGER.log(Level.INFO, sb.toString());
// Changing focus to the module where the .wsdl file is located
getProjectOperations().setModule(wsdlModule);
// executing mvn generate-sources command
getMavenOperations().executeMvnCommand("generate-sources");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
generateSourcesThread.start();
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class MethodMatcher method getSuffix.
private String getSuffix(final List<MemberHoldingTypeDetails> memberHoldingTypeDetailsList, final boolean singular, final Map<String, String> pluralMap) {
final ClassOrInterfaceTypeDetails cid = getMostConcreteClassOrInterfaceTypeDetails(memberHoldingTypeDetailsList);
if (singular) {
return cid.getName().getSimpleTypeName();
}
String plural = pluralMap.get(cid.getDeclaredByMetadataId());
for (final AnnotationMetadata annotationMetadata : cid.getAnnotations()) {
if (annotationMetadata.getAnnotationType().getFullyQualifiedTypeName().equals(ROO_PLURAL.getFullyQualifiedTypeName())) {
final AnnotationAttributeValue<?> annotationAttributeValue = annotationMetadata.getAttribute(new JavaSymbolName("value"));
if (annotationAttributeValue != null) {
plural = annotationAttributeValue.getValue().toString();
}
break;
}
}
if (StringUtils.isNotBlank(plural)) {
plural = StringUtils.capitalize(plural);
}
return plural;
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class ItdSourceFileComposer method appendInnerTypes.
/**
* supports static inner types with static field definitions only at this
* point
*/
private void appendInnerTypes() {
final List<ClassOrInterfaceTypeDetails> innerTypes = itdTypeDetails.getInnerTypes();
for (final ClassOrInterfaceTypeDetails innerType : innerTypes) {
content = true;
appendIndent();
if (innerType.getModifier() != 0) {
append(Modifier.toString(innerType.getModifier()));
append(" ");
}
append("class ");
append(introductionTo.getNameIncludingTypeParameters());
append(".");
append(innerType.getName().getSimpleTypeName());
if (innerType.getExtendsTypes().size() > 0) {
append(" extends ");
// There should only be one extends type for inner classes
final JavaType extendsType = innerType.getExtendsTypes().get(0);
if (resolver.isFullyQualifiedFormRequiredAfterAutoImport(extendsType)) {
append(extendsType.getNameIncludingTypeParameters());
} else {
append(extendsType.getNameIncludingTypeParameters(false, resolver));
}
append(" ");
}
final List<JavaType> implementsTypes = innerType.getImplementsTypes();
if (implementsTypes.size() > 0) {
append(" implements ");
for (int i = 0; i < implementsTypes.size(); i++) {
final JavaType implementsType = implementsTypes.get(i);
if (resolver.isFullyQualifiedFormRequiredAfterAutoImport(implementsType)) {
append(implementsType.getNameIncludingTypeParameters());
} else {
append(implementsType.getNameIncludingTypeParameters(false, resolver));
}
if (i != implementsTypes.size() - 1) {
append(", ");
} else {
append(" ");
}
}
}
append("{");
this.newLine(false);
// Write out fields
for (final FieldMetadata field : innerType.getDeclaredFields()) {
indent();
this.newLine(false);
// Append annotations
for (final AnnotationMetadata annotation : field.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
appendIndent();
if (field.getModifier() != 0) {
append(Modifier.toString(field.getModifier()));
append(" ");
}
append(field.getFieldType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(field.getFieldName().getSymbolName());
// Append initializer, if present
if (field.getFieldInitializer() != null) {
append(" = ");
append(field.getFieldInitializer());
}
// Complete the field declaration
append(";");
this.newLine(false);
indentRemove();
}
this.newLine(false);
// Write out constructors
indent();
writeInnerTypeConstructors(innerType.getName(), innerType.getDeclaredConstructors(), false, false);
indentRemove();
// Write out methods
indent();
writeMethods(innerType.getDeclaredMethods(), false, false);
indentRemove();
appendIndent();
append("}");
this.newLine(false);
this.newLine();
}
}
use of org.springframework.roo.classpath.details.annotations.AnnotationMetadata in project spring-roo by spring-projects.
the class ItdSourceFileComposer method appendConstructors.
private void appendConstructors() {
final List<? extends ConstructorMetadata> constructors = itdTypeDetails.getDeclaredConstructors();
if (constructors == null || constructors.isEmpty()) {
return;
}
content = true;
for (final ConstructorMetadata constructor : constructors) {
Validate.isTrue(constructor.getParameterTypes().size() == constructor.getParameterNames().size(), "Mismatched parameter names against parameter types");
// ROO-3447: Append comments if exists
CommentStructure commentStructure = constructor.getCommentStructure();
if (commentStructure != null && commentStructure.getBeginComments() != null) {
List<AbstractComment> constructorComments = commentStructure.getBeginComments();
String comment = "";
boolean missingComponentsAdded = false;
for (AbstractComment constructorComment : constructorComments) {
// Join all JavadocComment's
if (constructorComment instanceof JavadocComment) {
// Add JavaDoc missing components
if (!missingComponentsAdded) {
if (!constructor.getParameterNames().isEmpty() && ((JavadocComment) constructorComment).getParamsInfo() == null) {
List<String> paramsInfo = new ArrayList<String>();
for (JavaSymbolName name : constructor.getParameterNames()) {
paramsInfo.add(name.getSymbolName());
}
((JavadocComment) constructorComment).setParamsInfo(paramsInfo);
}
}
missingComponentsAdded = true;
comment = comment.concat(constructorComment.getComment()).concat(IOUtils.LINE_SEPARATOR);
} else {
// Not JavadocComment, write comment as it is, unchanged
appendFormalLine(constructorComment.getComment());
}
}
// Write JavadocComment's to ITD, in a single JavadocComment instance
String[] commentLines = comment.split(IOUtils.LINE_SEPARATOR);
for (String commentLine : commentLines) {
appendFormalLine(commentLine);
}
} else {
// ROO-3834: Append default Javadoc if not exists a comment structure,
// including constructor params
CommentStructure defaultCommentStructure = new CommentStructure();
List<String> parameterNames = new ArrayList<String>();
for (JavaSymbolName name : constructor.getParameterNames()) {
parameterNames.add(name.getSymbolName());
}
JavadocComment javadocComment = new JavadocComment("TODO Auto-generated constructor documentation", parameterNames, null, null);
defaultCommentStructure.addComment(javadocComment, CommentLocation.BEGINNING);
constructor.setCommentStructure(defaultCommentStructure);
// Now lines should be formatted, so write them
String[] comment = javadocComment.getComment().split(IOUtils.LINE_SEPARATOR);
for (String line : comment) {
appendFormalLine(line);
}
}
// Append annotations
for (final AnnotationMetadata annotation : constructor.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <TargetOfIntroduction>.new" portion
appendIndent();
if (constructor.getModifier() != 0) {
append(Modifier.toString(constructor.getModifier()));
append(" ");
}
append(introductionTo.getSimpleTypeName());
append(".");
append("new");
// Append parameter types and names
append("(");
final List<AnnotatedJavaType> parameterTypes = constructor.getParameterTypes();
final List<JavaSymbolName> parameterNames = constructor.getParameterNames();
for (int i = 0; i < parameterTypes.size(); i++) {
final AnnotatedJavaType paramType = parameterTypes.get(i);
final JavaSymbolName paramName = parameterNames.get(i);
for (final AnnotationMetadata methodParameterAnnotation : paramType.getAnnotations()) {
append(AnnotationMetadataUtils.toSourceForm(methodParameterAnnotation, resolver));
append(" ");
}
append(paramType.getJavaType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(paramName.getSymbolName());
if (i < parameterTypes.size() - 1) {
append(", ");
}
}
append(") {");
this.newLine(false);
indent();
// Add body
append(constructor.getBody());
indentRemove();
appendFormalLine("}");
this.newLine(false);
}
}
Aggregations