dimanche 5 mars 2017

Library to generate .java files from Java reflection

I'm developing a simple application that should generate a .Java file starting from a Class Diagram.

I was wondering if there is a Library which, given a Class defined through reflection with his own Methods/Fields, creates well-formatted java code.

What i wrote until now (it's a draft) is something like this, but it doesn't Indent the code.

for (*every object in the diagram*) {
                    if (*the object is a class*) {

                        //prepare content for new file java
                        lines = new ArrayList<String>();
                        ClassModel classe = *the class in the diagram*;
                        lines.add("Public class " + classe.getName() + "{");
                        for (Attribute a : classe.attributes) {
                            lines.add(a.getProtection() + " " + a.getType() + " " + a.getName() + ";");
                        }
                        for (Method m : classe.methods) {
                            lines.add(m.getProtection() + " " + m.getReturnType() + " " + m.getName() + " ();");
                        }

                        lines.add("}");


                        //prepare path for new file java
                        Path fileJava = Paths.get(currentModel.getTitle() + ".java");

                        //write file java
                        try {
                            Files.write(fileJava, lines, Charset.forName("UTF-8"));
                        } catch (IOException exc) {
                            return getFailureMessage();
                        }
                    }
                    //Else is not a class
                }





Aucun commentaire:

Enregistrer un commentaire