Skip to content

Imports are not part of the visited Spoon AST #5168

Open
@honghao12

Description

@honghao12

Describe the bug

I tried to access the import element through the ctimport filter, but it wasn't available

Source code you are trying to analyze/transform

package src.p4;

import src.p1.A;

import java.util.Date;

public class E implements D {
    public static int e = 5;
    protected final int d = 4;
    int a = 1;
    private final int c = 3;

    public E() {
        System.out.println("this is a constructor!");
    }

    @Override
    public void fun1() {
        // test fun1
        int b = 2;
        System.out.println("global:" + a);
        System.out.println("local:" + b);
    }

    @Override
    public void fun3() {
        Date date = new Date();
        A a1 = new A();
        a1.m1();
        long time = date.getTime();
        System.out.println("time:" + time);
    }

    public final void fun2() {
        System.out.println("final fun!!");
    }
}

Source code for your Spoon processing

Launcher launcher = new Launcher();
  launcher.getEnvironment().setComplianceLevel(11);
  launcher.getEnvironment().setAutoImports(true);
  launcher.addInputResource("D:\\test\\FILE_STORY\\java\\javademo2\\src\\p4\\E.java");
  CtModel model = launcher.buildModel();

  List<CtImport> imports = model.getElements(new TypeFilter<>(CtImport.class));
  System.out.println("size:"+imports.size());

Actual output

size:0

Expected output

size:2

Spoon Version

10.3.0

JVM Version

11

What operating system are you using?

win10

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions