public boolean isAdditionLegal(final JavaType javaType) { Validate.notNull(javaType, "Java type required"); if (javaType.getDataType() != DataType.TYPE) { // It's a type variable or primitive return false; } if (javaType.isDefaultPackage()) { // Cannot import types from the default package return false; } // Must be a class, so it's legal if there isn't an existing // registration that conflicts for (final Entry<JavaType, Boolean> entry : registeredImports.entrySet()) { JavaType candidate = entry.getKey(); if (candidate.getSimpleTypeName().equals(javaType.getSimpleTypeName())) { // Conflict detected return false; } } return true; }
private void prependCompilationUnitDetails() { final StringBuilder topOfFile = new StringBuilder(); topOfFile.append("// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.") .append(NEW_LINE); topOfFile .append( "// You may push code into the target .java compilation unit if you wish to edit any member(s).") .append(NEW_LINE).append(NEW_LINE); // Note we're directly interacting with the top of file string builder if (!aspect.isDefaultPackage()) { topOfFile.append("package ").append(aspect.getPackage().getFullyQualifiedPackageName()) .append(";").append(NEW_LINE); topOfFile.append(NEW_LINE); } // Ordered to ensure consistency of output final SortedMap<JavaType, Boolean> types = new TreeMap<JavaType, Boolean>(); types.putAll(resolver.getRegisteredImports()); if (!types.isEmpty()) { // First of all include the static imports writeStaticImports(topOfFile, types); // After static imports, include the other ones writeImports(topOfFile, types); topOfFile.append(NEW_LINE); } // Now append the normal file to the bottom topOfFile.append(pw.toString()); // Replace the old writer with out new writer pw = topOfFile; }
/** * @return the package name (never null) */ public JavaPackage getPackage() { if (isDefaultPackage() && !Character.isUpperCase(fullyQualifiedTypeName.charAt(0))) { return new JavaPackage("", module); } if (enclosingType != null) { final String enclosingTypeFullyQualifiedTypeName = enclosingType.getFullyQualifiedTypeName(); final int offset = enclosingTypeFullyQualifiedTypeName.lastIndexOf("."); // Handle case where the package name after the last period starts // with a capital letter. if (offset > -1 && Character.isUpperCase(enclosingTypeFullyQualifiedTypeName.charAt(offset + 1))) { return new JavaPackage(enclosingTypeFullyQualifiedTypeName, module); } return enclosingType.getPackage(); } final int offset = fullyQualifiedTypeName.lastIndexOf("."); return offset == -1 ? new JavaPackage("") : new JavaPackage(fullyQualifiedTypeName.substring(0, offset), module); }
@Override public final String getCompilationUnitContents(final ClassOrInterfaceTypeDetails cid) { Validate.notNull(cid, "Class or interface type details are required"); // Create a compilation unit to store the type to be created final CompilationUnit compilationUnit = new CompilationUnit(); // NB: this import list is replaced at the end of this method by a // sorted version compilationUnit.setImports(new ArrayList<ImportDeclaration>()); if (!cid.getName().isDefaultPackage()) { compilationUnit.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(cid.getName() .getPackage().getFullyQualifiedPackageName()))); } // Add the class of interface declaration to the compilation unit final List<TypeDeclaration> types = new ArrayList<TypeDeclaration>(); compilationUnit.setTypes(types); updateOutput(compilationUnit, null, cid, null); return compilationUnit.toString(); }
if (typeToImport.isDefaultPackage()) { return new NameExpr(typeToImport.getSimpleTypeName());
private void prependCompilationUnitDetails() { final StringBuilder topOfFile = new StringBuilder(); topOfFile.append("// WARNING: DO NOT EDIT THIS FILE. THIS FILE IS MANAGED BY SPRING ROO.") .append(NEW_LINE); topOfFile .append( "// You may push code into the target .java compilation unit if you wish to edit any member(s).") .append(NEW_LINE).append(NEW_LINE); // Note we're directly interacting with the top of file string builder if (!aspect.isDefaultPackage()) { topOfFile.append("package ").append(aspect.getPackage().getFullyQualifiedPackageName()) .append(";").append(NEW_LINE); topOfFile.append(NEW_LINE); } // Ordered to ensure consistency of output final SortedMap<JavaType, Boolean> types = new TreeMap<JavaType, Boolean>(); types.putAll(resolver.getRegisteredImports()); if (!types.isEmpty()) { // First of all include the static imports writeStaticImports(topOfFile, types); // After static imports, include the other ones writeImports(topOfFile, types); topOfFile.append(NEW_LINE); } // Now append the normal file to the bottom topOfFile.append(pw.toString()); // Replace the old writer with out new writer pw = topOfFile; }