Skip to content

Commit

Permalink
fix: 🚑 @SuppressWarning annotation copied without value - compile err…
Browse files Browse the repository at this point in the history
…or (#126)

fix: Copy annotations to components with values
  • Loading branch information
create-issue-branch[bot] authored Oct 10, 2023
1 parent 8752e48 commit 0a01528
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create v${{ env.RELEASE_VERSION }} modules/auto-record/target/auto-record-${{ env.RELEASE_VERSION }}.jar --generate-notes --verify-tag
gh release create v${{ env.RELEASE_VERSION }} --generate-notes --verify-tag
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Java record newContext generator

[![CI Verify Status](https://github.com/pawellabaj/auto-record/actions/workflows/verify.yml/badge.svg?branch=main)](https://github.com/pawellabaj/auto-record/actions/workflows/verify.yml)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=pl.com.labaj.autorecord%3Aauto-record-project&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=pl.com.labaj.autorecord%3Aauto-record-project)
[![Sonatype Lift Status](https://lift.sonatype.com/api/badge/github.com/pawellabaj/auto-record)](https://lift.sonatype.com/results/github.com/pawellabaj/auto-record)
[![Reproducible Builds](https://img.shields.io/badge/Reproducible_Builds-ok-success?labelColor=1e5b96)](https://github.com/jvm-repo-rebuild/reproducible-central#pl.com.labaj.autorecord:auto-record)
[![OpenSSF Best Practices](https://bestpractices.coreinfrastructure.org/projects/7700/badge)](https://bestpractices.coreinfrastructure.org/projects/7700)
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](.github/CODE_OF_CONDUCT.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@

@Target({METHOD, PARAMETER, RECORD_COMPONENT})
@Retention(CLASS)
public @interface ClassProperty {}
public @interface ClassProperty {
int priority() default 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@

@Target({METHOD, PARAMETER, RECORD_COMPONENT})
@Retention(SOURCE)
public @interface SourceProperty {}
public @interface SourceProperty {
String metadata() default "default";
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class GenerationTest {
"ToStringMemoized",
"ToStringMemoizedByAnnotation",
"HashCodeIgnoredFieldMemoized",
"AnnotatedProperty"
"AnnotatedProperty",
"AnnotationWithValue"
})
void shouldGenerateRecord(String interfaceName) {
//given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pl.com.labaj.autorecord.testcase;

/*-
* 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 pl.com.labaj.autorecord.AutoRecord;
import pl.com.labaj.autorecord.Memoized;
import pl.com.labaj.autorecord.annotations.ClassProperty;
import pl.com.labaj.autorecord.annotations.SourceProperty;

import javax.annotation.Nullable;

import static java.util.Objects.isNull;

@AutoRecord
interface AnnotationWithValue {
@Nullable
Integer id();

@SourceProperty
@ClassProperty(priority = 9)
@SuppressWarnings("DataFlowIssue")
@Memoized
default String idToString() {
if (isNull(id())) {
return "absent";
}

return Integer.toString(id());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package pl.com.labaj.autorecord.testcase;

/*-
* 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 static java.util.Objects.requireNonNullElseGet;

import java.lang.Integer;
import java.lang.Override;
import java.lang.String;
import java.lang.SuppressWarnings;
import javax.annotation.Nullable;
import javax.annotation.processing.Generated;
import pl.com.labaj.autorecord.GeneratedWithAutoRecord;
import pl.com.labaj.autorecord.Memoized;
import pl.com.labaj.autorecord.annotations.ClassProperty;
import pl.com.labaj.autorecord.annotations.SourceProperty;
import pl.com.labaj.autorecord.memoizer.Memoizer;

@Generated("pl.com.labaj.autorecord.AutoRecord")
@GeneratedWithAutoRecord
record AnnotationWithValueRecord(@Nullable Integer id,
@SourceProperty @ClassProperty(priority = 9) @Nullable Memoizer<String> idToStringMemoizer) implements AnnotationWithValue {
AnnotationWithValueRecord {
idToStringMemoizer = requireNonNullElseGet(idToStringMemoizer, Memoizer::new);
}

AnnotationWithValueRecord(@Nullable Integer id) {
this(id, new Memoizer<>());
}

@SourceProperty
@ClassProperty(priority = 9)
@SuppressWarnings({"DataFlowIssue"})
@Memoized
@Override
public String idToString() {
return idToStringMemoizer.computeIfAbsent(AnnotationWithValue.super::idToString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private void addMember(StaticImports staticImports, AnnotationSpec.Builder annot

if (returnType.isPrimitive()) {
annotationBuilder.addMember(name, "$L", actualValue);
} else if (option.type().isEnum()) {
} else if (returnType.isEnum()) {
var enumName = ((Enum<?>) actualValue).name();
staticImports.add(returnType, enumName);
annotationBuilder.addMember(name, "$L", enumName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import javax.annotation.Nullable;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
Expand All @@ -36,14 +37,15 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
import static org.apiguardian.api.API.Status.INTERNAL;

Expand Down Expand Up @@ -74,23 +76,21 @@ public static List<AnnotationSpec> createAnnotationSpecs(List<AnnotationMirror>
Set<ElementType> targets,
List<Class<? extends Annotation>> annotationsToAdd,
List<Class<? extends Annotation>> annotationsToExclude) {
var classNamesStream = annotationMirrors.stream()
.map(AnnotationMirror::getAnnotationType)
.map(DeclaredType::asElement)
.map(TypeElement.class::cast)
.filter(annotation -> canAnnotateElementType(annotation, targets))
.map(ClassName::get);
var classNamesToAdd = annotationsToAdd.stream()
.map(ClassName::get);
var parentAnnotationDetails = annotationMirrors.stream()
.map(annotationMirror -> AnnotationDetails.toAnnotationDetails(annotationMirror, targets))
.filter(Objects::nonNull);
var annotationDetailsToAdd = annotationsToAdd.stream()
.map(ClassName::get)
.map(className -> new AnnotationDetails(className, Map.of()));

var classNamesToExclude = annotationsToExclude.stream()
.map(ClassName::get)
.collect(toSet());

return Stream.concat(classNamesStream, classNamesToAdd)
.filter(not(classNamesToExclude::contains))
return Stream.concat(parentAnnotationDetails, annotationDetailsToAdd)
.filter(annotationDetails -> !classNamesToExclude.contains(annotationDetails.className()))
.distinct()
.map(AnnotationSpec::builder)
.map(AnnotationSpec.Builder::build)
.map(AnnotationDetails::toAnnotationSpec)
.toList();
}

Expand Down Expand Up @@ -153,7 +153,7 @@ public static List<AnnotationSpec> merge(List<AnnotationSpec> annotations1, List

var annotationBuilder = AnnotationSpec.builder(annotationlass);

record Member(String name, CodeBlock value){}
record Member(String name, CodeBlock value) {}
var members = entry.getValue().stream()
.map(annotationSpec -> annotationSpec.members)
.map(Map::entrySet)
Expand All @@ -172,4 +172,33 @@ record Member(String name, CodeBlock value){}
})
.toList();
}

private record AnnotationDetails(ClassName className, Map<String, AnnotationValue> values) {
@Nullable
private static AnnotationDetails toAnnotationDetails(AnnotationMirror annotationMirror, Set<ElementType> targets) {
var annotationType = annotationMirror.getAnnotationType();
var typeElement = (TypeElement) annotationType.asElement();

if (!canAnnotateElementType(typeElement, targets)) {
return null;
}

var className = ClassName.get(typeElement);

var values = annotationMirror.getElementValues().entrySet().stream()
.collect(toMap(
entry -> entry.getKey().getSimpleName().toString(),
entry -> (AnnotationValue) entry.getValue()
));

return new AnnotationDetails(className, values);
}

private AnnotationSpec toAnnotationSpec() {
var annotationBuilder = AnnotationSpec.builder(className);
values.forEach((name, value) -> annotationBuilder.addMember(name, "$L", value.toString()));

return annotationBuilder.build();
}
}
}

0 comments on commit 0a01528

Please sign in to comment.