how do I use M3 to extract annotation from a Java (Annotated) code for the purpose of code generation (for example generating an Entity-Relationship Model) from the information in the annotation? Like the one below:
@Entity
public class Customers {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "CustomerID", nullable = false)
private Integer customerId;
@Basic
@Column(name = "CustomerName", nullable = true, length = 50)
private String customerName;
@Basic
@Column(name = "ContactName", nullable = true, length = 50)
private String contactName;
@Basic
@Column(name = "Address", nullable = true, length = 50)
private String address;
@Basic
@Column(name = "City", nullable = true, length = 20)
private String city;
@Basic
@Column(name = "PostalCode", nullable = true, length = 10)
private String postalCode;
@Basic
@Column(name = "Country", nullable = true, length = 15)
private String country;
@OneToMany(mappedBy = "customersByCustomerId")
private Collection<Orders> ordersByCustomerId;
.....
}
I am familiar with the functions createM3FromFile
and createAstFromFile
. Any resource(s) to aid understand will also be appreciated
visit
and pattern matching.However, this could become quite an expensive operation for something so simple. Instead, it is often handy to use the M3 models as a fast index. Here https://www.rascal-mpl.org/docs/Library/lang/java/m3/Core/, you see that annotations
defines a relation between specific declarations and the annotations that are defined on them.
With this information, you can filter which classes have which interesting annotations, and then apply the AST pattern matching techniques if you need more information, such as concrete parameters to the actual annotations.
This example has code to explain how to analyze java ASTs: https://www.rascal-mpl.org/docs/Recipes/Metrics/MeasuringJava/MeasuringMethods/