diff --git a/compilermessages/integrationtest/src/main/java/io/toolisticon/aptk/compilermessage/test/OnClassAndNestedClass.java b/compilermessages/integrationtest/src/main/java/io/toolisticon/aptk/compilermessage/test/OnClassAndNestedClass.java index 733ab240..3e62ef46 100644 --- a/compilermessages/integrationtest/src/main/java/io/toolisticon/aptk/compilermessage/test/OnClassAndNestedClass.java +++ b/compilermessages/integrationtest/src/main/java/io/toolisticon/aptk/compilermessage/test/OnClassAndNestedClass.java @@ -13,6 +13,8 @@ public void anotherMethod() { } @DeclareCompilerMessage(enumValueName = "ON_NESTED_CLASS", code = "004",message = "ON NESTED CLASS Test ${0}") + @DeclareCompilerMessage(enumValueName = "FOR_EMPTY_CODE_TEST1", message = "FOR_EMPTY_CODE_TEST") + @DeclareCompilerMessage(enumValueName = "FOR_EMPTY_CODE_TEST2", message = "FOR_EMPTY_CODE_TEST") static class nestedClass { diff --git a/compilermessages/processor/src/main/java/io/toolisticon/aptk/compilermessage/processor/CompilerMessageProcessor.java b/compilermessages/processor/src/main/java/io/toolisticon/aptk/compilermessage/processor/CompilerMessageProcessor.java index 02d4144d..ddfe3791 100644 --- a/compilermessages/processor/src/main/java/io/toolisticon/aptk/compilermessage/processor/CompilerMessageProcessor.java +++ b/compilermessages/processor/src/main/java/io/toolisticon/aptk/compilermessage/processor/CompilerMessageProcessor.java @@ -127,12 +127,14 @@ boolean verify(List enumValues) { enumValue.enumValueNameAsAttributeWrapper().compilerMessage().asError().write(CompilerMessageProcessorMessages.ERROR_CODE_ENUM_VALUE_NAME_MUST_VALID, enumValue.enumValueName()); } + // Check if code is unique - if (codeSet.contains(enumValue.code())) { - enumValue.codeAsAttributeWrapper().compilerMessage().asError().write(CompilerMessageProcessorMessages.ERROR_CODE_MUST_BE_UNIQUE, enumValue.code()); + String codeToCheck = CompilerMessageWrapperCustomCode.getCalculatedCode(enumValue); + if (codeSet.contains(codeToCheck)) { + enumValue.codeAsAttributeWrapper().compilerMessage().asError().write(CompilerMessageProcessorMessages.ERROR_CODE_MUST_BE_UNIQUE, codeToCheck); retValue = false; } else { - codeSet.add(enumValue.code()); + codeSet.add(codeToCheck); } } diff --git a/compilermessages/processor/src/main/java/io/toolisticon/aptk/compilermessage/processor/CompilerMessageWrapperCustomCode.java b/compilermessages/processor/src/main/java/io/toolisticon/aptk/compilermessage/processor/CompilerMessageWrapperCustomCode.java index 9dd86331..1801c74d 100644 --- a/compilermessages/processor/src/main/java/io/toolisticon/aptk/compilermessage/processor/CompilerMessageWrapperCustomCode.java +++ b/compilermessages/processor/src/main/java/io/toolisticon/aptk/compilermessage/processor/CompilerMessageWrapperCustomCode.java @@ -58,7 +58,7 @@ public static CompilerMessageProcessor.TargetCompilerMessageEnum getTarget(Decla @CustomCodeMethod(DeclareCompilerMessage.class) public static String getCalculatedCode(DeclareCompilerMessageWrapper compilerMessageWrapper){ - return compilerMessageWrapper.codeIsDefaultValue() ? compilerMessageWrapper.enumValueName() : compilerMessageWrapper.code(); + return (compilerMessageWrapper.codeIsDefaultValue() || compilerMessageWrapper.code().equals("") ) ? compilerMessageWrapper.enumValueName() : compilerMessageWrapper.code(); } } diff --git a/integrationtest/java16/src/test/java/io/toolisticon/aptk/integrationtest/java16/Java16Tests.java b/integrationtest/java16/src/test/java/io/toolisticon/aptk/integrationtest/java16/Java16Tests.java index 00541c49..a50d9287 100644 --- a/integrationtest/java16/src/test/java/io/toolisticon/aptk/integrationtest/java16/Java16Tests.java +++ b/integrationtest/java16/src/test/java/io/toolisticon/aptk/integrationtest/java16/Java16Tests.java @@ -122,6 +122,10 @@ public void test_recordComponent_accessWrapperMethods() { MatcherAssert.assertThat(elementWrapper.getEnclosingRecordTypeElement().getQualifiedName(), Matchers.is(MyRecord.class.getCanonicalName())); MatcherAssert.assertThat(TypeElementWrapper.toTypeElement(elementWrapper.getEnclosingElement().get()).getQualifiedName(), Matchers.is(MyRecord.class.getCanonicalName())); + // Additional method + MatcherAssert.assertThat(elementWrapper.getField(), Matchers.notNullValue()); + MatcherAssert.assertThat(elementWrapper.getField().getSimpleName(), Matchers.is("name")); + } finally { ToolingProvider.clearTooling(); diff --git a/integrationtest/java17/src/test/java/io/toolisticon/aptk/integrationtest/java17/Java17Tests.java b/integrationtest/java17/src/test/java/io/toolisticon/aptk/integrationtest/java17/Java17Tests.java index 28678e93..ef20f63a 100644 --- a/integrationtest/java17/src/test/java/io/toolisticon/aptk/integrationtest/java17/Java17Tests.java +++ b/integrationtest/java17/src/test/java/io/toolisticon/aptk/integrationtest/java17/Java17Tests.java @@ -55,6 +55,7 @@ public void test_sealedClassesFeature_sealed() { MatcherAssert.assertThat(elementWrapper.getPermittedSubclasses().stream().map(e -> e.getQualifiedName()).collect(Collectors.toSet()), Matchers.contains(AllowedClass.class.getCanonicalName())); MatcherAssert.assertThat(element.getPermittedSubclasses().stream().map(e -> e.toString()).collect(Collectors.toSet()), Matchers.contains(AllowedClass.class.getCanonicalName())); + } finally { ToolingProvider.clearTooling(); } diff --git a/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/RecordComponentElementWrapper.java b/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/RecordComponentElementWrapper.java index 6643952a..48c657f0 100644 --- a/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/RecordComponentElementWrapper.java +++ b/tools/src/main/java/io/toolisticon/aptk/tools/wrapper/RecordComponentElementWrapper.java @@ -5,6 +5,7 @@ import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; import java.util.List; /** @@ -41,6 +42,19 @@ public ExecutableElementWrapper getAccessor() { return executableElement != null ? ExecutableElementWrapper.wrap(executableElement) : determineAccessorWorkaround(); } + /** + * Gets the related field for the record compoent. + * @return the wrapped of the VariableElement of the related field + */ + public VariableElementWrapper getField() { + List results = this.getEnclosingRecordTypeElement().filterEnclosedElements() + .applyFilter(AptkCoreMatchers.IS_FIELD) + .applyFilter(AptkCoreMatchers.BY_NAME).filterByOneOf(getSimpleName()) + .getResult(); + + return results.isEmpty() ? null : VariableElementWrapper.wrap(results.get(0)); + } + private ExecutableElementWrapper determineAccessorWorkaround(){ List results = this.getEnclosingRecordTypeElement().filterEnclosedElements() .applyFilter(AptkCoreMatchers.IS_METHOD)