Skip to content
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

[Bug]: Build model with source code from stdlib #5290

Closed
zentox opened this issue Jun 19, 2023 · 3 comments · Fixed by #5294
Closed

[Bug]: Build model with source code from stdlib #5290

zentox opened this issue Jun 19, 2023 · 3 comments · Fixed by #5294
Labels

Comments

@zentox
Copy link

zentox commented Jun 19, 2023

Describe the bug

I am trying to analyze the source code from the stdlib. Therefore I wrote an interface to retrieve the source code from the stdlib and add the content of the classes to the spoon launcher. It works for some classes e.g. Object but for some classes e.g. Class it seems to not work.

Source code you are trying to analyze/transform

java.lang.Class

Source code for your Spoon processing

Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(17);
launcher.getEnvironment().setIgnoreSyntaxErrors(true);

String className = "java.lang.Class";
String content = ...; // Source code of Class.java

VirtualFile file = new VirtualFile(content, className);


launcher.addInputResource(file);
CtModel model = launcher.buildModel();

CtElement element =  model.getAllTypes().stream()
                          .filter(it -> it.getQualifiedName().equals(className))
                          .findFirst()
                          .orElseThrow();

Actual output

Unknown TypeBinding: class org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding Unresolved type java.lang.Class
java.lang.RuntimeException: Unknown TypeBinding: class org.eclipse.jdt.internal.compiler.lookup.UnresolvedReferenceBinding Unresolved type java.lang.Class
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:866)
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:825)
	at spoon.support.compiler.jdt.ReferenceBuilder.getParameterizedTypeReference(ReferenceBuilder.java:900)
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:840)
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:825)
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReferenceFromTypeArgument(ReferenceBuilder.java:938)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:992)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeArguments(ReferenceBuilder.java:923)
	at spoon.support.compiler.jdt.ReferenceBuilder.getParameterizedTypeReference(ReferenceBuilder.java:910)
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:840)
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:825)
	at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:592)
	at spoon.support.compiler.jdt.ReferenceBuilder.buildTypeReference(ReferenceBuilder.java:138)
	at spoon.support.compiler.jdt.ReferenceBuilder.buildTypeReference(ReferenceBuilder.java:132)
	at spoon.support.compiler.jdt.JDTTreeBuilder.createParameterizedType(JDTTreeBuilder.java:1481)
	at spoon.support.compiler.jdt.JDTTreeBuilder.visit(JDTTreeBuilder.java:1469)
	at org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference.traverse(ParameterizedSingleTypeReference.java:405)
	at org.eclipse.jdt.internal.compiler.ast.Argument.traverse(Argument.java:275)
	at org.eclipse.jdt.internal.compiler.ast.MethodDeclaration.traverse(MethodDeclaration.java:427)
	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.traverse(TypeDeclaration.java:1831)
	at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.traverse(TypeDeclaration.java:1683)
	at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:827)
	at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:788)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.traverseUnitDeclaration(JDTBasedSpoonCompiler.java:481)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.lambda$buildModel$0(JDTBasedSpoonCompiler.java:438)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.forEachCompilationUnit(JDTBasedSpoonCompiler.java:465)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildModel(JDTBasedSpoonCompiler.java:436)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:373)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:336)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:117)
	at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:100)
	at spoon.Launcher.buildModel(Launcher.java:782)

Expected output

No Error

Spoon Version

10.3.0

JVM Version

17.0.6

What operating system are you using?

Windows 11

@zentox
Copy link
Author

zentox commented Jun 24, 2023

The problem occurs when we are traversing SoftReference<ReflectionData<T>> in java.lang.Class.

The binding that trigger the error: ParametrizedTypeBinding

private static class ReflectionData<T>
	extends NULL TYPENULL SUPERINTERFACES
	enclosing type : Unresolved type java.lang.ClassNULL FIELDSNULL METHODS

The call of

private CtTypeReference<?> getParameterizedTypeReference(ParameterizedTypeBinding binding) {

will call the binding's enclosing type which is unresolved and this will throw an error here:
<T> CtTypeReference<T> getTypeReference(TypeBinding binding, boolean resolveGeneric) {

@MartinWitt
Copy link
Collaborator

MartinWitt commented Jun 24, 2023

Hey,

The core problem of all code to reproduce is GPL licensed and the review @I-Al-Istannen was against merging it. You are welcome to discuss with him. I asked the JDT team if they are aware of other code to create such a bind,eclipse-jdt/eclipse.jdt.core#1186. These bindings are the last resort of JDT and probably a JDT bug. We haven't seen one in 15+ years.

@MartinWitt
Copy link
Collaborator

The JDT team answered me:

'UnresolvedReferencedBinding' is a proxy of sorts for an as yet uninternalized class file. The relevant class files will be internalized just when needed - during which process additional URBs may he created for other referenced classes which may get internalized at a still later time on demand.

In my understanding we should never get these bindings but in some special cases. Looks like java.lang.Class is one. The fix is obvious and only blocked by @I-Al-Istannen and his GPL concerns.

zentox added a commit to tudalgo/algoutils that referenced this issue Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants