1

I read java file using FileReader that contains some method. How i can read method scope (method area inside) to find duplicate variable?

For example, this is a java file that i read:

public double[] copyArray(double[] data) {
    int _nn = data.length;
    double[] _tmp = new double[_nn];

    System.arraycopy(data, 0, _tmp, 0, _nn);
    int _nn;
    _nn = tmp;

    return _tmp;
}

How to know method scope?, i mean between { and }, if scope has found, find duplicate variable such as example above (int _nn) duplicated.

[addition] i have tried using java parser, then is success. Then i should send the results using list, but only last method in the list. What's wrong with my code?

This is a MethodVisitor:

private static class MethodVisitor extends VoidVisitorAdapter {
    private List<String> list = new ArrayList<String>();

    @Override
    public void visit(MethodDeclaration n, Object file) {
        list.add(n.getName());

    }
    public List<String> getList() {
        return list;
    }
}

Then, this is a method to call MethodVisitor:

private MethodVisitor mv;

public void doIt(File file) throws Exception {
    CompilationUnit cu;
    try {
        // parse the file
        cu = JavaParser.parse(file);
    } finally {
        //file.close();
    }
    // visit and print the methods names
    mv = new MethodVisitor();
    mv.visit(cu, file);

    List<String> list = mv.getList();

    for(String item:list){
        System.out.println(item);
    }
} 
Samsul Arifin
  • 247
  • 1
  • 6
  • 24
  • use a stack to keep track of `{` and `}` – Ramanlfc Dec 04 '15 at 08:05
  • 1
    You use a Java parser, or a compiler such as the Eclipse compiler, which will provide extensive error information and warnings (e.g. field hiding). – Andreas Dec 04 '15 at 08:06
  • Compile the code. Javac won't let you declare duplicate variables. – Andy Turner Dec 04 '15 at 08:08
  • @Ramanlfc, i am afraid if there are more than one { inside method. – Samsul Arifin Dec 04 '15 at 08:09
  • @Andreas, would you mind if you give me an example? – Samsul Arifin Dec 04 '15 at 08:09
  • @AndyTurner, that's what i mean, if duplicate variable found, then remove it. – Samsul Arifin Dec 04 '15 at 08:11
  • 1
    That isn't what your question asks: you ask how to *find* duplicate variables. – Andy Turner Dec 04 '15 at 08:12
  • How would you know that simply removing it is the right solution? Why do you even have this problem? Code is either written by users and they should fix it, or it is generated by other code which shouldn't generate bad code. Trying to scan source code for something like this is the wrong approach. Kick users who left compiler errors and tell them to fix it, or fix the code generator to not generate bad code in the first place. – Andreas Dec 04 '15 at 08:17

1 Answers1

3

1 you need to parse java code:

http://code.google.com/p/javaparser/

or read this:

Java source code parsers/generators

2 after that, try something and show your code

Community
  • 1
  • 1
  • Please use the new version of JavaParser (https://github.com/javaparser/javaparser). The one you referred to has not been updated for several years and it supports Java 1.5, while the new one is actively maintained and it supports Java 1.8 – Federico Tomassetti Dec 08 '15 at 09:21
  • @FedericoTomassetti thanks for your reply, i have got all the method, then i want to ask you how to get all variable in each method? thanks :D – Samsul Arifin Dec 10 '15 at 15:26