Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add java dumper #9

Open
nitzano opened this issue Jul 14, 2018 · 2 comments
Open

Add java dumper #9

nitzano opened this issue Jul 14, 2018 · 2 comments
Labels
enhancement New feature or request

Comments

@nitzano
Copy link
Owner

nitzano commented Jul 14, 2018

Is your feature request related to a problem? Please describe.

Yes, there is only java parser at the moment without the ability to dump to java.

Describe the solution you'd like

A java dumper.

Additional context

@nitzano nitzano changed the title Adding java dumper Add java dumper Jul 14, 2018
@nitzano nitzano added enhancement New feature or request and removed enhancement New feature or request labels Jul 14, 2018
@liron-navon
Copy link

liron-navon commented Aug 22, 2018

@nitzano
Since the project started by converting enums from javascript and python, it included numeric and boolean values in them, In Java Enums simply don't have that kind of logic, for example here is a python enum, lets see how we can convert it manually:

from enum import Enum, auto, IntEnum

class Colors(Enum):
    Green = 1
    Yellow = auto()
    Blue = 'Blue'
    Red = True

Enums in python/javascript also have types, while in java, enums are of their own type (in python - auto()), there are work arounds, but they are not straight foreward, for instance we can do this, which includes a lot of boiler plate code for type conversions:

enum Colors{
    Green(1), Yellow("Yellow"), Blue("Blue"), Red(true);

    private boolean b;
    private int i;
    private String s;

    Colors(int n) {
        this.n= n;
    }

    Colors(boolean b) {
        this.b= b;
    }

    Colors(String s) {
        this.s= s;
    }

    public int numeric() {
        return n;
    }

    public boolean bool() {
        return b;
    }

    public String string() {
        return s;
    }
}

which will assert like this:

Int greenValue = Colors.Green.numeric(); // 1
String greenName= String.valueOf(Colors.Green); // "Green"

// and we also get Enum typing which could be important for java development
public class Test{
   public paint(Colors color) {/* do something with the color here*/}
}

Test t = new Test();
t.paint(1) // type error
t.paint(Colors.Green) // acceptable

But I didn't find another way to create enums with types in java... Is this a valid solution?
It could probably be better to do an object with static constants, But It is not a real "Enum" (with a type of an enum), so that is a question...

public class Colors {
   final static int Green = 1;
   final static String Yellow= "Yellow ";
   final static String Blue = "Blue ";
   final static boolean Red  = true;
}

@nitzano
Copy link
Owner Author

nitzano commented Aug 22, 2018

thanks! see CONTRIBUTING.md for general help.

@nitzano nitzano assigned nitzano and unassigned nitzano Aug 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants