Q:

How to determine TypeDeclaration is a class?(TypeDeclaration cannot be converted to Class)

I want to get all class names in .java file.But i can't determine TypeDeclaration is a class or something.
I try to use following code snippet and I get error
    if (type instanceof Class))
    {return packagePrefix + type.getName();}

Error:TypeDeclaration cannot be converted to Class
Full Code:
public static String parseClassname(File filename) throws Exception {
    try (FileInputStream fin = new FileInputStream(filename)) {
        CompilationUnit cu = JavaParser.parse(fin);
        String packagePrefix = cu.getPackage().getName().toString();
        if (!packagePrefix.isEmpty()) packagePrefix += ".";

        for (TypeDeclaration type : cu.getTypes())
        {
            if (type instanceof Class)) ////
            {return packagePrefix + type.getName();}      

        }
    }
    return null;
}

A:

Class is part of Java reflection API for representing types at run-time. If you would like to get the first class declaration from the source, you should use ClassOrInterfaceDeclaration with Java parser, and check that isInterface is false:
if (!(type instanceof ClassOrInterfaceDeclaration))) {
    continue;
}
ClassOrInterfaceDeclaration classOrInterface = (ClassOrInterfaceDeclaration)type;
if (!classOrInterface.isInterface()) {
    return packagePrefix + type.getName();
}

