How to configure UnitSourceGenerator not to add imports for "java.lang" classes #43
-
|
Good morning, This works really well except if the source clazz has member fields belonging to "java.lang" package. Normally usage of these types would not result in an import directive, since "java.lang" is always available. Does anyone know how to switch this off? Kind regards |
Beta Was this translation helpful? Give feedback.
Answered by
Roberto-Gentili
Nov 21, 2025
Replies: 1 comment 1 reply
-
|
Try this: static void createPojo(Class<?> clazz) {
UnitSourceGenerator sourceGenerator = UnitSourceGenerator.create("my.cool.package");
String clazzSimpleName = clazz.getSimpleName();
ClassSourceGenerator classSourceGenerator = ClassSourceGenerator.create(TypeDeclarationSourceGenerator
.create(clazzSimpleName));
Arrays.stream(clazz.getDeclaredFields())
.forEach(field -> {
if (field.getType().getName().startsWith("java.lang")) {
classSourceGenerator
.addField(VariableSourceGenerator.create(field.getType().getSimpleName() + " " + field.getName())
.addModifier(Modifier.PRIVATE));
} else {
classSourceGenerator
.addField(VariableSourceGenerator.create(field.getType(), field.getName())
.addModifier(Modifier.PRIVATE));
}
});
sourceGenerator.addClass(classSourceGenerator);
System.out.println(sourceGenerator.make());
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
fjakop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this: