-
-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with qualified names of CtTypeReference when using setNoClasspath(true) #465
Comments
It's not always possible to determine the qualified name of a reference when the reference type is not in the classpath. Please provides the code that you try to Spoon so that we can verify the behavior of Spoon on your specific case. |
This is a minimal example program that uses an external library: package example;
import com.google.common.base.Joiner;
public class Demo {
public static void main(String[] args) {
System.out.println(Joiner.on(" ").join("Hello", "World!"));
}
} I use this code to analyze it with spoon: package analysis;
import java.io.File;
import java.util.Arrays;
import spoon.Launcher;
import spoon.SpoonModelBuilder;
import spoon.processing.AbstractProcessor;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtTypeReference;
public class Analyzer {
public static void main(String[] args) {
Launcher spLauncher = new Launcher();
spLauncher.getEnvironment().setNoClasspath(true);
SpoonModelBuilder spModelBuilder = spLauncher.createCompiler();
spModelBuilder.addInputSource(new File("../TestProject/src"));
spModelBuilder.build();
spModelBuilder.process(Arrays.asList(new TypeReferenceProcessor()));
}
private static class TypeReferenceProcessor extends AbstractProcessor<CtType<?>> {
@Override
public void process(CtType<?> spType) {
for (CtTypeReference<?> spRef : spType.getReferencedTypes()) {
System.out.println(spRef.getQualifiedName());
}
}
}
} The output from the above Analyzer-class is:
The qualified name of the reference to |
I am using Spoon 4.4.1 by the way... |
I Created a PR that fix your problem, unfortunately I found an other related issue that I was not able to resolve. |
Thank you very much for the quick solution! Will there be a bug fix release or will this only be available in the next major/minor release? |
No problem. For the release date, I don't know. The spoon maintainers are on holidays until next week. There is still an issue with static fields that are not in the classpath, the current spoon model is not correct... I need to discuss with the maintainers to solve this issue. |
Alright. I will try to build Spoon including your patch by myself then. Is there any documentation with instructions on how to build Spoon from source? |
I thought yes, but I can not find the instructions. Build instructions
|
Thank you very much for these detailed instructions! It works :) |
I discovered a use case where the problem still occurs . I extended the above example code by implementing an Interface: package example;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
public class Demo implements Function<String, String> {
public static void main(String[] args) {
System.out.println(Joiner.on(" ").join("Hello", "World!"));
}
@Override
public String apply(String input) {
return input;
}
} The result of the processor is now:
As you can see public void foo(Function<> f) {
} Now when I also add a member Variable of that type... package example;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
public class Demo implements Function<String, String> {
private Function f;
public static void main(String[] args) {
System.out.println(Joiner.on(" ").join("Hello", "World!"));
}
@Override
public String apply(String input) {
return input;
}
} the corresponding reference will contain the package name, which leads to two different CtTypeReferences:
So, from an outsider's point of view, it looks like Spoon doesn't relate the import statement with type references from |
Thanks, we will work on it. |
When settings setNoClasspath(true) on the environment, unresolved type references don't have the correct qualified name.
getQualifiedName()
returns the simple name (without the package prefix) instead.I discovered that behaviour with this processor:
The text was updated successfully, but these errors were encountered: