diff --git a/src/main/java/org/seed/core/codegen/AbstractSourceCodeBuilder.java b/src/main/java/org/seed/core/codegen/AbstractSourceCodeBuilder.java index bef7c3789..f4d793630 100644 --- a/src/main/java/org/seed/core/codegen/AbstractSourceCodeBuilder.java +++ b/src/main/java/org/seed/core/codegen/AbstractSourceCodeBuilder.java @@ -88,7 +88,6 @@ protected SourceCode buildSourceCode() { // imports addImport(GeneratedCode.class); - forEach(classMetadata.annotations, this::addImport); forEach(classMetadata.interfaceClasses, this::addImport); if (classMetadata.superClass != null) { addImport(classMetadata.superClass); @@ -131,22 +130,6 @@ protected void addImport(TypeClass typeClass) { } } - protected void addImport(AnnotationMetadata annotation) { - Assert.notNull(annotation, C.ANNOTATION); - - addImport(annotation.annotationClass); - if (annotation.hasParameters()) { - for (Object value : annotation.parameterMap.values()) { - if (value instanceof AnnotationMetadata[]) { - forEach((AnnotationMetadata[]) value, this::addImport); - } - else { - addImport(value.getClass()); - } - } - } - } - protected void addImportPackage(String packageName) { Assert.notNull(packageName, "package name"); @@ -159,7 +142,6 @@ protected void addMember(String name, TypeClass typeClass, AnnotationMetadata .. Assert.state(!memberMap.containsKey(name), "duplicate member definition for: " + name); addImport(typeClass); - forEach(annotations, this::addImport); final var member = new MemberMetadata(name, typeClass); memberMap.put(name, member); buildMember(codeBuffer, member, annotations); @@ -171,7 +153,6 @@ protected void addGetterAndSetter(String memberName, AnnotationMetadata ...annot } protected void addGetter(String memberName, AnnotationMetadata ...annotations) { - forEach(annotations, this::addImport); buildGetter(codeBuffer, getMember(memberName), annotations); } @@ -187,7 +168,6 @@ protected void addMethod(@Nullable TypeClass returnType, String methodName, // annotations forEach(annotations, annotation -> { - addImport(annotation); codeBuffer.append('\t'); buildAnnotation(codeBuffer, annotation); codeBuffer.append(LF); @@ -213,7 +193,6 @@ protected void addMethod(@Nullable TypeClass returnType, String methodName, codeBuffer.append(SEPARATOR); } if (parameter.annotation != null) { - addImport(parameter.annotation); buildAnnotation(codeBuffer, parameter.annotation); codeBuffer.append(' '); } @@ -231,7 +210,6 @@ protected void addMethod(@Nullable TypeClass returnType, String methodName, protected void addAnnotation(AnnotationMetadata annotation) { Assert.notNull(annotation, C.ANNOTATION); - addImport(annotation); codeBuffer.append('\t'); buildAnnotation(codeBuffer, annotation); codeBuffer.append(LF); @@ -321,7 +299,7 @@ private static void buildSetter(StringBuilder buf, MemberMetadata member) { } private static void buildAnnotation(StringBuilder buf, AnnotationMetadata annotation) { - buf.append('@').append(annotation.annotationClass.getSimpleName()); + buf.append('@').append(annotation.annotationClass.getName()); if (annotation.hasParameters()) { buf.append('('); boolean first = true; @@ -338,7 +316,7 @@ private static void buildAnnotation(StringBuilder buf, AnnotationMetadata annota continue; } if (entry.getValue() instanceof Enum) { - buf.append(entry.getValue().getClass().getSimpleName()).append('.'); + buf.append(entry.getValue().getClass().getName()).append('.'); } buf.append(entry.getValue()); } diff --git a/src/main/java/org/seed/core/entity/EntityValidator.java b/src/main/java/org/seed/core/entity/EntityValidator.java index cb7698477..53818772f 100644 --- a/src/main/java/org/seed/core/entity/EntityValidator.java +++ b/src/main/java/org/seed/core/entity/EntityValidator.java @@ -388,7 +388,7 @@ else if (!isNameLengthAllowed(entity.getName())) { errors.addOverlongName(getMaxNameLength()); } else if (!NameUtils.containsAlphabet(entity.getInternalName()) || - NameUtils.isIllegalEntityName(entity.getInternalName())) { + NameUtils.isIllegalEntityName(entity.getGeneratedClass())) { errors.addIllegalName(entity.getInternalName()); } else if (NameUtils.startsWithNumber(entity.getInternalName())) { diff --git a/src/main/java/org/seed/core/util/NameUtils.java b/src/main/java/org/seed/core/util/NameUtils.java index b6a4d0b0c..4cd089e0d 100644 --- a/src/main/java/org/seed/core/util/NameUtils.java +++ b/src/main/java/org/seed/core/util/NameUtils.java @@ -45,10 +45,18 @@ public abstract class NameUtils { "union", "unique", "update", "values", "view", "where" }; + private static final String[] ILLEGAL_ENTITYNAMES = { + + "abstractvalueobject", "arraylist", "bigdecimal", "bytearrayjsonserializer", + "date", "fileobject", "fileobjectjsonserializer", "generatedcode", + "hashset", "list", "referencejsonserializer", "set", "valueentity" + }; + private static final String[] ILLEGAL_FIELDNAMES = { "createdby", "createdon", "entityid", "entitystatus", "id", "lastmodified", - "modifiedby", "modifiedon", "revision_id", "revisiontype", "status_id", "uid", "version" + "modifiedby", "modifiedon", "revision_id", "revisiontype", "status_id", + "uid", "version" }; private static final String[] TRUE_VALUES = { @@ -56,8 +64,6 @@ public abstract class NameUtils { "1", "j", "ja", "on", "true", "y", "yes" }; - private static final String ILLEGAL_ENTITYNAME = "entity"; - private NameUtils() {} public static boolean isKeyword(String name) { @@ -68,9 +74,21 @@ public static boolean isSqlKeyword(String name) { return name != null && find(SQL_KEYWORDS, name); } + public static boolean isJavaLangClassName(String name) { + try { + return name != null && + Class.forName("java.lang.".concat(name)) != null; + } + catch (ClassNotFoundException cnfe) { + return false; + } + } + public static boolean isIllegalEntityName(String name) { - return ILLEGAL_ENTITYNAME.equalsIgnoreCase(name) || - (name != null && find(ILLEGAL_FIELDNAMES, name)); + return name != null && + (find(ILLEGAL_ENTITYNAMES, name) || + find(ILLEGAL_FIELDNAMES, name) || + isJavaLangClassName(name)); } public static boolean isIllegalFieldName(String name) { diff --git a/src/test/java/org/seed/test/integration/entity/CreateEntityTest.java b/src/test/java/org/seed/test/integration/entity/CreateEntityTest.java index cc17513ea..00c8dc6aa 100644 --- a/src/test/java/org/seed/test/integration/entity/CreateEntityTest.java +++ b/src/test/java/org/seed/test/integration/entity/CreateEntityTest.java @@ -42,7 +42,7 @@ void testCreateEntity() { clickButton(tabpanel, "save"); findValidationMessage(); // name is empty - findTextbox(tabpanel, "name").sendKeys("Entity"); + findTextbox(tabpanel, "name").sendKeys("String"); clickButton(tabpanel, "save"); findValidationMessage(); // name is illegal diff --git a/src/test/java/org/seed/test/unit/util/NameUtilsTest.java b/src/test/java/org/seed/test/unit/util/NameUtilsTest.java index 1108d3315..b975db83b 100644 --- a/src/test/java/org/seed/test/unit/util/NameUtilsTest.java +++ b/src/test/java/org/seed/test/unit/util/NameUtilsTest.java @@ -48,15 +48,28 @@ void testIsSqlKeyword() { assertTrue(NameUtils.isSqlKeyword("FROM")); } + @Test + void testIsJavaLangClassName() { + assertFalse(NameUtils.isJavaLangClassName(null)); + assertFalse(NameUtils.isJavaLangClassName("")); + assertFalse(NameUtils.isJavaLangClassName("Test")); + assertFalse(NameUtils.isJavaLangClassName("List")); + + assertTrue(NameUtils.isJavaLangClassName("String")); + assertTrue(NameUtils.isJavaLangClassName("Exception")); + assertTrue(NameUtils.isJavaLangClassName("WeakPairMap")); + } + @Test void testIsIllegalEntityName() { assertFalse(NameUtils.isIllegalEntityName(null)); assertFalse(NameUtils.isIllegalEntityName("")); assertFalse(NameUtils.isIllegalEntityName("test")); - assertFalse(NameUtils.isIllegalEntityName("entität")); - assertTrue(NameUtils.isIllegalEntityName("entity")); - assertTrue(NameUtils.isIllegalEntityName("Entity")); + assertTrue(NameUtils.isIllegalEntityName("String")); + assertTrue(NameUtils.isIllegalEntityName("Exception")); + assertTrue(NameUtils.isIllegalEntityName("AbstractValueObject")); + assertTrue(NameUtils.isIllegalEntityName("List")); assertTrue(NameUtils.isIllegalEntityName("id")); assertTrue(NameUtils.isIllegalEntityName("UID")); }