Skip to content

Commit

Permalink
entity validation fix 2
Browse files Browse the repository at this point in the history
  • Loading branch information
seed-master committed Oct 11, 2023
1 parent 48c3f60 commit 9f3a858
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
26 changes: 2 additions & 24 deletions src/main/java/org/seed/core/codegen/AbstractSourceCodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");

Expand All @@ -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);
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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(' ');
}
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/seed/core/entity/EntityValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/org/seed/core/util/NameUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,25 @@ 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 = {

"1", "j", "ja", "on", "true", "y", "yes"
};

private static final String ILLEGAL_ENTITYNAME = "entity";

private NameUtils() {}

public static boolean isKeyword(String name) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 16 additions & 3 deletions src/test/java/org/seed/test/unit/util/NameUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down

0 comments on commit 9f3a858

Please sign in to comment.