How to get problems in CompilationUnit regarding imports

189 Views Asked by At

I want to detect an error regarding an import statement. But compilation unit seems to detect errors inside the class declaration itself.

My code on which I am experimenting is as follows :

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import org.apache.commons.*;

public class Bad {

    Random r1 = new Random();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    int x = new Random().nextInt(3);
    double y = new Random().nextDouble();

    int z = 10

}

I have not added apache commons in my project. I am able to see two errors in my astview as follows.
1) E[90, 10] The import org.apache cannot be resolved
2) E[323, 2] Syntax error, insert ";" to complete FieldDeclaration

But when I tried to print the number of problems using CompilationUnit it is returning only one and the message is as follows
1) Syntax error, insert ";" to complete FieldDeclaration

How can I get the import error programmatically using the compilation unit? Or is there any way I can do this? Thanks in advance.

1

There are 1 best solutions below

0
Arun Krishna On

When you say astview, I'm gonna assume you are talking about eclipse ASTView plugin. The ASTView plugin is part of the IDE and is built using all the information the IDE has. This includes the libraries that have been added to the project.

Compilation unit is creating an ast using only the source code. It has no knowledge about the libraries that are added in the project. It can only detect syntactic errors and hence detects the semicolon missing issue.

You can check JavaConventions validateImportDeclaration.