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

NoSuchElementException when solving type #3030

Closed
4everTheOne opened this issue Jan 8, 2021 · 0 comments · Fixed by #3031
Closed

NoSuchElementException when solving type #3030

4everTheOne opened this issue Jan 8, 2021 · 0 comments · Fixed by #3031
Milestone

Comments

@4everTheOne
Copy link
Contributor

4everTheOne commented Jan 8, 2021

There's a bug in the class JavaParserClassDeclaration that doesn't allow the resolution of the following example code.
This example was taken from Guava Collect Spliterators as a sample:

package com.example;

import java.util.Comparator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.function.IntFunction;
import java.util.stream.IntStream;

public class TestClass {

    static <T> Spliterator<T> indexed(int size, int extraCharacteristics, IntFunction<T> function, Comparator<? super T> comparator) {
        class WithCharacteristics implements Spliterator<T> {

            private final Spliterator.OfInt delegate;

            WithCharacteristics(Spliterator.OfInt delegate) {
                this.delegate = delegate;
            }

            @Override
            public boolean tryAdvance(Consumer<? super T> action) {
                return delegate.tryAdvance((IntConsumer) i -> action.accept(function.apply(i)));
            }

            @Override
            public void forEachRemaining(Consumer<? super T> action) {
                delegate.forEachRemaining((IntConsumer) i -> action.accept(function.apply(i)));
            }

            @Override
            public Spliterator<T> trySplit() {
                Spliterator.OfInt split = delegate.trySplit();
                return (split == null) ? null : new WithCharacteristics(split);
            }

            @Override
            public long estimateSize() {
                return delegate.estimateSize();
            }

            @Override
            public int characteristics() {
                return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED | extraCharacteristics;
            }

            @Override
            public Comparator<? super T> getComparator() {
                if (hasCharacteristics(Spliterator.SORTED)) {
                    return comparator;
                } else {
                    throw new IllegalStateException();
                }
            }
        }
        return new WithCharacteristics(IntStream.range(0, size).spliterator());
    }
}

Expected Behavior

When executing the test with the example above the field type should be resolved as java.util.Spliterator.OfInt.

Current Behavior

When resolving the example above an java.util.NoSuchElementException is thrown.

java.util.NoSuchElementException: No value present

	at java.util.Optional.get(Optional.java:135)
	at com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration.getAncestors(JavaParserClassDeclaration.java:357)
	at com.github.javaparser.symbolsolver.javaparsermodel.contexts.JavaParserTypeDeclarationAdapter.checkAncestorsForType(JavaParserTypeDeclarationAdapter.java:117)
	at com.github.javaparser.symbolsolver.javaparsermodel.contexts.JavaParserTypeDeclarationAdapter.solveType(JavaParserTypeDeclarationAdapter.java:99)
	at com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext.solveType(ClassOrInterfaceDeclarationContext.java:142)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.convertToUsage(JavaParserFacade.java:668)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.convert(JavaParserFacade.java:728)
	at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.convert(JavaParserFacade.java:724)
	at com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserFieldDeclaration.getType(JavaParserFieldDeclaration.java:62)
	at pt.forever.university.analyser.TestBug.test0(TestBug.java:103)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)

Test for issue

@Test
public void test3030() {
    String sourceCode = "package com.example;\n" +
            "\n" +
            "import java.util.Comparator;\n" +
            "import java.util.Spliterator;\n" +
            "import java.util.function.Consumer;\n" +
            "import java.util.function.IntConsumer;\n" +
            "import java.util.function.IntFunction;\n" +
            "import java.util.stream.IntStream;\n" +
            "\n" +
            "public class TestClass {\n" +
            "\n" +
            "    static <T> Spliterator<T> indexed(int size, int extraCharacteristics, IntFunction<T> function, Comparator<? super T> comparator) {\n" +
            "        class WithCharacteristics implements Spliterator<T> {\n" +
            "\n" +
            "            private final Spliterator.OfInt delegate;\n" +
            "\n" +
            "            WithCharacteristics(Spliterator.OfInt delegate) {\n" +
            "                this.delegate = delegate;\n" +
            "            }\n" +
            "\n" +
            "            @Override\n" +
            "            public boolean tryAdvance(Consumer<? super T> action) {\n" +
            "                return delegate.tryAdvance((IntConsumer) i -> action.accept(function.apply(i)));\n" +
            "            }\n" +
            "\n" +
            "            @Override\n" +
            "            public void forEachRemaining(Consumer<? super T> action) {\n" +
            "                delegate.forEachRemaining((IntConsumer) i -> action.accept(function.apply(i)));\n" +
            "            }\n" +
            "\n" +
            "            @Override\n" +
            "            public Spliterator<T> trySplit() {\n" +
            "                Spliterator.OfInt split = delegate.trySplit();\n" +
            "                return (split == null) ? null : new WithCharacteristics(split);\n" +
            "            }\n" +
            "\n" +
            "            @Override\n" +
            "            public long estimateSize() {\n" +
            "                return delegate.estimateSize();\n" +
            "            }\n" +
            "\n" +
            "            @Override\n" +
            "            public int characteristics() {\n" +
            "                return Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED | extraCharacteristics;\n" +
            "            }\n" +
            "\n" +
            "            @Override\n" +
            "            public Comparator<? super T> getComparator() {\n" +
            "                if (hasCharacteristics(Spliterator.SORTED)) {\n" +
            "                    return comparator;\n" +
            "                } else {\n" +
            "                    throw new IllegalStateException();\n" +
            "                }\n" +
            "            }\n" +
            "        }\n" +
            "        return new WithCharacteristics(IntStream.range(0, size).spliterator());\n" +
            "    }\n" +
            "\n" +
            "}\n";

    ReflectionTypeSolver reflectionSolver = new ReflectionTypeSolver();
    JavaSymbolSolver symbolSolver = new JavaSymbolSolver(reflectionSolver);
    StaticJavaParser.getConfiguration()
            .setSymbolResolver(symbolSolver);

    CompilationUnit cu = StaticJavaParser.parse(sourceCode);
    ResolvedType resolvedField = cu.findFirst(FieldDeclaration.class).get().resolve().getType();
    assertEquals(Spliterator.OfInt.class.getCanonicalName(), resolvedField.asReferenceType().getQualifiedName());
}
4everTheOne added a commit to 4everTheOne/javaparser that referenced this issue Jan 8, 2021
Added unit test that was provided in the issue javaparser#3030.
jlerbsc added a commit that referenced this issue Jan 9, 2021
Fix issue #3030 NoSuchElementException when solving type
@MysterAitch MysterAitch added this to the next release milestone Feb 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants