-
-
Notifications
You must be signed in to change notification settings - Fork 352
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
EnumFactory.getEnum(Class<?>) returns null #563
Comments
It's probably because DayOfWeek.java is not in the spoon source folder. The feature of reflecting the structural part of runtime classes as Spoon objects is very interesting IMHO |
Thank you for the answer @monperrus. From what I understand, it means that I can't get an enum value for an arbitrary enum using spoon? Or maybe there is an alternative way? |
Hi @simonedavico,
|
Hi @GerardPaligot,
public class SpoonTest {
public static void main(String[] args) {
Path outputDirectory = Paths.get("./src/main/resources");
Launcher spoonLauncher = new spoon.Launcher();
spoonLauncher.setSourceOutputDirectory(outputDirectory.toFile());
spoonLauncher.addProcessor(new ClassProcessor());
spoonLauncher.addInputResource("./testSpoon/Test.java");
System.out.println("Before run");
spoonLauncher.run();
}
} As you can see, I am only adding as source the class I want to modify (
To give more context, I am having trouble setting the value through
|
Ok, I understand your issue. Spoon doesn't build java classes in models. If you want to react with java classes, you must create For example, if you want all classes annotated with an annotation named public class ClassProcessor extends AbstractProcessor<CtClass> {
@Override
public boolean isToBeProcessed(CtClass candidate) {
final CtAnnotation<DefaultDay> annotation = candidate.getAnnotation(getFactory().Type().createReference(DefaultDay.class));
final CtExpression day = annotation.getValue("day");
return day instanceof CtFieldRead;
}
public void process(CtClass element) {
// Make what do you want here.
}
} |
@GerardPaligot thank you for your response (that I completely missed, sorry about the late reply). What I am looking for is not really checking whether my class has an annotation So, for example, I would like to go from public class Test {
//body of the class
} to @DefaultDay(day = DayOfWeek.MONDAY)
public class Test {
//body of the class
} Is there a way I can achieve this with Spoon? The closes I could achieve, through this code int dayOfWeek = DayOfWeek.MONDAY.getValue();
CtCodeSnippetExpression<Object> castDaySnippet = getFactory().Code().createCodeSnippetExpression("DayOfWeek.of(" + dayOfWeek + ")");
getFactory().Annotation().annotate(element, DefaultDay.class);
element.getAnnotation(getFactory().Type().createReference(DefaultDay.class)).addValue("day", castDaySnippet); generates an annotation like this: @complete.path.to.DefaultDay(day = java.time.DayOfWeek.of(1)) Is there a way to add the value without having to generate the expression snippet? |
Hi, In order to create a what you need, you have to do something like this: CtFieldAccess<?> fieldRead = factory.Core().createFieldRead();
CtTypeReference<DayOfWeek> enumReference = factory.Type().createReference(DayOfWeek.class);
CtFieldReference fieldReference = factory.Field().createReference(enumReference, enumReference, DayOfWeek.MONDAY.name());
fieldReference.setStatic(true);
fieldRead.setVariable(fieldReference);
CtAnnotation<?> ctAnnotation = element.getAnnotation(getFactory().Type().createReference(DefaultDay.class));
ctAnnotation.addValue("day", fieldRead); |
@tdurieux thank you, it works as expected! |
Fixed by #569 |
Simple test case I was trying while playing around with
EnumFactory
:This processor will throw a
NullPointerException
, since for some reasondayOfWeekEnum
will be null.Am I doing something wrong, or is this a bug in spoon?
I am using
5.1.0
from Maven.The text was updated successfully, but these errors were encountered: