Skip to content

Commit

Permalink
Add warning message about non-public constructor for @ConfigProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand authored and gsmet committed May 26, 2020
1 parent cb3edcf commit 0cf7184
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.Modifier;
import java.util.List;

import org.jboss.logging.Logger;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
Expand All @@ -15,6 +16,8 @@

public class ConfigPropertyBuildItemCandidateUtil {

private static final Logger LOGGER = Logger.getLogger(ClassConfigPropertiesUtil.class);

/**
* This method inspects the {@code configClass} bytecode to identify all fields that have a default value set in the class
* constructor. These fields are removed from the {@link ConfigPropertyBuildItemCandidate} list because we don't want to
Expand All @@ -25,25 +28,30 @@ public static void removePropertiesWithDefaultValue(ClassLoader classLoader, Str
List<ConfigPropertyBuildItemCandidate> candidates) {
try (InputStream is = classLoader.getResourceAsStream(configClass.replace('.', '/') + ".class")) {
ClassReader configClassReader = new ClassReader(is);
configClassReader.accept(new ConfigClassVisitor(candidates), 0);
configClassReader.accept(new ConfigClassVisitor(candidates, configClass), 0);
} catch (IOException e) {
throw new UncheckedIOException(configClass + " class reading failed", e);
}
}

private static class ConfigClassVisitor extends ClassVisitor {

private List<ConfigPropertyBuildItemCandidate> candidates;
private final List<ConfigPropertyBuildItemCandidate> candidates;
private final String configClass;

private ConfigClassVisitor(List<ConfigPropertyBuildItemCandidate> candidates) {
private ConfigClassVisitor(List<ConfigPropertyBuildItemCandidate> candidates, String configClass) {
super(Gizmo.ASM_API_VERSION);
this.candidates = candidates;
this.configClass = configClass;
}

@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor superMethodVisitor = super.visitMethod(access, name, descriptor, signature, exceptions);
if (access == Modifier.PUBLIC && name.equals("<init>") && descriptor.equals("()V")) {
if (access != Modifier.PUBLIC) {
LOGGER.warn("Class '" + configClass
+ "' which is meant to hold configuration properties does not have a public constructor and therefore may not function correctly");
} else if (name.equals("<init>") && descriptor.equals("()V")) {
return new ConfigClassConstructorVisitor(superMethodVisitor, candidates);
}
return superMethodVisitor;
Expand Down

0 comments on commit 0cf7184

Please sign in to comment.