Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for additional modifiers #9201

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,18 @@ private static Set<Modifier> toModifiers(ClassMemberInfo memberInfo) {
if (mi.isAbstract()) {
result.add(Modifier.ABSTRACT);
}
if (mi.isSynchronized()) {
result.add(Modifier.SYNCHRONIZED);
}
if (mi.isNative()) {
result.add(Modifier.NATIVE);
}
}

if (memberInfo instanceof FieldInfo fi) {
if (fi.isTransient()) {
result.add(Modifier.TRANSIENT);
}
tomas-langer marked this conversation as resolved.
Show resolved Hide resolved
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@
import io.helidon.common.Weight;

@Weight(48)
public class TriggerType {
public final class TriggerType {
private transient volatile String field = "value";

public synchronized final String getField() {
return field;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.annotation.ElementType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
Expand All @@ -33,6 +34,7 @@

import static io.helidon.common.testing.junit5.OptionalMatcher.optionalPresent;
import static io.helidon.common.testing.junit5.OptionalMatcher.optionalValue;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
Expand All @@ -44,6 +46,7 @@
public class CodegenValidationTest {
private static final String CLASS_NAME = "io.helidon.codegen.test.codegen.use.TriggerType__Generated";
private static Class<?> clazz;
private static Object instance;

@BeforeAll
public static void setUpClass() {
Expand All @@ -52,6 +55,37 @@ public static void setUpClass() {
} catch (ClassNotFoundException e) {
fail("Class " + CLASS_NAME + " should have been code generated by TestCodegenExtension");
}
try {
instance = clazz.getConstructor().newInstance();
} catch (ReflectiveOperationException e) {
fail("Class " + CLASS_NAME + " should have an accessible constructor", e);
}
}

@Test
void testClassModifiers() throws ReflectiveOperationException {
String modifiers = (String) clazz.getMethod("classModifiers")
.invoke(instance);

assertThat(modifiers, containsString("final"));
}

@Test
void testMethodModifiers() throws ReflectiveOperationException {
String modifiers = (String) clazz.getMethod("methodModifiers")
.invoke(instance);

assertThat(modifiers, containsString("final"));
assertThat(modifiers, containsString("synchronized"));
}

@Test
void testFieldModifiers() throws ReflectiveOperationException {
String modifiers = (String) clazz.getMethod("fieldModifiers")
.invoke(instance);

assertThat(modifiers, containsString("transient"));
assertThat(modifiers, containsString("volatile"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import io.helidon.codegen.CodegenContext;
import io.helidon.codegen.RoundContext;
import io.helidon.codegen.classmodel.ClassModel;
import io.helidon.codegen.spi.CodegenExtension;
import io.helidon.common.types.AccessModifier;
import io.helidon.common.types.Annotation;
import io.helidon.common.types.ElementKind;
import io.helidon.common.types.Modifier;
import io.helidon.common.types.TypeInfo;
import io.helidon.common.types.TypeName;
import io.helidon.common.types.TypeNames;

class TestCodegenExtension implements CodegenExtension {
private static final TypeName CRAZY = TypeName.create("io.helidon.codegen.test.codegen.use.CrazyAnnotation");
Expand Down Expand Up @@ -72,7 +76,42 @@ private void process(TypeInfo typeInfo) {
.addConstructor(ctr -> ctr.addAnnotation(annotation()))
.addMethod(method -> method
.name("method")
.addAnnotation(annotation()));
.addAnnotation(annotation()))
.addMethod(classMods -> classMods
.name("classModifiers")
.returnType(TypeNames.STRING)
.addContent("return \"")
.addContent(typeInfo.elementModifiers()
.stream()
.map(Modifier::modifierName)
.collect(Collectors.joining(",")))
.addContentLine("\";"));

typeInfo.elementInfo()
.forEach(it -> {
if (it.kind() == ElementKind.METHOD) {
classModel.addMethod(methodMods -> methodMods
.name("methodModifiers")
.returnType(TypeNames.STRING)
.addContent("return \"")
.addContent(it.elementModifiers()
.stream()
.map(Modifier::modifierName)
.collect(Collectors.joining(",")))
.addContentLine("\";"));
}
if (it.kind() == ElementKind.FIELD) {
classModel.addMethod(fieldMods -> fieldMods
.name("fieldModifiers")
.returnType(TypeNames.STRING)
.addContent("return \"")
.addContent(it.elementModifiers()
.stream()
.map(Modifier::modifierName)
.collect(Collectors.joining(",")))
.addContentLine("\";"));
}
});

ctx.filer().writeSourceFile(classModel.build());
}
Expand Down
20 changes: 18 additions & 2 deletions common/types/src/main/java/io/helidon/common/types/Modifier.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,7 +39,23 @@ public enum Modifier {
/**
* The {@code final} modifier.
*/
FINAL("final");
FINAL("final"),
/**
* The {@code transient} modifier.
*/
TRANSIENT("transient"),
/**
* The {@code volatile} modifier.
*/
VOLATILE("volatile"),
/**
* The {@code synchronized} modifier.
*/
SYNCHRONIZED("synchronized"),
/**
* The {@code native} modifier.
*/
NATIVE("native");

private final String modifierName;

Expand Down
Loading