getSupers is empty

Answered

I am developing a plugin which shall analyse all java files in a project given a path.

So the first point is to get the project:

 

String projectPath = "/data/";
ProjectManager projectManager = ProjectManager.getInstance();

Project project = projectManager.loadAndOpenProject(projectPath);

ExternalSystemUtil.refreshProject(
        project,
        projectSystemId,
        "/data",
        new ExternalProjectRefreshCallback() {
            public void onSuccess(DataNode<ProjectData> externalProject) {
                System.out.println("Project dependencies synchronized successfully.");

                ApplicationManager.getApplication().runReadAction(() -> {
                            // Your code that needs read access goes here
                    JavaFileAnalyzer analyzer = new JavaFileAnalyzer(project);
                    analyzer.analyze();

 

And then I want to analyse the project.

 

I first load all files within the project

 

List<VirtualFile> allFiles = new ArrayList<>();
VirtualFile baseDir = project.getBaseDir();
VfsUtilCore.iterateChildrenRecursively(baseDir, virtualFile -> true, virtualFile -> {
    if (!virtualFile.isDirectory() && virtualFile.getFileType() == JavaFileType.INSTANCE) {
        allFiles.add(virtualFile);
    }
    return true;
});

// Does not work
//virtualFiles = com.intellij.psi.search.FileTypeIndex.getFiles(JavaFileType.INSTANCE,
//        GlobalSearchScope.projectScope(project));

 

For every class i find

 

Collection<PsiClass> classesInFile = PsiTreeUtil.findChildrenOfType(currentFile, PsiClass.class);
for (PsiClass currentClass : classesInFile) {
    if (currentClass.getQualifiedName() != null) {
        traverseClasses(currentClass);

 

I want to traverse it.

But when analyzing a class.

PsiClass[] supers = currentClass.getSupers();
System.out.println("--Supers: " + supers.length);
for(PsiClass superClazz : supers){
    System.out.println("--Super: " + superClazz.getQualifiedName());
}

 

I get an empty list of supers. Altough using the reference extend list.

PsiReferenceList extendsList = currentClass.getExtendsList();

I get results but the classes don't have a qualified name and only the normal name.

 

What am i missing here? Why do i get supers an empty list.

0
3 comments

idea-parser_1  | Class: HelloApplication QN: com.example.testproject.HelloApplication
idea-parser_1  | --No superclass detected or not in scope
idea-parser_1  | --Supers: 0
idea-parser_1  | --Implements: 0
idea-parser_1  | Using Reference
idea-parser_1  |   Extends: Application

i dont know any further…

 

// print the class name
System.out.println("Class: " + currentClass.getName() + " QN: " + currentClass.getQualifiedName());

// print the superclass
PsiClass superClass = currentClass.getSuperClass();
if (superClass != null) {
    System.out.println("--Extends: " + superClass.getQualifiedName());
} else {
    System.out.println("--No superclass detected or not in scope");
}

PsiClass[] supers = currentClass.getSupers();
System.out.println("--Supers: " + supers.length);
for(PsiClass superClazz : supers){
    System.out.println("--Super: " + superClazz.getQualifiedName());
}

// print the implemented interfaces
PsiClass[] interfaces = currentClass.getInterfaces();

System.out.println("--Implements: " + interfaces.length);

System.out.println("Using Reference");

PsiReferenceList extendsList = currentClass.getExtendsList();
PsiReferenceList implementsList = currentClass.getImplementsList();

if (extendsList != null) {
    for (PsiJavaCodeReferenceElement reference : extendsList.getReferenceElements()) {
        System.out.println("  Extends: " + reference.getQualifiedName());
    }
}
if (implementsList != null) {
    for (PsiJavaCodeReferenceElement reference : implementsList.getReferenceElements()) {
        System.out.println("  Implements: " + reference.getQualifiedName());
    }
}

System.out.println("");

 

 

it does not show me the superclasses … i think the sdk is somehow not correctly defined. But this is my Dockerfile

 

FROM ubuntu:jammy-20230624

WORKDIR /app

# Install necessary packages and Java 17
RUN apt-get update \
  && apt-get install -y unzip wget libfreetype6 fontconfig openjdk-17-jdk \
  && rm -rf /var/lib/apt/lists/*

# Set JAVA_HOME environment variable
ENV JAVA_HOME /usr/lib/jvm/java-17-openjdk-amd64

# Download and extract IntelliJ IDEA
RUN wget https://download.jetbrains.com/idea/ideaIC-2023.2.tar.gz \
    && tar -xzf ideaIC-2023.2.tar.gz \
    && rm ideaIC-2023.2.tar.gz \
    && mv idea-* idea

# Set the JAVA_HOME for IntelliJ IDEA
RUN echo "idea.jdk=$JAVA_HOME" >> /app/idea/bin/idea.properties

# Create Maven repository directory
RUN mkdir -p /root/.m2/repository

# Set the working directory to /app/idea for plugin operations
WORKDIR /app/idea

# Download and install the Gradle plugin for IntelliJ IDEA
RUN wget -O gradle-intellij-plugin.zip "https://plugins.jetbrains.com/plugin/download?rel=true&updateId=410281" \
    && mkdir -p plugins/gradle-intellij-plugin \
    && unzip gradle-intellij-plugin.zip -d plugins/gradle-intellij-plugin \
    && rm gradle-intellij-plugin.zip

# Copy your custom plugin and formatter
COPY build/distributions/formatter-plugin plugins/formatter-plugin
COPY formatter /usr/bin/formatter

# Set the working directory back to /data
WORKDIR /data

ENTRYPOINT ["formatter"]
 

0

Hi Nils,

Javadoc of com.intellij.psi.PsiClass#getSupers states:

Returns the array of classes and interfaces extended or implemented by the class.
Returns:
the array of classes or interfaces. May return zero elements when jdk is not configured, so no java.lang.Object is found

Are you sure the project you are analyzing has the JDK configured?

Check File | Project Structure… and see the Project tab - it should have SDK set to a JDK.

0

Please sign in to leave a comment.