use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class JavaPackageConverterTest method testConvertFromCompoundPackageNameInUpdateContext.
@Test
public void testConvertFromCompoundPackageNameInUpdateContext() {
assertConvertFromValidText("COM.example", "update", TOP_LEVEL_PACKAGE);
verify(mockLastUsed).setPackage(new JavaPackage(TOP_LEVEL_PACKAGE), mockPom);
}
use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class JpaCommands method getClassPossibleValues.
/**
* Indicator that provides all possible values for --class parameter The
* provided results will not be validate. It will not include space on finish.
*
* @param shellContext
* @return List with all possible values for --class parameter
*/
@CliOptionAutocompleteIndicator(command = "entity jpa", param = "class", help = "Provided --class option should be a class annotated with @RooJpaEntity.", validate = false, includeSpaceOnFinish = false)
public List<String> getClassPossibleValues(ShellContext shellContext) {
List<String> allPossibleValues = new ArrayList<String>();
// Add all modules to completions list
if (projectOperations.isMultimoduleProject()) {
Collection<String> modules = projectOperations.getModuleNames();
for (String module : modules) {
// Ignore root module
if (StringUtils.isBlank(module)) {
continue;
}
// Ignore module name if it is the focused module
if (module.equals(projectOperations.getFocusedModule().getModuleName())) {
List<JavaPackage> modulePackages = typeLocationService.getPackagesForModule(module);
// Always add module top level package and project top level package
modulePackages.add(projectOperations.getTopLevelPackage(module));
for (JavaPackage javaPackage : modulePackages) {
// Check if package name contains top level package to shorten it
String currentPackageName = getPackageStringValue(module, javaPackage.getFullyQualifiedPackageName());
// Add package to possible values
if (!allPossibleValues.contains(currentPackageName.concat("."))) {
allPossibleValues.add(currentPackageName.concat("."));
}
}
} else {
// It is not the focused module
List<JavaPackage> modulePackages = typeLocationService.getPackagesForModule(module);
// Always add module top level package and project top level package
modulePackages.add(projectOperations.getTopLevelPackage(module));
for (JavaPackage javaPackage : modulePackages) {
// Check if package name contains top level package to shorten it
String currentPackageName = getPackageStringValue(module, javaPackage.getFullyQualifiedPackageName());
// Add package to possible values
String valueToAdd = String.format("%s%s%s.", module, LogicalPath.MODULE_PATH_SEPARATOR, currentPackageName);
if (!allPossibleValues.contains(valueToAdd)) {
allPossibleValues.add(valueToAdd);
}
}
}
}
} else {
// Check all JavaPackages in single module project
for (JavaPackage javaPackage : typeLocationService.getPackagesForModule("")) {
// Check if package name contains top level package to shorten it
String currentPackageName = getPackageStringValue("", javaPackage.getFullyQualifiedPackageName());
// Add package to possible values
if (!allPossibleValues.contains(currentPackageName.concat("."))) {
allPossibleValues.add(currentPackageName.concat("."));
}
}
}
return allPossibleValues;
}
use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class RepositoryJpaCommands method repository.
@CliCommand(value = "repository jpa", help = "Generates new Spring Data repository for specified entity or for all entities in generated " + "project.")
public void repository(@CliOption(key = "all", mandatory = false, specifiedDefaultValue = "true", unspecifiedDefaultValue = "false", help = "Indicates if developer wants to generate repositories for every entity of current " + "project. " + "This option is mandatory if `--entity` is not specified. Otherwise, using `--entity` " + "will cause the parameter `--all` won't be available. " + "Default if option present: `true`; default if option not present: `false`.") boolean all, @CliOption(key = "entity", mandatory = false, optionContext = PROJECT, help = "The domain entity this repository should manage. When working on a single module " + "project, simply specify the name of the entity. If you consider it necessary, you can " + "also specify the package. Ex.: `--class ~.domain.MyEntity` (where `~` is the base package). " + "When working with multiple modules, you should specify the name of the entity and the " + "module where it is. Ex.: `--class model:~.domain.MyEntity`. If the module is not specified, " + "it is assumed that the entity is in the module which has the focus. " + "Possible values are: any of the entities in the project. " + "This option is mandatory if `--all` is not specified. Otherwise, using `--all` " + "will cause the parameter `--entity` won't be available.") final JavaType domainType, @CliOption(key = "interface", mandatory = true, help = "The java Spring Data repository to generate. When working on a single module " + "project, simply specify the name of the class. If you consider it necessary, you can " + "also specify the package. Ex.: `--class ~.domain.MyClass` (where `~` is the base package). " + "When working with multiple modules, you should specify the name of the class and the " + "module where it is. Ex.: `--class model:~.domain.MyClass`. If the module is not specified, " + "it is assumed that the class is in the module which has the focus. " + "This option is mandatory if `--entity` has been already specified and the project is " + "multi-module. " + "This option is available only when `--entity` has been specified.") final JavaType interfaceType, @CliOption(key = "defaultReturnType", mandatory = false, help = "The default return type which this repository will have for all finders, including those" + "created by default. The default return type should be a Projection class associated to " + "the entity specified in `--entity` parameter. " + "Possible values are: any of the projections associated to the entity in `--entity` option. " + "This option is not available if domain entity specified in `--entity` parameter has no " + "associated Projections. " + "Default: the entity specified in the `entity` option.") JavaType defaultReturnType, @CliOption(key = "package", mandatory = false, help = "The package where repositories will be generated. In multi-module project you should " + "specify the module name before the package name. " + "Ex.: `--package model:org.springframework.roo` but, if module name is not present, " + "the Roo Shell focused module will be used. " + "This option is not available if `--all` option has not been specified. " + "Default value if not present: `~.repository` package, or 'repository:~.' if multi-module " + "project.") JavaPackage repositoriesPackage) {
if (all) {
// If user didn't specified some JavaPackage, use default repository package
if (repositoriesPackage == null) {
if (projectOperations.isMultimoduleProject()) {
// Build default JavaPackage with module
for (String moduleName : projectOperations.getModuleNames()) {
if (moduleName.equals("repository")) {
Pom module = projectOperations.getPomFromModuleName(moduleName);
repositoriesPackage = new JavaPackage(typeLocationService.getTopLevelPackageForModule(module), moduleName);
break;
}
}
// Check if repository found
Validate.notNull(repositoriesPackage, "Couldn't find in project a default 'repository' " + "module. Please, use 'package' option to specify module and package.");
} else {
// Build default JavaPackage for single module
repositoriesPackage = new JavaPackage(projectOperations.getFocusedTopLevelPackage().getFullyQualifiedPackageName().concat(".repository"), projectOperations.getFocusedModuleName());
}
}
repositoryJpaOperations.generateAllRepositories(repositoriesPackage);
} else {
repositoryJpaOperations.addRepository(interfaceType, domainType, defaultReturnType, true);
}
}
use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class JavaParserClassOrInterfaceTypeDetailsBuilder method build.
@Override
public ClassOrInterfaceTypeDetails build() {
Validate.notEmpty(compilationUnit.getTypes(), "No types in compilation unit, so unable to continue parsing");
ClassOrInterfaceDeclaration clazz = null;
EnumDeclaration enumClazz = null;
final StringBuilder sb = new StringBuilder(compilationUnit.getPackage().getName().toString());
if (name.getEnclosingType() != null) {
sb.append(".").append(name.getEnclosingType().getSimpleTypeName());
}
compilationUnitPackage = new JavaPackage(sb.toString());
// Determine the type name, adding type parameters if possible
final JavaType newName = JavaParserUtils.getJavaType(compilationUnitServices, typeDeclaration);
// Revert back to the original type name (thus avoiding unnecessary
// inferences about java.lang types; see ROO-244)
name = new JavaType(newName.getFullyQualifiedTypeName(), newName.getEnclosingType(), newName.getArray(), newName.getDataType(), newName.getArgName(), newName.getParameters(), name.getModule());
final ClassOrInterfaceTypeDetailsBuilder cidBuilder = new ClassOrInterfaceTypeDetailsBuilder(declaredByMetadataId);
physicalTypeCategory = PhysicalTypeCategory.CLASS;
if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
clazz = (ClassOrInterfaceDeclaration) typeDeclaration;
if (clazz.isInterface()) {
physicalTypeCategory = PhysicalTypeCategory.INTERFACE;
}
} else if (typeDeclaration instanceof EnumDeclaration) {
enumClazz = (EnumDeclaration) typeDeclaration;
physicalTypeCategory = PhysicalTypeCategory.ENUMERATION;
}
Validate.notNull(physicalTypeCategory, "%s (%s for %s)", UNSUPPORTED_MESSAGE_PREFIX, typeDeclaration.getClass().getSimpleName(), name);
cidBuilder.setName(name);
cidBuilder.setPhysicalTypeCategory(physicalTypeCategory);
imports = compilationUnit.getImports();
if (imports == null) {
imports = new ArrayList<ImportDeclaration>();
compilationUnit.setImports(imports);
}
// Verify the package declaration appears to be correct
if (compilationUnitPackage.equals(name.getPackage()) != true) {
String warningStr = "[Warning] Compilation unit package '" + compilationUnitPackage + "' unexpected for type '" + name.getPackage() + "', it may be a nested class.";
LOGGER.log(Level.WARNING, warningStr);
}
for (final ImportDeclaration importDeclaration : imports) {
if (importDeclaration.getName() instanceof QualifiedNameExpr) {
final String qualifier = ((QualifiedNameExpr) importDeclaration.getName()).getQualifier().toString();
final String simpleName = importDeclaration.getName().getName();
final String fullName = qualifier + "." + simpleName;
// We want to calculate these...
final JavaType type = new JavaType(fullName);
final JavaPackage typePackage = importDeclaration.isAsterisk() ? new JavaPackage(fullName) : type.getPackage();
// Process any comments for the import
final CommentStructure commentStructure = new CommentStructure();
JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, importDeclaration);
final ImportMetadataBuilder newImport = new ImportMetadataBuilder(declaredByMetadataId, 0, typePackage, type, importDeclaration.isStatic(), importDeclaration.isAsterisk());
newImport.setCommentStructure(commentStructure);
cidBuilder.add(newImport.build());
}
}
// Convert Java Parser modifier into JDK modifier
cidBuilder.setModifier(JavaParserUtils.getJdkModifier(typeDeclaration.getModifiers()));
// Type parameters
final Set<JavaSymbolName> typeParameterNames = new HashSet<JavaSymbolName>();
for (final JavaType param : name.getParameters()) {
final JavaSymbolName arg = param.getArgName();
// Fortunately type names can only appear at the top-level
if (arg != null && !JavaType.WILDCARD_NEITHER_ARG.equals(arg) && !JavaType.WILDCARD_EXTENDS_ARG.equals(arg) && !JavaType.WILDCARD_SUPER_ARG.equals(arg)) {
typeParameterNames.add(arg);
}
}
List<ClassOrInterfaceType> implementsList;
List<AnnotationExpr> annotationsList = null;
List<BodyDeclaration> members = null;
if (clazz != null) {
final List<ClassOrInterfaceType> extendsList = clazz.getExtends();
if (extendsList != null) {
for (final ClassOrInterfaceType candidate : extendsList) {
final JavaType javaType = JavaParserUtils.getJavaTypeNow(compilationUnitServices, candidate, typeParameterNames);
cidBuilder.addExtendsTypes(javaType);
}
}
final List<JavaType> extendsTypes = cidBuilder.getExtendsTypes();
// Obtain the superclass, if this is a class and one is available
if (physicalTypeCategory == PhysicalTypeCategory.CLASS && extendsTypes.size() == 1) {
final JavaType superclass = extendsTypes.get(0);
final String superclassId = typeLocationService.getPhysicalTypeIdentifier(superclass);
PhysicalTypeMetadata superPtm = null;
if (superclassId != null) {
superPtm = (PhysicalTypeMetadata) metadataService.get(superclassId);
}
if (superPtm != null && superPtm.getMemberHoldingTypeDetails() != null) {
cidBuilder.setSuperclass(superPtm.getMemberHoldingTypeDetails());
}
}
implementsList = clazz.getImplements();
if (implementsList != null) {
for (final ClassOrInterfaceType candidate : implementsList) {
final JavaType javaType = JavaParserUtils.getJavaTypeNow(compilationUnitServices, candidate, typeParameterNames);
cidBuilder.addImplementsType(javaType);
}
}
annotationsList = typeDeclaration.getAnnotations();
members = clazz.getMembers();
}
if (enumClazz != null) {
final List<EnumConstantDeclaration> constants = enumClazz.getEntries();
if (constants != null) {
for (final EnumConstantDeclaration enumConstants : constants) {
cidBuilder.addEnumConstant(new JavaSymbolName(enumConstants.getName()));
}
}
implementsList = enumClazz.getImplements();
annotationsList = enumClazz.getAnnotations();
members = enumClazz.getMembers();
}
if (annotationsList != null) {
for (final AnnotationExpr candidate : annotationsList) {
final AnnotationMetadata md = JavaParserAnnotationMetadataBuilder.getInstance(candidate, compilationUnitServices).build();
final CommentStructure commentStructure = new CommentStructure();
JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, candidate);
md.setCommentStructure(commentStructure);
cidBuilder.addAnnotation(md);
}
}
if (members != null) {
// type in the signature of the enclosing type
for (final BodyDeclaration bodyDeclaration : members) {
if (bodyDeclaration instanceof TypeDeclaration) {
// Found a type
innerTypes.add((TypeDeclaration) bodyDeclaration);
}
}
for (final BodyDeclaration member : members) {
if (member instanceof FieldDeclaration) {
final FieldDeclaration castMember = (FieldDeclaration) member;
for (final VariableDeclarator var : castMember.getVariables()) {
final FieldMetadata field = JavaParserFieldMetadataBuilder.getInstance(declaredByMetadataId, castMember, var, compilationUnitServices, typeParameterNames).build();
final CommentStructure commentStructure = new CommentStructure();
JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
field.setCommentStructure(commentStructure);
cidBuilder.addField(field);
}
}
if (member instanceof MethodDeclaration) {
final MethodDeclaration castMember = (MethodDeclaration) member;
final MethodMetadata method = JavaParserMethodMetadataBuilder.getInstance(declaredByMetadataId, castMember, compilationUnitServices, typeParameterNames).build();
final CommentStructure commentStructure = new CommentStructure();
JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
method.setCommentStructure(commentStructure);
cidBuilder.addMethod(method);
}
if (member instanceof ConstructorDeclaration) {
final ConstructorDeclaration castMember = (ConstructorDeclaration) member;
final ConstructorMetadata constructor = JavaParserConstructorMetadataBuilder.getInstance(declaredByMetadataId, castMember, compilationUnitServices, typeParameterNames).build();
final CommentStructure commentStructure = new CommentStructure();
JavaParserCommentMetadataBuilder.updateCommentsToRoo(commentStructure, member);
constructor.setCommentStructure(commentStructure);
cidBuilder.addConstructor(constructor);
}
if (member instanceof TypeDeclaration) {
final TypeDeclaration castMember = (TypeDeclaration) member;
final JavaType innerType = new JavaType(castMember.getName(), name);
final String innerTypeMetadataId = PhysicalTypeIdentifier.createIdentifier(innerType, PhysicalTypeIdentifier.getPath(declaredByMetadataId));
final ClassOrInterfaceTypeDetails cid = new JavaParserClassOrInterfaceTypeDetailsBuilder(compilationUnit, compilationUnitServices, castMember, innerTypeMetadataId, innerType, metadataService, typeLocationService).build();
cidBuilder.addInnerType(cid);
}
}
}
return cidBuilder.build();
}
use of org.springframework.roo.model.JavaPackage in project spring-roo by spring-projects.
the class JavaParserTypeParsingService method updateOutput.
/**
* Appends the presented class to the end of the presented body
* declarations. The body declarations appear within the presented
* compilation unit. This is used to progressively build inner types.
*
* @param compilationUnit the work-in-progress compilation unit (required)
* @param enclosingCompilationUnitServices
* @param cid the new class to add (required)
* @param parent the class body declarations a subclass should be added to
* (may be null, which denotes a top-level type within the
* compilation unit)
*/
private void updateOutput(final CompilationUnit compilationUnit, CompilationUnitServices enclosingCompilationUnitServices, final ClassOrInterfaceTypeDetails cid, final List<BodyDeclaration> parent) {
// Append the new imports this class declares
Validate.notNull(compilationUnit.getImports(), "Compilation unit imports should be non-null when producing type '%s'", cid.getName());
for (final ImportMetadata importType : cid.getRegisteredImports()) {
ImportDeclaration importDeclaration;
if (!importType.isAsterisk()) {
NameExpr typeToImportExpr;
if (importType.getImportType().getEnclosingType() == null) {
typeToImportExpr = new QualifiedNameExpr(new NameExpr(importType.getImportType().getPackage().getFullyQualifiedPackageName()), importType.getImportType().getSimpleTypeName());
} else {
typeToImportExpr = new QualifiedNameExpr(new NameExpr(importType.getImportType().getEnclosingType().getFullyQualifiedTypeName()), importType.getImportType().getSimpleTypeName());
}
importDeclaration = new ImportDeclaration(typeToImportExpr, importType.isStatic(), false);
} else {
importDeclaration = new ImportDeclaration(new NameExpr(importType.getImportPackage().getFullyQualifiedPackageName()), importType.isStatic(), importType.isAsterisk());
}
JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(importDeclaration, importType.getCommentStructure());
compilationUnit.getImports().add(importDeclaration);
}
// Create a class or interface declaration to represent this actual type
final int javaParserModifier = JavaParserUtils.getJavaParserModifier(cid.getModifier());
TypeDeclaration typeDeclaration;
ClassOrInterfaceDeclaration classOrInterfaceDeclaration;
// Implements handling
final List<ClassOrInterfaceType> implementsList = new ArrayList<ClassOrInterfaceType>();
for (final JavaType current : cid.getImplementsTypes()) {
implementsList.add(JavaParserUtils.getResolvedName(cid.getName(), current, compilationUnit));
}
if (cid.getPhysicalTypeCategory() == PhysicalTypeCategory.INTERFACE || cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS) {
final boolean isInterface = cid.getPhysicalTypeCategory() == PhysicalTypeCategory.INTERFACE;
if (parent == null) {
// Top level type
typeDeclaration = new ClassOrInterfaceDeclaration(javaParserModifier, isInterface, cid.getName().getNameIncludingTypeParameters().replace(cid.getName().getPackage().getFullyQualifiedPackageName() + ".", ""));
classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
} else {
// Inner type
typeDeclaration = new ClassOrInterfaceDeclaration(javaParserModifier, isInterface, cid.getName().getSimpleTypeName());
classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
if (cid.getName().getParameters().size() > 0) {
classOrInterfaceDeclaration.setTypeParameters(new ArrayList<TypeParameter>());
for (final JavaType param : cid.getName().getParameters()) {
NameExpr pNameExpr = JavaParserUtils.importTypeIfRequired(cid.getName(), compilationUnit.getImports(), param);
final String tempName = StringUtils.replace(pNameExpr.toString(), param.getArgName() + " extends ", "", 1);
pNameExpr = new NameExpr(tempName);
final ClassOrInterfaceType pResolvedName = JavaParserUtils.getClassOrInterfaceType(pNameExpr);
classOrInterfaceDeclaration.getTypeParameters().add(new TypeParameter(param.getArgName().getSymbolName(), Collections.singletonList(pResolvedName)));
}
}
}
// Superclass handling
final List<ClassOrInterfaceType> extendsList = new ArrayList<ClassOrInterfaceType>();
for (final JavaType current : cid.getExtendsTypes()) {
if (!OBJECT.equals(current)) {
extendsList.add(JavaParserUtils.getResolvedName(cid.getName(), current, compilationUnit));
}
}
if (extendsList.size() > 0) {
classOrInterfaceDeclaration.setExtends(extendsList);
}
// Implements handling
if (implementsList.size() > 0) {
classOrInterfaceDeclaration.setImplements(implementsList);
}
} else {
typeDeclaration = new EnumDeclaration(javaParserModifier, cid.getName().getSimpleTypeName());
}
typeDeclaration.setMembers(new ArrayList<BodyDeclaration>());
Validate.notNull(typeDeclaration.getName(), "Missing type declaration name for '%s'", cid.getName());
// If adding a new top-level type, must add it to the compilation unit
// types
Validate.notNull(compilationUnit.getTypes(), "Compilation unit types must not be null when attempting to add '%s'", cid.getName());
if (parent == null) {
// Top-level class
compilationUnit.getTypes().add(typeDeclaration);
} else {
// Inner class
parent.add(typeDeclaration);
}
// CompilationUnitServices needs to be created
if (enclosingCompilationUnitServices == null) {
// Create a compilation unit so that we can use JavaType*Metadata
// static methods directly
enclosingCompilationUnitServices = new CompilationUnitServices() {
@Override
public JavaPackage getCompilationUnitPackage() {
return cid.getName().getPackage();
}
@Override
public JavaType getEnclosingTypeName() {
return cid.getName();
}
@Override
public List<ImportDeclaration> getImports() {
return compilationUnit.getImports();
}
@Override
public List<TypeDeclaration> getInnerTypes() {
return compilationUnit.getTypes();
}
@Override
public PhysicalTypeCategory getPhysicalTypeCategory() {
return cid.getPhysicalTypeCategory();
}
};
}
final CompilationUnitServices finalCompilationUnitServices = enclosingCompilationUnitServices;
// A hybrid CompilationUnitServices must be provided that references the
// enclosing types imports and package
final CompilationUnitServices compilationUnitServices = new CompilationUnitServices() {
@Override
public JavaPackage getCompilationUnitPackage() {
return finalCompilationUnitServices.getCompilationUnitPackage();
}
@Override
public JavaType getEnclosingTypeName() {
return cid.getName();
}
@Override
public List<ImportDeclaration> getImports() {
return finalCompilationUnitServices.getImports();
}
@Override
public List<TypeDeclaration> getInnerTypes() {
return compilationUnit.getTypes();
}
@Override
public PhysicalTypeCategory getPhysicalTypeCategory() {
return cid.getPhysicalTypeCategory();
}
};
// Add type annotations
final List<AnnotationExpr> annotations = new ArrayList<AnnotationExpr>();
typeDeclaration.setAnnotations(annotations);
for (final AnnotationMetadata candidate : cid.getAnnotations()) {
JavaParserAnnotationMetadataBuilder.addAnnotationToList(compilationUnitServices, annotations, candidate);
}
// ROO-3834: Generating default Javadoc inside class if class doesn't contains JavaDoc
List<Comment> classComments = compilationUnit.getComments();
if (classComments == null || classComments.isEmpty()) {
CommentStructure defaultCommentStructure = new CommentStructure();
String defaultComment = "= ".concat(cid.getType().getSimpleTypeName()).concat("\n \nTODO Auto-generated class documentation");
defaultCommentStructure.addComment(new JavadocComment(defaultComment), CommentLocation.BEGINNING);
if (annotations.isEmpty()) {
JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(typeDeclaration, defaultCommentStructure);
} else {
// If exists some annotation, include comment before the existing annotations
AnnotationExpr firstAnnotation = annotations.get(0);
JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(firstAnnotation, defaultCommentStructure);
}
}
// Add enum constants and interfaces
if (typeDeclaration instanceof EnumDeclaration && cid.getEnumConstants().size() > 0) {
final EnumDeclaration enumDeclaration = (EnumDeclaration) typeDeclaration;
final List<EnumConstantDeclaration> constants = new ArrayList<EnumConstantDeclaration>();
enumDeclaration.setEntries(constants);
for (final JavaSymbolName constant : cid.getEnumConstants()) {
addEnumConstant(constants, constant);
}
// Implements handling
if (implementsList.size() > 0) {
enumDeclaration.setImplements(implementsList);
}
}
// Add fields
for (final FieldMetadata candidate : cid.getDeclaredFields()) {
JavaParserFieldMetadataBuilder.addField(compilationUnitServices, typeDeclaration.getMembers(), candidate);
}
// Add constructors
for (final ConstructorMetadata candidate : cid.getDeclaredConstructors()) {
JavaParserConstructorMetadataBuilder.addConstructor(compilationUnitServices, typeDeclaration.getMembers(), candidate, null);
}
// Add methods
for (final MethodMetadata candidate : cid.getDeclaredMethods()) {
JavaParserMethodMetadataBuilder.addMethod(compilationUnitServices, typeDeclaration.getMembers(), candidate, null);
}
// Add inner types
for (final ClassOrInterfaceTypeDetails candidate : cid.getDeclaredInnerTypes()) {
updateOutput(compilationUnit, compilationUnitServices, candidate, typeDeclaration.getMembers());
}
final HashSet<String> imported = new HashSet<String>();
final ArrayList<ImportDeclaration> imports = new ArrayList<ImportDeclaration>();
for (final ImportDeclaration importDeclaration : compilationUnit.getImports()) {
JavaPackage importPackage = null;
JavaType importType = null;
if (importDeclaration.isAsterisk()) {
importPackage = new JavaPackage(importDeclaration.getName().toString());
} else {
importType = new JavaType(importDeclaration.getName().toString());
importPackage = importType.getPackage();
}
if (importPackage.equals(cid.getName().getPackage()) && importDeclaration.isAsterisk()) {
continue;
}
if (importPackage.equals(cid.getName().getPackage()) && importType != null && importType.getEnclosingType() == null) {
continue;
}
if (importType != null && importType.equals(cid.getName())) {
continue;
}
if (!imported.contains(importDeclaration.getName().toString())) {
imports.add(importDeclaration);
imported.add(importDeclaration.getName().toString());
}
}
Collections.sort(imports, new Comparator<ImportDeclaration>() {
@Override
public int compare(final ImportDeclaration importDeclaration, final ImportDeclaration importDeclaration1) {
return importDeclaration.getName().toString().compareTo(importDeclaration1.getName().toString());
}
});
compilationUnit.setImports(imports);
}
Aggregations