Given a java file, I would like to annotate it for persistence with DataNucleus using ANTLR4, preserving white spaces and comments.
I have tried to follow ANTLR 4 reference to modify this grammar https://github.com/antlr/grammars-v4/blob/master/java/Java.g4 but have not been able to make it work.
Example: Given
package parsear;
import math;
import java.io.*;
public class Persona {
private String nombre;
private int edad;
private String ciudad;
private int telefono;
public Persona(){}
/**
** Preserve all comments and indentation
*/
public Persona(String nombre, int edad, String ciudad, int telefono) {
super();
this.nombre = nombre;
this.edad = edad;
this.ciudad = ciudad;
this.telefono = telefono;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
//Even one line comments
public int getEdad() {
return edad;
}
public void setEdad(int edad) {
this.edad = edad;
}
public String getCiudad() {
return ciudad;
}
public void setCiudad(String ciudad) {
this.ciudad = ciudad;
}
public int getTelefono() {
return telefono;
}
public void setTelefono(int telefono) {
this.telefono = telefono;
}
@Override
public String toString() {
return "Persona [nombre=" + nombre + ", edad=" + edad + ", ciudad="
+ ciudad + ", telefono=" + telefono + "]";
}
}
Write the following to a file:
package parsear;
import math;
import java.io.*;
//Add the following imports
import javax.jdo.annotations.Column;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable(table="TEST_EMPLEADO")
public class Persona {
@PrimaryKey
//@Persistent(primaryKey="true", valueStrategy = IdGeneratorStrategy.IDENTITY)
@Persistent
@Column(name = "c_name", length = 200)
private String nombre;
@Persistent
@Column(name="c_edad")
private int edad;
@Persistent
@Column(name = "c_ciudad", length = 200)
private String ciudad;
@Persistent
@Column(name = "c_phone", length = 200)
private int telefono;
public Persona(){}
/**
** Preserve all comments and indentation
*/
public Persona(String nombre, int edad, String ciudad, int telefono) {
super();
this.nombre = nombre;
this.edad = edad;
this.ciudad = ciudad;
this.telefono = telefono;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
//Even one line comments
public int getEdad() {
return edad;
}
public void setEdad(int edad) {
this.edad = edad;
}
public String getCiudad() {
return ciudad;
}
public void setCiudad(String ciudad) {
this.ciudad = ciudad;
}
public int getTelefono() {
return telefono;
}
public void setTelefono(int telefono) {
this.telefono = telefono;
}
@Override
public String toString() {
return "Persona [nombre=" + nombre + ", edad=" + edad + ", ciudad="
+ ciudad + ", telefono=" + telefono + "]";
}
}
Thank you very much, greetings.
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.