0

I have been trying and trying for a while with this and I just seem to cannot solve it.

I am supposed to extract classes from a Java-file and print in a shape UML-diagram, in the IDE or writing on a file

e.g the program

 public class Complex {
    private int re;
    private int im;

    public Complex(int re, int im) {
        this.re = re;
        this.im = im;
    }

    public Complex add(Complex h) {
        return new Complex(this.re + h.re, this.im + h.im);
    }

    public Complex sub(Complex h) {
        return new Complex(this.re - h.re, this.im - h.im);
    }

    public Complex mul(Complex h) {
        int a = re;
        int b = im;
        int c = h.re;
        int d = h.im;
        return new Complex(a * c - b * d, b * c + a * d);
    }

    public Complex div(Complex h) {
        int a = re;
        int b = im;
        int c = h.re;
        int d = h.im;
        return new Complex((a * c + b * d) / (c * c + d * d), (b * c - a * d)
                / (c * c + d * d));
    }

    public String toString() {
        if (im >= 0) {
            return re + " + " + im + "i";
        } else
            return re + "  " + im + "i";
    }
}

Should generate something like:

a Complex
b  int re
b  int re
a  Complex(re:int, im:int)
a  add():Complex
a  sub():Complex
a  mul():Complex
a  div():Complex
a  toString():String
a main()

I have started with stripping the first parenthesis from the file;

import java.io.*;

public class ClassExtract {

    public static void main(String[] args) {

        ClassExtract obj = new ClassExtract();
        obj.removeBracket("Complexx.txt");
    }

    public void removeBracket(String filnamn) {

        try {
            File f = new File(filnamn);
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);

            FileWriter fw = new FileWriter("noparanthesis_" + filnamn);
            BufferedWriter bw = new BufferedWriter(fw);

            String rad = br.readLine();
            while (rad != null) {
                rad = rad.replaceAll("\\(", " ");
                bw.write(rad);
                bw.newLine();
                rad = br.readLine();
            }

            bw.close();
            br.close();

        } catch (FileNotFoundException e) {
            System.out.printf("The file " + filnamn + " was not found.");
        } catch (IOException e) {
            System.out.printf("Writing error.");
        }
    }
}

I have thought of different ways of approaching this problem. The way that I think would be easiest would be to strip everything after collected the head public class Complex, which would mean the rest of the file would look something like:

public Complex int re, int im
public Complex add Complex h
public Complex sub Complex h

etc and do the same and read the index of the lines.

I actually feel very lost and I have hard to tackle this problem, any help would really be appreciated.

2
  • do you need to parse the java? or can you load the compiled class with a classloader and use reflection? Commented Feb 16, 2012 at 21:04
  • Yeah, I have to read a .java-file and make a .txt-file with the result or have it directly in my IDE console. @Qwe I am reading about reflection now, it seems it can be handy since it obtains the classes or such - but a little concrete example could help alot with how you think I could implement it. Commented Feb 16, 2012 at 21:07

1 Answer 1

1

As Qwe says in the comment, you would be much better off looking at the Java Reflection APIs.

These let you inspect a class and list its methods, superclasses, interfaces and so on.

By tackling this problem with regexes/text analysis, you are basically trying to write a partial Java parser, which is an entirely unnecessary amount of pain!

Sign up to request clarification or add additional context in comments.

3 Comments

Not quite sure what you mean, The point is that Java will deal with all the textual details and just provide the high-level class relationships and method signatures that you need. Even if the .java is strangely formatted, for example!
Yeah, nevermind that comment, I thought wrong. Class aClass = MyObject.class Constructor[] constructors = aClass.getConstructors(); I am a bit confused..
Anyone? I am currently experimenting with reflection-abilities from this website: java.sun.com/developer/technicalArticles/ALT/Reflection but I just cant to seem to get the methods parameters right and with right output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.