Annotating java class for DataNucleus with ANTLR4

32 views
Skip to first unread message

Jav

unread,
Nov 20, 2014, 2:04:46 AM11/20/14
to antlr-di...@googlegroups.com

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.

Bence Erős

unread,
Nov 20, 2014, 9:09:40 AM11/20/14
to antlr-di...@googlegroups.com
Hello,

I'm afraid you will have to slightly modify the Java.g4 grammar, since that one skips the whitespaces and comments.

--
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.



--
Bence Erős
CyclonePHP
core developer
Reply all
Reply to author
Forward
0 new messages