Reputation: 125
I am trying to modify my models and remove deprecated annotations. Explained below.
Current Class
Class User{
@Column(name="Name",percision = 3,constraint = "constraint")
private String name;
}
After modification
@Column(name="Name",percision = 3)
private String name;
}
As constraint field in deprecated in annotations I want to remove it keeping rest of the things same. I am not able to figure out how to do that in spoon.
If any one could help me please share snippets or examples if you have.
Tried editing below code but was not able to retrieve existing annotation and edit.
public class AddNullable {
public static void main(String[] args) {
Launcher spoon = new Launcher();
spoon.addInputResource("field/HasField.java");
spoon.getEnvironment().setAutoImports(true);
spoon.setSourceOutputDirectory(new File("output"));
CtModel model = spoon.buildModel();
Factory factory = spoon.getFactory();
CtAnnotationType nullableAnnotation = (CtAnnotationType) factory.Type().get(Nullable.class);
CtType hasField = model.getRootPackage().getType("HasField");
List<CtField> fields = hasField.getFields();
System.out.println(fields.size());
for (CtField field : fields) {
if (!field.hasAnnotation(Nullable.class) && !field.getType().isPrimitive()) {
System.out.println(field.getSimpleName() + " does not has annotation.");
CtAnnotation an = factory.createAnnotation(nullableAnnotation.getReference());
field.addAnnotation(an);
}
}
spoon.prettyprint();
}
}
Upvotes: 1
Views: 153