-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Custom extensions for updating compact constructor can be add…
…ed (#102) * feat: Allowed to implement an extension for compact constructor updating
- Loading branch information
1 parent
340d0c6
commit 9e4c36f
Showing
200 changed files
with
4,203 additions
and
1,035 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
titleAndCommits: true | ||
anyCommit: true | ||
anyCommit: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,4 @@ | |
./*.txt | ||
|
||
/.idea/ | ||
/target/ | ||
**/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>pl.com.labaj</groupId> | ||
<artifactId>auto-record-project</artifactId> | ||
<version>2.0.1-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>auto-record-tests</artifactId> | ||
|
||
<properties> | ||
<maven.install.skip>true</maven.install.skip> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>pl.com.labaj</groupId> | ||
<artifactId>auto-record</artifactId> | ||
<version>${project.parent.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.testing.compile</groupId> | ||
<artifactId>compile-testing</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<profiles> | ||
<profile> | ||
<id>verify</id> | ||
|
||
<properties> | ||
<sonar.coverage.exclusions>src/main/java/**/*</sonar.coverage.exclusions> | ||
</properties> | ||
</profile> | ||
<profile> | ||
<id>release</id> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.sonatype.plugins</groupId> | ||
<artifactId>nexus-staging-maven-plugin</artifactId> | ||
<extensions>true</extensions> | ||
<executions> | ||
<execution> | ||
<id>default-deploy</id> | ||
<phase>none</phase> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
</project> |
48 changes: 48 additions & 0 deletions
48
...ava/pl/com/labaj/autorecord/test/extension/compact/AlwaysCompactConstructorExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package pl.com.labaj.autorecord.test.extension.compact; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import com.squareup.javapoet.CodeBlock; | ||
import pl.com.labaj.autorecord.context.Context; | ||
import pl.com.labaj.autorecord.context.StaticImports; | ||
import pl.com.labaj.autorecord.extension.CompactConstructorExtension; | ||
|
||
import java.util.Arrays; | ||
|
||
public class AlwaysCompactConstructorExtension implements CompactConstructorExtension { | ||
private String[] parameters; | ||
|
||
@Override | ||
public void setParameters(String[] parameters) { | ||
this.parameters = parameters; | ||
} | ||
|
||
@Override | ||
public boolean shouldGenerate(boolean isGeneratedByProcessor, Context context) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public CodeBlock suffixCompactConstructorContent(Context context, StaticImports staticImports) { | ||
staticImports.add(System.class, "out"); | ||
|
||
return CodeBlock.builder() | ||
.addStatement("var params = $S", Arrays.toString(parameters)) | ||
.addStatement("out.println(params)") | ||
.build(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...n/java/pl/com/labaj/autorecord/test/extension/compact/IsPersonAdultVerifierExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package pl.com.labaj.autorecord.test.extension.compact; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import com.squareup.javapoet.CodeBlock; | ||
import pl.com.labaj.autorecord.context.Context; | ||
import pl.com.labaj.autorecord.context.StaticImports; | ||
import pl.com.labaj.autorecord.extension.CompactConstructorExtension; | ||
import pl.com.labaj.autorecord.processor.AutoRecordProcessorException; | ||
|
||
public class IsPersonAdultVerifierExtension implements CompactConstructorExtension { | ||
|
||
private static final Class<RuntimeException> RUNTIME_EXCEPTION_CLASS = RuntimeException.class; | ||
private Class<? extends RuntimeException> exceptionClass; | ||
|
||
@SuppressWarnings("java:S112") | ||
@Override | ||
public void setParameters(String[] parameters) { | ||
if (parameters.length < 1) { | ||
exceptionClass = RUNTIME_EXCEPTION_CLASS; | ||
} else { | ||
var className = parameters[0]; | ||
|
||
try { | ||
var aClass = Class.forName(className); | ||
exceptionClass = getExceptionClass(aClass); | ||
} catch (ClassNotFoundException e) { | ||
throw new RuntimeException("Cannot load " + className + " class", e); | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Class<? extends RuntimeException> getExceptionClass(Class<?> aClass) { | ||
if (RUNTIME_EXCEPTION_CLASS.isAssignableFrom(aClass)) { | ||
return (Class<? extends RuntimeException>) aClass; | ||
} else { | ||
throw new AutoRecordProcessorException("Class " + aClass.getName() + " does not extend " + RUNTIME_EXCEPTION_CLASS.getName()); | ||
} | ||
} | ||
|
||
@Override | ||
public CodeBlock suffixCompactConstructorContent(Context context, StaticImports staticImports) { | ||
return CodeBlock.builder() | ||
.beginControlFlow("if (age < 18)") | ||
.addStatement("var message = \"%s is not adult. She/he is only %d!\".formatted(name, age)") | ||
.addStatement("throw new $T(message)", exceptionClass) | ||
.endControlFlow() | ||
.build(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...-tests/src/main/java/pl/com/labaj/autorecord/test/extension/compact/LoggingExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package pl.com.labaj.autorecord.test.extension.compact; | ||
|
||
/*- | ||
* Copyright © 2023 Auto Record | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import com.squareup.javapoet.CodeBlock; | ||
import pl.com.labaj.autorecord.context.Context; | ||
import pl.com.labaj.autorecord.context.RecordComponent; | ||
import pl.com.labaj.autorecord.context.StaticImports; | ||
import pl.com.labaj.autorecord.extension.CompactConstructorExtension; | ||
|
||
import java.util.HashMap; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import static java.util.logging.Level.FINE; | ||
|
||
public class LoggingExtension implements CompactConstructorExtension { | ||
|
||
private Level level; | ||
|
||
@Override | ||
public void setParameters(String[] parameters) { | ||
if (parameters.length < 1) { | ||
level = FINE; | ||
} else { | ||
level = Level.parse(parameters[0].toUpperCase()); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean shouldGenerate(boolean isGeneratedByProcessor, Context context) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public CodeBlock prefixCompactConstructorContent(Context context, StaticImports staticImports) { | ||
var codeBuilder = CodeBlock.builder(); | ||
|
||
codeBuilder.addStatement("var map = new $T<$T, $T>()", HashMap.class, String.class, Object.class); | ||
|
||
context.components().stream() | ||
.map(RecordComponent::name) | ||
.forEach(name -> codeBuilder.addStatement("map.put($S, $L)", name, name)); | ||
|
||
codeBuilder.addStatement("var logger = getLogger($L.class.getName())", context.recordName()) | ||
.addStatement("logger.log($L, $S, $L)", level, "Parameters passed to record: {0}", "map"); | ||
|
||
staticImports.add(Logger.class, "getLogger") | ||
.add(Level.class, level.getName()); | ||
|
||
return codeBuilder.build(); | ||
} | ||
} |
Oops, something went wrong.