Skip to content

Commit

Permalink
Fixed default code handling for compile message enum generator
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasstamann committed Apr 12, 2024
1 parent 3b2bb34 commit 03a5b13
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,14 @@ boolean verify(List<DeclareCompilerMessageWrapper> 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);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<VariableElement> 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<ExecutableElement> results = this.getEnclosingRecordTypeElement().filterEnclosedElements()
.applyFilter(AptkCoreMatchers.IS_METHOD)
Expand Down

0 comments on commit 03a5b13

Please sign in to comment.