j3.27.0.0 working

This commit is contained in:
Mann Patel
2025-10-11 02:02:27 -06:00
parent 6c591bbf37
commit d80c25c5d7
2973 changed files with 383519 additions and 51603 deletions

View File

@@ -0,0 +1,5 @@
target
build
/.classpath
/.project
.settings

View File

@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>javaparser-parent</artifactId>
<groupId>com.github.javaparser</groupId>
<version>3.27.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>javaparser-symbol-solver-testing</artifactId>
<description>A Symbol Solver for Java, built on top of JavaParser (tests)</description>
<licenses>
<license>
<name>GNU Lesser General Public License</name>
<url>http://www.gnu.org/licenses/lgpl-3.0.html</url>
<distribution>repo</distribution>
</license>
<license>
<name>Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<profiles>
<profile>
<id>NonSlowTests</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedGroups>com.github.javaparser.SlowTest</excludedGroups>
<parallel>methods</parallel>
<threadCount>4</threadCount>
<!-- Note that <argLine> overwrites, not appends, hence need for `${argLine}` -->
<argLine>-Xms256m -Xmx2g -verbose:gc @{jacoco.javaagent}</argLine>
<reportFormat>plain</reportFormat>
<failIfNoTests>true</failIfNoTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>AlsoSlowTests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Note that <argLine> overwrites, not appends, hence need for `${argLine}` -->
<argLine>-Xms256m -Xmx2g -verbose:gc @{jacoco.javaagent}</argLine>
<reportFormat>plain</reportFormat>
<failIfNoTests>true</failIfNoTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<!-- no need to release this module -->
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
<dependency>
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-symbol-solver-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,33 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Tag("com.github.javaparser.SlowTest")
public @interface SlowTest {}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.ast.type;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class ClassOrInterfaceTypeTest {
private static final ParserConfiguration PARSER_CONFIGURATION = new ParserConfiguration();
@BeforeAll
public static void setup() {
ReflectionTypeSolver reflectionTypeSolver = new ReflectionTypeSolver();
JavaSymbolSolver javaSymbolSolver = new JavaSymbolSolver(reflectionTypeSolver);
PARSER_CONFIGURATION.setSymbolResolver(javaSymbolSolver);
}
private JavaParser javaParser;
@BeforeEach
public void beforeEach() {
javaParser = new JavaParser(PARSER_CONFIGURATION);
}
@Test
void resolveClassType() {
ParseResult<CompilationUnit> compilationUnit = javaParser.parse("class A {}");
assertTrue(compilationUnit.getResult().isPresent());
ClassOrInterfaceType classOrInterfaceType = StaticJavaParser.parseClassOrInterfaceType("String");
classOrInterfaceType.setParentNode(compilationUnit.getResult().get());
ResolvedReferenceType resolved = classOrInterfaceType.resolve().asReferenceType();
assertEquals(String.class.getCanonicalName(), resolved.getQualifiedName());
}
@Test
void testToDescriptor() {
ParseResult<CompilationUnit> compilationUnit = javaParser.parse("class A {}");
assertTrue(compilationUnit.getResult().isPresent());
ClassOrInterfaceType classOrInterfaceType = StaticJavaParser.parseClassOrInterfaceType("String");
classOrInterfaceType.setParentNode(compilationUnit.getResult().get());
assertEquals("Ljava/lang/String;", classOrInterfaceType.toDescriptor());
}
@Test
void testToDescriptorWithTypeVariables() {
ParseResult<CompilationUnit> compilationUnit =
javaParser.parse("public class A { public static <T extends String> void method(T arg); }");
assertEquals(
"(Ljava/lang/String;)V",
compilationUnit
.getResult()
.get()
.getType(0)
.getMethodsByName("method")
.get(0)
.toDescriptor());
}
}

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import com.github.javaparser.ast.Node;
import java.util.Optional;
import org.junit.jupiter.api.Test;
public interface AssociableToASTTest {
/**
* Helper method to cast the instance to the correct {@link Class}.
*
* @param instance The instance to be casted.
* @param clazz The expected {@link Class}.
* @param <T> The expected type.
*
* @return The instance casted as the correct type.
*/
default <T extends AssociableToAST> T safeCast(AssociableToAST instance, Class<T> clazz) {
if (clazz.isInstance(instance)) return clazz.cast(instance);
throw new UnsupportedOperationException(
String.format("Unable to cast %s into %s.", instance.getClass().getName(), clazz.getName()));
}
/**
* Create a new instance of {@link AssociableToAST} to be used for testing.
*
* @return The created instance.
*/
AssociableToAST createValue();
/**
* Get the node that can be associated with an AST.
*
* @param associableToAST The node that can be associated with an AST.
*
* @return The node being wrapped.
*/
Optional<Node> getWrappedDeclaration(AssociableToAST associableToAST);
@Test
default void checkThatToASTMatchesTheCorrectWrappedNode() {
AssociableToAST associableToAST = createValue();
Optional<Node> wrappedNode = getWrappedDeclaration(associableToAST);
if (wrappedNode.isPresent()) assertEquals(wrappedNode, associableToAST.toAst());
else assertFalse(associableToAST.toAst().isPresent());
}
@Test
default void checkThatToASTWithCorrectTypeMatchesTheCorrectWrappedNode() {
AssociableToAST associableToAST = createValue();
Optional<Node> wrappedNode = getWrappedDeclaration(associableToAST);
if (wrappedNode.isPresent())
assertEquals(wrappedNode, associableToAST.toAst(wrappedNode.get().getClass()));
else assertFalse(associableToAST.toAst().isPresent());
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
public interface HasAccessSpecifierTest {
HasAccessSpecifier createValue();
@Test
default void accessSpecifierCantBeNull() {
assertNotNull(createValue().accessSpecifier());
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
public interface ResolvedAnnotationDeclarationTest extends ResolvedReferenceTypeDeclarationTest {
@Override
ResolvedAnnotationDeclaration createValue();
@Test
default void getAnnotationMembersCantBeNull() {
assertNotNull(createValue().getAnnotationMembers());
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
public interface ResolvedAnnotationMemberDeclarationTest extends ResolvedValueDeclarationTest {
@Override
ResolvedAnnotationMemberDeclaration createValue();
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public interface ResolvedClassDeclarationTest
extends ResolvedReferenceTypeDeclarationTest, ResolvedTypeParametrizableTest, HasAccessSpecifierTest {
@Override
ResolvedClassDeclaration createValue();
@Test
default void resolvedClassShouldBeMarkedAsClass() {
assertTrue(createValue().isClass());
}
@Test
default void getSuperClassShouldNotBeNull() {
assertNotNull(createValue().getSuperClass());
}
@Test
default void getInterfacesShouldNotBeNull() {
assertNotNull(createValue().getInterfaces());
}
@Test
default void getAllSuperClasses() {
assertNotNull(createValue().getAllSuperClasses());
}
@Test
default void getAllInterfaces() {
assertNotNull(createValue().getAllInterfaces());
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
public interface ResolvedConstructorDeclarationTest extends ResolvedMethodLikeDeclarationTest, AssociableToASTTest {
@Override
ResolvedConstructorDeclaration createValue();
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public interface ResolvedDeclarationTest extends AssociableToASTTest {
ResolvedDeclaration createValue();
@Test
default void whenNameIsPresentACallForMethodGetNameShouldNotBeNull() {
ResolvedDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.hasName()) assertNotNull(resolvedDeclaration.getName());
else assertNull(resolvedDeclaration.getName());
}
@Test
default void whenDeclarationIsAFieldTheCallToTheMethodAsFieldShouldNotThrow() {
ResolvedDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isField()) assertDoesNotThrow(resolvedDeclaration::asField);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asField);
}
@Test
default void whenDeclarationIsAMethodTheCallToTheMethodAsMethodShouldNotThrow() {
ResolvedDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isMethod()) assertDoesNotThrow(resolvedDeclaration::asMethod);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asMethod);
}
@Test
default void whenDeclarationIsAParameterTheCallToTheMethodAsParameterShouldNotThrow() {
ResolvedDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isParameter()) assertDoesNotThrow(resolvedDeclaration::asParameter);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asParameter);
}
@Test
default void whenDeclarationIsAPatternTheCallToTheMethodAsPatternShouldNotThrow() {
ResolvedDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isTypePattern()) assertDoesNotThrow(resolvedDeclaration::asTypePattern);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asTypePattern);
}
@Test
default void whenDeclarationIsAEnumConstantTheCallToTheMethodAsEnumConstantShouldNotThrow() {
ResolvedDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isEnumConstant()) assertDoesNotThrow(resolvedDeclaration::asEnumConstant);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asEnumConstant);
}
@Test
default void whenDeclarationIsATypeTheCallToTheMethodAsTypeShouldNotThrow() {
ResolvedDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isType()) assertDoesNotThrow(resolvedDeclaration::asType);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asType);
}
/**
* According to the documentation in {@link AssociableToAST#toAst()}
* all the Resolved declaration most be associable to a AST.
*
* @see AssociableToAST#toAst()
*/
@Test
default void declarationMostBeAssociableToAST() {
ResolvedDeclaration resolvedDeclaration = createValue();
assertTrue(resolvedDeclaration instanceof AssociableToAST);
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public interface ResolvedEnumConstantDeclarationTest extends ResolvedValueDeclarationTest {
@Override
ResolvedEnumConstantDeclaration createValue();
@Test
default void enumConstantShouldBeMarkedAsEnum() {
assertTrue(createValue().isEnumConstant());
}
@Test
default void enumNameShouldNotBeNull() {
assertNotNull(createValue().getName());
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public interface ResolvedEnumDeclarationTest extends ResolvedReferenceTypeDeclarationTest {
@Override
ResolvedEnumDeclaration createValue();
@Test
default void resolvedEnumShouldBeMarkedAsEnum() {
assertTrue(createValue().isEnum());
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public interface ResolvedFieldDeclarationTest extends ResolvedValueDeclarationTest, HasAccessSpecifierTest {
/**
* Create a new non-static {@link ResolvedFieldDeclaration}.
*
* @return The non-static value.
*/
@Override
ResolvedFieldDeclaration createValue();
/**
* Create a new static {@link ResolvedFieldDeclaration}.
*
* @return The static value.
*/
ResolvedFieldDeclaration createStaticValue();
@Test
default void whenAFieldIsStaticShouldBeMarkedAsSuch() {
assertFalse(createValue().isStatic());
assertTrue(createStaticValue().isStatic());
}
@Test
default void theDeclaringTypeCantBeNull() {
assertNotNull(createValue().declaringType());
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
public interface ResolvedMethodDeclarationTest extends ResolvedMethodLikeDeclarationTest {
@Override
ResolvedMethodDeclaration createValue();
// TODO: Implement tests for ResolvedMethodDeclaration
}

View File

@@ -0,0 +1,32 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
public interface ResolvedMethodLikeDeclarationTest
extends ResolvedDeclarationTest, ResolvedTypeParametrizableTest, HasAccessSpecifierTest {
@Override
ResolvedMethodLikeDeclaration createValue();
// TODO: Implement tests for ResolvedMethodLikeDeclaration
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public interface ResolvedParameterDeclarationTest extends ResolvedValueDeclarationTest {
@Override
ResolvedParameterDeclaration createValue();
@Test
default void resolvedParameterShouldBeMarkedAsPattern() {
assertTrue(createValue().isParameter());
}
@Test
default void resolvedParameterShouldHaveAName() {
assertTrue(createValue().hasName());
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public interface ResolvedReferenceTypeDeclarationTest
extends ResolvedTypeDeclarationTest, ResolvedTypeParametrizableTest {
@Override
ResolvedReferenceTypeDeclaration createValue();
@Test
default void isReferenceTypeShouldBeTrue() {
assertTrue(createValue().isReferenceType());
}
@Test
default void getAllFieldsCantBeNull() {
assertNotNull(createValue().getAllFields());
}
@Test
default void getDeclaredMethodsCantBeNull() {
assertNotNull(createValue().getDeclaredMethods());
}
@Test
default void getConstructorsCantBeNull() {
assertNotNull(createValue().getConstructors());
}
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public interface ResolvedTypeDeclarationTest extends ResolvedDeclarationTest {
@Override
ResolvedTypeDeclaration createValue();
@Test
default void whenDeclarationIsAClassTheCallToTheMethodAsClassShouldNotThrow() {
ResolvedTypeDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isClass()) assertDoesNotThrow(resolvedDeclaration::asClass);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asClass);
}
@Test
default void whenDeclarationIsAInterfaceTheCallToTheMethodAsInterfaceShouldNotThrow() {
ResolvedTypeDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isInterface()) assertDoesNotThrow(resolvedDeclaration::asInterface);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asInterface);
}
@Test
default void whenDeclarationIsAEnumTheCallToTheMethodAsEnumShouldNotThrow() {
ResolvedTypeDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isEnum()) assertDoesNotThrow(resolvedDeclaration::asEnum);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asEnum);
}
@Test
default void whenDeclarationIsATypeParameterTheCallToTheMethodAsTypeParameterShouldNotThrow() {
ResolvedTypeDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isTypeParameter()) assertDoesNotThrow(resolvedDeclaration::asTypeParameter);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asTypeParameter);
}
@Test
default void whenDeclarationIsAReferenceTypeTheCallToTheMethodAsReferenceTypeShouldNotThrow() {
ResolvedTypeDeclaration resolvedDeclaration = createValue();
if (resolvedDeclaration.isReferenceType()) assertDoesNotThrow(resolvedDeclaration::asReferenceType);
else assertThrows(UnsupportedOperationException.class, resolvedDeclaration::asReferenceType);
}
@Test
default void qualifiedNameCantBeNull() {
assertNotNull(createValue().getQualifiedName());
}
@Test
default void getIdCantBeNull() {
assertNotNull(createValue().getId());
}
@Test
default void containerTypeCantBeNull() {
assertNotNull(createValue().containerType());
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public interface ResolvedTypeParameterDeclarationTest extends ResolvedTypeDeclarationTest {
@Override
ResolvedTypeParameterDeclaration createValue();
@Test
default void isTypeParameter_shouldBeTrue() {
assertTrue(createValue().isTypeParameter());
}
// TODO: Test ResolvedTypeParameterDeclaration
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
public interface ResolvedTypeParametrizableTest {
ResolvedTypeParametrizable createValue();
@Test
default void getTypeParametersCantBeNull() {
assertNotNull(createValue().getTypeParameters());
}
// TODO: Implement the missing check
@Disabled(value = "JavaParserTypeVariable is not throwing yet.")
@Test
default void findTypeParameterShouldThrowIllegalArgumentExceptionWhenNullIsProvided() {
ResolvedTypeParametrizable typeParametrizable = createValue();
assertThrows(IllegalArgumentException.class, () -> typeParametrizable.findTypeParameter(null));
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
public interface ResolvedTypePatternDeclarationTest extends ResolvedValueDeclarationTest {
@Override
ResolvedTypePatternDeclaration createValue();
@Test
default void resolvedPatternShouldBeMarkedAsPattern() {
assertTrue(createValue().isTypePattern());
}
@Test
default void resolvedPatternShouldHaveAName() {
assertTrue(createValue().hasName());
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.resolution.declarations;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
public interface ResolvedValueDeclarationTest extends ResolvedDeclarationTest {
@Override
ResolvedValueDeclaration createValue();
String getCanonicalNameOfExpectedType(ResolvedValueDeclaration resolvedDeclaration);
@Test
default void getTypeShouldNotReturnNull() {
ResolvedValueDeclaration resolvedDeclaration = createValue();
String expectedTypeQualifiedName = getCanonicalNameOfExpectedType(resolvedDeclaration);
assertNotNull(expectedTypeQualifiedName, resolvedDeclaration.getType().describe());
}
}

View File

@@ -0,0 +1,161 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.resolution.SymbolResolver;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.utils.CodeGenerationUtils;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
public abstract class AbstractSymbolResolutionTest {
@AfterEach
public void reset() {
// reset configuration to not potentially disturb others tests.
// So we have to set specific configuration between each test.
StaticJavaParser.setConfiguration(
new ParserConfiguration().setSymbolResolver(symbolResolver(defaultTypeSolver())));
}
@AfterAll
public static void tearDown() {
// clear internal caches
JavaParserFacade.clearInstances();
}
/**
* An initial attempt at allowing JDK-specific test cases. It is a work-in-progress, and subject to change.
* @deprecated <strong>Note that use of TestJdk should be a last-resort, preferably implementing JDK-agnostic tests.</strong>
*/
@Deprecated
protected enum TestJdk {
JDK8(8),
JDK9(9),
JDK10(10),
JDK11(11),
JDK12(12),
JDK13(13),
JDK14(14),
JDK15(15),
JDK16(16),
JDK17(17),
JDK18(18);
private final Integer major;
/**
* @deprecated <strong>Note that use of TestJdk should be a last-resort, preferably implementing JDK-agnostic tests.</strong>
*/
@Deprecated
TestJdk(Integer major) {
this.major = major;
}
/**
* @deprecated <strong>Note that use of TestJdk should be a last-resort, preferably implementing JDK-agnostic tests.</strong>
*/
@Deprecated
public int getMajorVersion() {
return this.major;
}
/**
* @deprecated <strong>Note that use of TestJdk should be a last-resort, preferably implementing JDK-agnostic tests.</strong>
*/
@Deprecated
public static TestJdk getCurrentHostJdk() {
String javaVersion = System.getProperty("java.version");
// JavaParser explicitly requires a minimum of JDK8 to build.
if ("8".equals(javaVersion) || javaVersion.startsWith("1.8") || javaVersion.startsWith("8")) {
return JDK8;
} else if ("9".equals(javaVersion) || javaVersion.startsWith("9.")) {
return JDK9;
} else if ("10".equals(javaVersion) || javaVersion.startsWith("10.")) {
return JDK10;
} else if ("11".equals(javaVersion) || javaVersion.startsWith("11.")) {
return JDK11;
} else if ("12".equals(javaVersion) || javaVersion.startsWith("12.")) {
return JDK12;
} else if ("13".equals(javaVersion) || javaVersion.startsWith("13.")) {
return JDK13;
} else if ("14".equals(javaVersion) || javaVersion.startsWith("14.")) {
return JDK14;
} else if ("15".equals(javaVersion) || javaVersion.startsWith("15.")) {
return JDK15;
} else if ("16".equals(javaVersion) || javaVersion.startsWith("16.")) {
return JDK16;
} else if ("17".equals(javaVersion) || javaVersion.startsWith("17.")) {
return JDK17;
} else if ("18".equals(javaVersion) || javaVersion.startsWith("18.")) {
return JDK18;
}
throw new IllegalStateException("Unable to determine the current version of java running");
}
/**
* @deprecated <strong>Note that use of TestJdk should be a last-resort, preferably implementing JDK-agnostic tests.</strong>
*/
@Deprecated
@Override
public String toString() {
return "TestJdk{" + "System.getProperty(\"java.version\")="
+ System.getProperty("java.version") + ",major="
+ major + '}';
}
}
protected static Path adaptPath(Path path) {
if (Files.exists(path)) {
return path.toAbsolutePath();
}
Path underSymbolSolver = CodeGenerationUtils.mavenModuleRoot(AbstractSymbolResolutionTest.class)
.resolve("javaparser-symbol-solver-testing")
.resolve(path);
if (Files.exists(underSymbolSolver)) {
return underSymbolSolver;
} else {
throw new IllegalArgumentException("I cannot adapt the path " + path);
}
}
protected static Path adaptPath(String path) {
return adaptPath(Paths.get(path));
}
protected SymbolResolver symbolResolver(TypeSolver typeSolver) {
return new JavaSymbolSolver(typeSolver);
}
protected TypeSolver defaultTypeSolver() {
return new ReflectionTypeSolver();
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.declarations.ResolvedDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.google.common.collect.ImmutableSet;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
class FindingAllFieldsTest extends AbstractResolutionTest {
@Test
void findAllInheritedFields() {
CompilationUnit cu = parseSample("AClassWithFields");
ClassOrInterfaceDeclaration classC = Navigator.demandClass(cu, "C");
ResolvedReferenceTypeDeclaration typeDeclaration =
JavaParserFacade.get(new ReflectionTypeSolver()).getTypeDeclaration(classC);
assertEquals(3, typeDeclaration.getAllFields().size());
assertEquals(
ImmutableSet.of("a", "b", "c"),
typeDeclaration.getAllFields().stream()
.map(ResolvedDeclaration::getName)
.collect(Collectors.toSet()));
}
@Test
void findAllInheritedFieldsAndGenerics() {
CompilationUnit cu = parseSample("AClassWithFieldsAndGenerics");
ClassOrInterfaceDeclaration classC = Navigator.demandClass(cu, "C");
ResolvedReferenceTypeDeclaration typeDeclaration =
JavaParserFacade.get(new ReflectionTypeSolver()).getTypeDeclaration(classC);
assertEquals(3, typeDeclaration.getAllFields().size());
assertEquals(
ImmutableSet.of("a", "b", "c"),
typeDeclaration.getAllFields().stream()
.map(ResolvedDeclaration::getName)
.collect(Collectors.toSet()));
assertEquals(
"java.util.List<java.lang.String>",
typeDeclaration.getField("b").getType().describe());
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.StaticJavaParser.parse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class Issue113Test extends AbstractSymbolResolutionTest {
private TypeSolver typeSolver;
@BeforeEach
void setup() {
typeSolver = new CombinedTypeSolver(
new ReflectionTypeSolver(),
new JavaParserTypeSolver(adaptPath("src/test/resources/issue113"), new LeanParserConfiguration()));
}
@Test
void issue113providedCodeDoesNotCrash() throws IOException {
Path pathToSourceFile = adaptPath("src/test/resources/issue113/com/foo/Widget.java");
CompilationUnit cu = parse(pathToSourceFile);
JavaParserFacade parserFacade = JavaParserFacade.get(typeSolver);
MethodDeclaration methodDeclaration = cu.findAll(MethodDeclaration.class).stream()
.filter(node -> node.getName().getIdentifier().equals("doSomething"))
.findAny()
.orElse(null);
methodDeclaration.findAll(MethodCallExpr.class).forEach(parserFacade::solve);
}
@Test
void issue113superClassIsResolvedCorrectly() throws IOException {
Path pathToSourceFile = adaptPath("src/test/resources/issue113/com/foo/Widget.java");
CompilationUnit cu = parse(pathToSourceFile);
JavaParserClassDeclaration jssExtendedWidget =
new JavaParserClassDeclaration(cu.getClassByName("Widget").get(), typeSolver);
ResolvedReferenceType superClass = jssExtendedWidget.getSuperClass().get();
assertEquals("com.foo.base.Widget", superClass.getQualifiedName());
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
class Issue116Test extends AbstractResolutionTest {
@Test
void arrayTypeIsNotPartOfTheTree() {
CompilationUnit cu = parseSample("Issue116");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "foo");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
com.github.javaparser.ast.type.Type typeNode =
methodDeclaration.getParameters().get(0).getType();
ResolvedType type = javaParserFacade.convert(typeNode, typeNode);
assertEquals("java.lang.String[]", type.describe());
ExpressionStmt expressionStmt = (ExpressionStmt)
methodDeclaration.getBody().get().getStatements().get(0);
Expression argRef = expressionStmt.getExpression();
assertEquals("java.lang.String[]", javaParserFacade.getType(argRef).describe());
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class Issue128Test extends AbstractResolutionTest {
private TypeSolver typeSolver;
@BeforeEach
void setup() throws IOException {
Path srcDir = adaptPath("src/test/resources/issue128");
typeSolver = new CombinedTypeSolver(
new ReflectionTypeSolver(), new JavaParserTypeSolver(srcDir, new LeanParserConfiguration()));
}
@Test
void verifyJavaTestClassIsSolved() {
typeSolver.solveType("foo.JavaTest");
}
@Test
void loopOnStaticallyImportedType() {
CompilationUnit cu = parseSampleWithStandardExtension("issue128/foo/Issue128");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
ExpressionStmt expressionStmt = (ExpressionStmt)
clazz.getMethodsByName("test").get(0).getBody().get().getStatement(0);
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
assertEquals(false, javaParserFacade.solve(methodCallExpr).isSolved());
}
}

View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.*;
import com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.resolution.model.SymbolReference;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* @author Dominik Hardtke
* @since 02/02/2018
*/
class Issue1364Test extends AbstractResolutionTest {
private JavaParser javaParser;
@BeforeEach
void setup() {
ClassOrInterfaceDeclaration fakeObject = new ClassOrInterfaceDeclaration();
fakeObject.setName(new SimpleName("java.lang.Object"));
TypeSolver typeSolver = new TypeSolver() {
@Override
public TypeSolver getParent() {
return null;
}
@Override
public void setParent(TypeSolver parent) {}
@Override
public SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name) {
if ("java.lang.Object".equals(name)) {
// custom handling
return SymbolReference.solved(new JavaParserClassDeclaration(fakeObject, this));
}
return SymbolReference.unsolved();
}
};
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
javaParser = new JavaParser(config);
}
@Test
void resolveSubClassOfObject() {
assertTimeoutPreemptively(Duration.ofMillis(1000L), () -> {
String code = String.join(
System.lineSeparator(),
"package graph;",
"public class Vertex {",
" public static void main(String[] args) {",
" System.out.println();",
" }",
"}");
ParseResult<CompilationUnit> parseResult =
javaParser.parse(ParseStart.COMPILATION_UNIT, Providers.provider(code));
assertTrue(parseResult.isSuccessful());
assertTrue(parseResult.getResult().isPresent());
List<MethodCallExpr> methodCallExprs = parseResult.getResult().get().findAll(MethodCallExpr.class);
assertEquals(1, methodCallExprs.size());
try {
methodCallExprs.get(0).calculateResolvedType();
fail("An UnsolvedSymbolException should be thrown");
} catch (UnsolvedSymbolException ignored) {
}
});
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
/**
* Solving generic types that are of type java.lang.Object
* @see <a href="https://github.com/javaparser/javaparser/issues/1370">https://github.com/javaparser/javaparser/issues/1370</a>
*/
public class Issue1370Test {
@Test
public void test() {
final String source = String.join(
System.lineSeparator(),
"package graph;",
"class Vertex<Data> {",
" private final Data data;",
" public Vertex(Data data) { this.data = data; }",
" public Data getData() { return this.data; }",
"}",
"",
"public class Application {",
" public static void main(String[] args) {",
" System.out.println(new Vertex<>(42).getData().equals(42));",
" }",
"}");
final JavaParserFacade facade = JavaParserFacade.get(new ReflectionTypeSolver(false));
StaticJavaParser.parse(source)
.accept(
new VoidVisitorAdapter<Void>() {
@Override
public void visit(final MethodCallExpr n, final Void arg) {
super.visit(n, arg);
try {
System.out.printf("Node: %s, solved Type: %s%n", n, facade.solve(n));
} catch (RuntimeException e) {
e.printStackTrace();
}
}
},
null);
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.nio.file.Path;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class Issue144Test extends AbstractResolutionTest {
private TypeSolver typeSolver;
@BeforeEach
void setup() {
Path srcDir = adaptPath("src/test/resources/issue144");
typeSolver = new JavaParserTypeSolver(srcDir, new LeanParserConfiguration());
}
@Test
void issue144() {
CompilationUnit cu = parseSampleWithStandardExtension("issue144/HelloWorld");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "HelloWorld");
ExpressionStmt expressionStmt = (ExpressionStmt)
clazz.getMethodsByName("main").get(0).getBody().get().getStatement(0);
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
Expression firstParameter = methodCallExpr.getArgument(0);
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
assertEquals(true, javaParserFacade.solve(firstParameter).isSolved());
assertEquals(
true,
javaParserFacade
.solve(firstParameter)
.getCorrespondingDeclaration()
.isField());
assertEquals(
"hw",
javaParserFacade
.solve(firstParameter)
.getCorrespondingDeclaration()
.getName());
}
@Test
void issue144WithReflectionTypeSolver() {
CompilationUnit cu = parseSampleWithStandardExtension("issue144/HelloWorld");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "HelloWorld");
ExpressionStmt expressionStmt = (ExpressionStmt)
clazz.getMethodsByName("main").get(0).getBody().get().getStatement(0);
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
Expression firstParameter = methodCallExpr.getArgument(0);
JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver(true));
assertEquals(true, javaParserFacade.solve(firstParameter).isSolved());
}
@Test
void issue144WithCombinedTypeSolver() {
CompilationUnit cu = parseSampleWithStandardExtension("issue144/HelloWorld");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "HelloWorld");
ExpressionStmt expressionStmt = (ExpressionStmt)
clazz.getMethodsByName("main").get(0).getBody().get().getStatement(0);
MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
Expression firstParameter = methodCallExpr.getArgument(0);
JavaParserFacade javaParserFacade =
JavaParserFacade.get(new CombinedTypeSolver(typeSolver, new ReflectionTypeSolver(true)));
assertEquals(true, javaParserFacade.solve(firstParameter).isSolved());
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
class Issue1456Test extends AbstractResolutionTest {
@Test
void fieldAccessIssue() throws IOException {
Path rootSourceDir = adaptPath("src/test/resources/issue1456");
Path pathToSourceFile = adaptPath(rootSourceDir.toString() + "/bar/A.java");
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new JavaParserTypeSolver(rootSourceDir.toFile())));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(pathToSourceFile);
ClassOrInterfaceDeclaration cid =
cu.findFirst(ClassOrInterfaceDeclaration.class).get();
ResolvedTypeDeclaration rtd = cid.resolve();
List<ResolvedReferenceType> ancestors = rtd.asClass().getAncestors();
assertEquals("foo.A", ancestors.get(0).describe());
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import java.util.Optional;
import org.junit.jupiter.api.Test;
public class Issue1479Test extends AbstractSymbolResolutionTest {
@Test
public void test() throws IOException {
CombinedTypeSolver typeSolver = new CombinedTypeSolver(
new ReflectionTypeSolver(), new JavaParserTypeSolver(adaptPath("src/test/resources/issue1479")));
JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
StaticJavaParser.getConfiguration().setSymbolResolver(symbolSolver);
String src = "public class Foo {\n" + " public void m() {\n"
+ " doSomething(B.AFIELD);\n"
+ " }\n"
+ " public void doSomething(String a) {\n"
+ " }\n"
+ "}\n";
CompilationUnit cu = StaticJavaParser.parse(src);
FieldAccessExpr fae = cu.findFirst(FieldAccessExpr.class).get();
assertTrue(fae.calculateResolvedType().describe().equals("java.lang.String"));
ResolvedFieldDeclaration value = fae.resolve().asField();
assertTrue(value.getName().equals("AFIELD"));
Optional<FieldDeclaration> fd = value.toAst(FieldDeclaration.class);
assertEquals(
"a",
fd.get()
.getVariable(0)
.getInitializer()
.get()
.asStringLiteralExpr()
.getValue());
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
class Issue1480Test extends AbstractResolutionTest {
@Test()
void test() throws IOException {
Path rootSourceDir = adaptPath("src/test/resources/issue1480");
Path pathToSourceFile = adaptPath(rootSourceDir.toString() + "/B.java");
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new JavaParserTypeSolver(rootSourceDir.toFile())));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(pathToSourceFile);
MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
assertThrows(UnsolvedSymbolException.class, () -> {
mce.resolve().getQualifiedName();
});
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseStart;
import com.github.javaparser.StreamProvider;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
class Issue1485Test extends AbstractSymbolResolutionTest {
@Test
void issue1485withoutSpecifyingJARs() throws IOException {
Path dir = adaptPath("src/test/resources/issue1485");
Path file = adaptPath("src/test/resources/issue1485/Complex.java");
CombinedTypeSolver typeSolver = new CombinedTypeSolver();
typeSolver.add(new ReflectionTypeSolver());
typeSolver.add(new JavaParserTypeSolver(dir, new LeanParserConfiguration()));
JavaParser javaParser = new JavaParser();
javaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
CompilationUnit unit = javaParser
.parse(
ParseStart.COMPILATION_UNIT,
new StreamProvider(Files.newInputStream(file), StandardCharsets.UTF_8.name()))
.getResult()
.get();
MethodCallExpr methodCallExpr = unit.findFirst(
MethodCallExpr.class, m -> m.getName().getIdentifier().equals("println"))
.get();
ResolvedMethodDeclaration resolvedMethodDeclaration = methodCallExpr.resolve();
assertEquals(
"java.io.PrintStream.println(java.lang.String)", resolvedMethodDeclaration.getQualifiedSignature());
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.StaticJavaParser.parse;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.File;
import java.io.FileNotFoundException;
import org.junit.jupiter.api.Test;
class Issue1491Test {
@Test
void verifyIssue1491SolvingClassInSameFile() throws FileNotFoundException {
File aJava = new File("src/test/resources/issue1491/A.java");
if (!aJava.exists()) {
throw new IllegalStateException();
}
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(aJava.getAbsoluteFile().getParentFile()));
ParserConfiguration parserConfiguration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
StaticJavaParser.setConfiguration(parserConfiguration);
CompilationUnit cu = parse(aJava);
cu.accept(
new VoidVisitorAdapter() {
public void visit(NameExpr n, Object arg) {
ResolvedType type = JavaParserFacade.get(localCts).getType(n);
super.visit(n, arg);
}
},
null);
}
@Test
void verifyIssue1491ResolvingStaticMethodCalls() throws FileNotFoundException {
File aJava = new File("src/test/resources/issue1491/A.java");
if (!aJava.exists()) {
throw new IllegalStateException();
}
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(aJava.getAbsoluteFile().getParentFile()));
ParserConfiguration parserConfiguration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
StaticJavaParser.setConfiguration(parserConfiguration);
CompilationUnit cu = parse(aJava);
cu.accept(
new VoidVisitorAdapter() {
public void visit(MethodCallExpr n, Object arg) {
ResolvedMethodDeclaration decl =
JavaParserFacade.get(localCts).solve(n).getCorrespondingDeclaration();
super.visit(n, arg);
}
},
null);
}
@Test
void verifyIssue1491Combined() throws FileNotFoundException {
File aJava = new File("src/test/resources/issue1491/A.java");
if (!aJava.exists()) {
throw new IllegalStateException();
}
CombinedTypeSolver localCts = new CombinedTypeSolver();
localCts.add(new ReflectionTypeSolver());
localCts.add(new JavaParserTypeSolver(aJava.getAbsoluteFile().getParentFile()));
ParserConfiguration parserConfiguration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(localCts));
StaticJavaParser.setConfiguration(parserConfiguration);
CompilationUnit cu = parse(aJava);
cu.accept(
new VoidVisitorAdapter<Void>() {
public void visit(NameExpr n, Void arg) {
try {
ResolvedType type = JavaParserFacade.get(localCts).getType(n);
} catch (UnsolvedSymbolException e) {
throw new RuntimeException("Unable to solve name expr at " + n.getRange(), e);
}
super.visit(n, arg);
}
public void visit(MethodCallExpr n, Void arg) {
ResolvedMethodDeclaration decl =
JavaParserFacade.get(localCts).solve(n).getCorrespondingDeclaration();
super.visit(n, arg);
}
},
null);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.symbolsolver.AbstractSymbolResolutionTest.adaptPath;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.FileNotFoundException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
/**
* IndexOutOfBoundsException when attempting to resolve super() #1511
*
* @see <a href="https://github.com/javaparser/javaparser/issues/1511">https://github.com/javaparser/javaparser/issues/1511</a>
*/
public class Issue1511Test {
@Test
public void test() throws FileNotFoundException {
Path dir = adaptPath("src/test/resources/issue1511");
Path file = adaptPath("src/test/resources/issue1511/A.java");
// configure symbol solver
CombinedTypeSolver typeSolver = new CombinedTypeSolver();
typeSolver.add(new ReflectionTypeSolver());
typeSolver.add(new JavaParserTypeSolver(dir.toFile()));
JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
StaticJavaParser.getConfiguration().setSymbolResolver(symbolSolver);
// get compilation unit & extract explicit constructor invocation statement
CompilationUnit cu = StaticJavaParser.parse(file.toFile());
ExplicitConstructorInvocationStmt ecis = cu.getPrimaryType()
.orElseThrow(IllegalStateException::new)
.asClassOrInterfaceDeclaration()
.getMember(0)
.asConstructorDeclaration()
.getBody()
.getStatement(0)
.asExplicitConstructorInvocationStmt();
// attempt to resolve explicit constructor invocation statement
ResolvedConstructorDeclaration rcd = ecis.resolve(); // .resolveInvokedConstructor(); // <-- exception occurs
}
@Test
public void exploratory_resolveAndGetSuperClass() {
ParserConfiguration configuration = new ParserConfiguration();
configuration.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
JavaParser javaParser = new JavaParser(configuration);
CompilationUnit foo = javaParser.parse("class A {}").getResult().orElseThrow(IllegalStateException::new);
ResolvedReferenceType a = foo.getClassByName("A")
.orElseThrow(IllegalStateException::new)
.resolve()
.asClass()
.getSuperClass()
.get();
assertEquals("java.lang.Object", a.getQualifiedName());
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
class Issue1518Test extends AbstractResolutionTest {
@Test()
void test() throws IOException {
Path rootSourceDir = adaptPath("src/test/resources/issue1518");
String src = "public class App {\n" + " public static void main(String[] args) {\n"
+ " Test1.Test2 test2 = new Test1.Test2();\n"
+ " Test1.Test3 test3 = new Test1.Test3();\n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new JavaParserTypeSolver(rootSourceDir.toFile())));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(src);
List<ObjectCreationExpr> oce = cu.findAll(ObjectCreationExpr.class);
assertEquals("Test1.Test2", oce.get(0).calculateResolvedType().describe());
assertEquals("Test1.Test3", oce.get(1).calculateResolvedType().describe());
}
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
/**
* CompilationUnitContext.solveType(String name, TypeSolver typeSolver) checks package and imports in wrong order.
* @see <a href="https://github.com/javaparser/javaparser/issues/1526">https://github.com/javaparser/javaparser/issues/1526</a>
*/
public class Issue1526Test extends AbstractSymbolResolutionTest {
private final Path testRoot = adaptPath("src/test/resources/issue1526");
private final Path rootCompiles = testRoot.resolve("compiles");
private final Path rootErrors = testRoot.resolve("errors");
@Test
public void givenImport_whenCompiles_expectPass() throws IOException {
Path root = rootCompiles;
Path file = rootCompiles.resolve("a/b/c/ExampleClass.java");
assertDoesNotThrow(() -> {
doTest(root, file);
});
}
@Test
public void givenImportCommentOut_whenCompiles_expectFail() throws IOException {
Path root = rootErrors;
Path file = rootErrors.resolve("a/b/c/ExampleClass.java");
assertThrows(UnsolvedSymbolException.class, () -> {
doTest(root, file);
});
}
private void doTest(Path root, Path file) throws IOException {
CombinedTypeSolver typeSolver = new CombinedTypeSolver();
typeSolver.add(new ReflectionTypeSolver());
typeSolver.add(new JavaParserTypeSolver(root, new LeanParserConfiguration()));
JavaParser javaParser = new JavaParser();
javaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
ParseResult<CompilationUnit> cu = javaParser.parse(file);
assumeTrue(cu.isSuccessful(), "the file should compile -- errors are expected when attempting to resolve.");
cu.getResult().get().findAll(MethodCallExpr.class).forEach(methodCallExpr -> {
methodCallExpr.resolve();
methodCallExpr.calculateResolvedType();
});
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
class Issue156Test extends AbstractResolutionTest {
@Test
void testFieldAccessThroughClassAndThis() {
CompilationUnit cu = parseSample("Issue156");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Issue156");
List<MethodCallExpr> methods =
clazz.getChildNodes().get(3).getChildNodes().get(1).findAll(MethodCallExpr.class);
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
assertEquals("char", javaParserFacade.getType(methods.get(0)).describe());
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.comments.Comment;
import java.io.File;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue1574Test {
private static final String LINE_FILE = "src/test/resources/issue1574/Comment.java";
private static final String BLOCK_FILE = "src/test/resources/issue1574/BlockComment.java";
private static final String ORPHAN_FILE = "src/test/resources/issue1574/ClassWithOrphanComments.java";
@Test
void removeAllCommentsBeforePackageLine() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(LINE_FILE));
for (Comment child : cu.getComments()) {
child.remove();
}
assertEquals(0, cu.getComments().size());
assertFalse(cu.getComment().isPresent());
}
@Test
void removeAllCommentsBeforePackageBlock() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(BLOCK_FILE));
for (Comment child : cu.getComments()) {
child.remove();
}
assertEquals(0, cu.getComments().size());
assertFalse(cu.getComment().isPresent());
}
@Test
void getAllContainedCommentBeforePackageDeclarationLine() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(LINE_FILE));
List<Comment> comments = cu.getAllContainedComments();
assertEquals(2, comments.size());
}
@Test
void getAllContainedCommentBeforePackageDeclarationBlock() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(BLOCK_FILE));
List<Comment> comments = cu.getAllContainedComments();
assertEquals(2, comments.size());
}
@Test
void getAllCommentBeforePackageDeclarationOrphan() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(ORPHAN_FILE));
List<Comment> comments = cu.getAllContainedComments();
assertEquals(6, comments.size());
}
@Test
void getOrphanComments() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(LINE_FILE));
List<Comment> comments = cu.getOrphanComments();
// The 2 first should be orphan comment while the third will be associated to the package
assertEquals(1, comments.size());
}
@Test
void getOrphanCommentsBlock() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(BLOCK_FILE));
List<Comment> comments = cu.getOrphanComments();
// The 2 first should be orphan comment while the third will be associated to the package
assertEquals(1, comments.size());
}
@Test
void getAllCommentBeforePackageDeclarationLine() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(LINE_FILE));
List<Comment> comments = cu.getComments();
assertEquals(3, comments.size());
}
@Test
void getAllCommentBeforePackageDeclarationBlock() throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new File(BLOCK_FILE));
List<Comment> comments = cu.getComments();
assertEquals(3, comments.size());
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
class Issue1599Test extends AbstractResolutionTest {
@Test()
void test() throws IOException {
Path rootSourceDir = adaptPath("src/test/resources/issue1599");
String src = "public class Foo {\n" + " public void m() {\n"
+ " A myVar = new A() {\n"
+ " public void bar() {}\n"
+ " public void bar2() {}\n"
+ " };\n"
+ " myVar.bar();\n"
+ " myVar.bar2();\n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new JavaParserTypeSolver(rootSourceDir.toFile())));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(src);
List<MethodCallExpr> mce = cu.findAll(MethodCallExpr.class);
assertEquals("void", mce.get(0).calculateResolvedType().describe());
assertThrows(RuntimeException.class, () -> mce.get(1).calculateResolvedType());
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class Issue1668Test {
private JavaParser javaParser;
@BeforeEach
void setUp() {
TypeSolver typeSolver = new ReflectionTypeSolver();
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
javaParser = new JavaParser(config);
}
@Test
void testResolveArrayDeclaration() {
String code = String.join(
System.lineSeparator(),
"public class X {",
" public static void main(String[] args) {",
" String s = \"a,b,c,d,e\";",
" String[] stringArray = s.split(',');",
" }",
"}");
ParseResult<CompilationUnit> parseResult =
javaParser.parse(ParseStart.COMPILATION_UNIT, Providers.provider(code));
assertTrue(parseResult.isSuccessful());
assertTrue(parseResult.getResult().isPresent());
CompilationUnit compilationUnit = parseResult.getResult().get();
VariableDeclarator variableDeclarator = compilationUnit
.findFirst(VariableDeclarator.class, v -> v.getNameAsString().equals("stringArray"))
.get();
VariableDeclarationExpr variableDeclarationExpr =
(VariableDeclarationExpr) variableDeclarator.getParentNode().get();
ResolvedType resolvedType = variableDeclarationExpr.calculateResolvedType();
assertEquals("java.lang.String[]", resolvedType.describe());
ResolvedValueDeclaration resolve = variableDeclarator.resolve();
assertEquals("java.lang.String[]", resolve.getType().describe());
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.NormalAnnotationExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import org.junit.jupiter.api.Test;
class Issue1713Test extends AbstractResolutionTest {
@Test()
void test() throws IOException {
String src = "class X {\n" + " @SuppressWarnings(value = \"unchecked\")\n" + " void x() {}\n" + "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(src);
NormalAnnotationExpr nae = cu.findFirst(NormalAnnotationExpr.class).get();
assertEquals("java.lang.SuppressWarnings", nae.resolve().getQualifiedName());
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue1726Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
TypeSolver typeSolver = new ReflectionTypeSolver(false);
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);
String s = "import static java.util.concurrent.TimeUnit.SECONDS; \n" + "public class A {\n"
+ " public static void main( String[] args )\n"
+ " {\n"
+ " System.out.println(SECONDS);\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
assertEquals("void", (mce.calculateResolvedType().describe()));
assertEquals(
"java.util.concurrent.TimeUnit",
(mce.getArgument(0).calculateResolvedType().describe()));
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import org.junit.jupiter.api.Test;
public class Issue1757Test extends AbstractResolutionTest {
@Test()
void test() throws IOException {
String src = "import java.util.Comparator;\n" + "public class A {\n"
+ " public void m() {\n"
+ " Comparator<String> c = new Comparator<String>() {\n"
+ " public int compare(String o1, String o2) {\n"
+ " return 0;\n"
+ " }\n"
+ " };\n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver()));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(src);
ObjectCreationExpr oce = cu.findFirst(ObjectCreationExpr.class).get();
assertEquals(
"java.util.Comparator<java.lang.String>",
oce.calculateResolvedType().describe());
assertTrue(oce.resolve().getQualifiedName().startsWith("A.Anonymous"));
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
public class Issue1769Test extends AbstractResolutionTest {
@Test()
void testExtendsNestedclass() throws IOException {
Path rootSourceDir = adaptPath("src/test/resources/issue1769");
String src = "import foo.OtherClass;\n" + "public class MyClass extends OtherClass.InnerClass {\n" + "}\n";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new JavaParserTypeSolver(rootSourceDir.toFile())));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(src);
ClassOrInterfaceDeclaration cid =
cu.findFirst(ClassOrInterfaceDeclaration.class).get();
cid.getExtendedTypes().forEach(t -> {
assertEquals("foo.OtherClass.InnerClass", t.resolve().describe());
});
}
@Test()
void testInstanciateNestedClass() throws IOException {
Path rootSourceDir = adaptPath("src/test/resources/issue1769");
String src = "import foo.OtherClass;\n" + "public class MyClass{\n"
+ " public InnerClass myTest() {\n"
+ " return new OtherClass.InnerClass();\n"
+ " }\n"
+ "}\n";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new JavaParserTypeSolver(rootSourceDir.toFile())));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(src);
ObjectCreationExpr oce = cu.findFirst(ObjectCreationExpr.class).get();
assertEquals(
"foo.OtherClass.InnerClass",
oce.calculateResolvedType().asReferenceType().getQualifiedName());
// The qualified name of the method composed by the qualfied name of the declaring type
// followed by a dot and the name of the method.
assertEquals("foo.OtherClass.InnerClass.InnerClass", oce.resolve().getQualifiedName());
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue1774Test extends AbstractResolutionTest {
@Test
public void test() {
StaticJavaParser.setConfiguration(
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false))));
String str = "public class A { "
+ " String s1 = false + \"str\";"
+ " String s2 = 'a' + \"str\";"
+ " String s3 = 1 + \"foo\";"
+ " float f = 3 % 2.71f;"
+ " double d = 3 % 2.0;"
+ " Integer i1 = 'G' & 6;"
+ " long l1 = 'z' & 1L;"
+ " long l2 = 0x01 & 2L;"
+ " Integer i2 = 'G' & 6;"
+ " long l3 = 'z' & 1L;"
+ " long l4 = 0x01 & 2L;"
+ " int i10 = 'B' << 1;"
+ " byte b = 8;"
+ " int i11 = b >> 2;"
+ " short s = 0x0f;"
+ " int i12 = 'B' << 1;"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(str);
List<BinaryExpr> exprs = cu.findAll(BinaryExpr.class);
assertEquals("java.lang.String", exprs.get(0).calculateResolvedType().describe());
assertEquals("java.lang.String", exprs.get(1).calculateResolvedType().describe());
assertEquals("java.lang.String", exprs.get(2).calculateResolvedType().describe());
assertEquals("float", exprs.get(3).calculateResolvedType().describe());
assertEquals("double", exprs.get(4).calculateResolvedType().describe());
assertEquals("int", exprs.get(5).calculateResolvedType().describe());
assertEquals("long", exprs.get(6).calculateResolvedType().describe());
assertEquals("long", exprs.get(7).calculateResolvedType().describe());
assertEquals("int", exprs.get(8).calculateResolvedType().describe());
assertEquals("long", exprs.get(9).calculateResolvedType().describe());
assertEquals("long", exprs.get(10).calculateResolvedType().describe());
// unary primitve promotion
assertEquals("int", exprs.get(11).calculateResolvedType().describe());
assertEquals("int", exprs.get(12).calculateResolvedType().describe());
assertEquals("int", exprs.get(13).calculateResolvedType().describe());
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.*;
import com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.resolution.model.SymbolReference;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* @author Dominik Hardtke
* @since 01/09/2018
*/
class Issue1814Test extends AbstractResolutionTest {
private JavaParser javaParser;
@BeforeEach
void setup() {
final CompilationUnit compilationUnit = new CompilationUnit();
compilationUnit.setPackageDeclaration("java.lang");
// construct a fake java.lang.Object class with only one method (java.lang.Object#equals(java.lang.Object)
final ClassOrInterfaceDeclaration clazz = compilationUnit.addClass("Object", Modifier.Keyword.PUBLIC);
final MethodDeclaration equals = clazz.addMethod("equals", Modifier.Keyword.PUBLIC);
equals.addParameter("Object", "obj");
final BlockStmt body = new BlockStmt();
body.addStatement("return this == obj;");
equals.setBody(body);
TypeSolver typeSolver = new TypeSolver() {
@Override
public TypeSolver getParent() {
return null;
}
@Override
public void setParent(TypeSolver parent) {}
@Override
public SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name) {
if ("java.lang.Object".equals(name)) {
// custom handling
return SymbolReference.solved(new JavaParserClassDeclaration(clazz, this));
}
return SymbolReference.unsolved();
}
};
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
javaParser = new JavaParser(config);
}
@Test
void getAllMethodsVisibleToInheritors() {
assertTimeoutPreemptively(Duration.ofMillis(1000L), () -> {
String code = String.join(
System.lineSeparator(), "public class AbstractExercise extends java.lang.Object {", "}");
ParseResult<CompilationUnit> parseResult =
javaParser.parse(ParseStart.COMPILATION_UNIT, Providers.provider(code));
assertTrue(parseResult.isSuccessful());
assertTrue(parseResult.getResult().isPresent());
List<ClassOrInterfaceType> referenceTypes =
parseResult.getResult().get().findAll(ClassOrInterfaceType.class);
assertTrue(referenceTypes.size() > 0);
final List<ResolvedMethodDeclaration> methods =
referenceTypes.get(0).resolve().asReferenceType().getAllMethodsVisibleToInheritors();
assertEquals(1, methods.size());
});
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
public class Issue1817Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
Path testResources = adaptPath("src/test/resources/issue1817");
CombinedTypeSolver typeSolver = new CombinedTypeSolver();
typeSolver.add(new ReflectionTypeSolver());
typeSolver.add(new JavaParserTypeSolver(testResources));
StaticJavaParser.setConfiguration(
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver)));
String s = "interface A extends X.A {\n" + " default void foo() {\n"
+ " X.A xa = null;\n"
+ " xa.bar();\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
assertEquals("X.A.bar", mce.resolve().getQualifiedName());
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue1827Test extends AbstractResolutionTest {
@Test
public void solveParametrizedParametersConstructor() {
String src = "public class ParametrizedParametersConstructor {\n"
+ " public void foo() {\n"
+ " EClass arg = new EClass();\n"
+ " ParametrizedClass<String> pc = new ParametrizedClass<>(arg, arg);\n"
+ " }\n"
+ "\n"
+ " class EClass implements BaseType<String> {\n"
+ " }\n"
+ "}\n"
+ "\n"
+ "class ParametrizedClass<T> {\n"
+ " public ParametrizedClass(BaseType<T> arg1, BaseType<T> arg2) {\n"
+ " }\n"
+ "}\n"
+ "\n"
+ "interface BaseType<T> {\n"
+ "}";
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaSymbolSolver symbolSolver = new JavaSymbolSolver(typeSolver);
StaticJavaParser.getConfiguration().setSymbolResolver(symbolSolver);
CompilationUnit cu = StaticJavaParser.parse(src);
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ParametrizedParametersConstructor");
ObjectCreationExpr oce = clazz.findAll(ObjectCreationExpr.class).get(1); // new ParametrizedClass<>(arg, arg)
assertDoesNotThrow(() -> oce.resolve());
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.StaticJavaParser.parse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
class Issue185Test extends AbstractResolutionTest {
@Test
void testIssue() throws IOException {
Path src = adaptPath("src/test/resources/recursion-issue");
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new JavaParserTypeSolver(src, new LeanParserConfiguration()));
combinedTypeSolver.add(new ReflectionTypeSolver());
CompilationUnit agendaCu = parse(adaptPath("src/test/resources/recursion-issue/Usage.java"));
MethodCallExpr foo = Navigator.findMethodCall(agendaCu, "foo").get();
assertNotNull(foo);
JavaParserFacade.get(combinedTypeSolver).getType(foo);
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue1868Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
Path testResources = adaptPath("src/test/resources/issue1868");
CombinedTypeSolver typeSolver = new CombinedTypeSolver();
typeSolver.add(new ReflectionTypeSolver());
typeSolver.add(new JavaParserTypeSolver(testResources));
StaticJavaParser.setConfiguration(
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver)));
String s = "class A {\n" + " public void foo() {\n"
+ " toArray(new String[0]);\n"
+ " }\n"
+ " public void bar() {\n"
+ " B b = null;\n"
+ " b.toArray(new String[0]);\n"
+ " }\n"
+ " public <T> T[] toArray(T[] tArray) {\n"
+ " // ...\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
List<MethodCallExpr> mces = cu.findAll(MethodCallExpr.class);
assertEquals(
"toArray(new String[0]) resolved to A.toArray",
String.format(
"%s resolved to %s", mces.get(0), mces.get(0).resolve().getQualifiedName()));
assertEquals(
"b.toArray(new String[0]) resolved to B.toArray",
String.format(
"%s resolved to %s", mces.get(1), mces.get(1).resolve().getQualifiedName()));
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.LambdaExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
class Issue186Test extends AbstractResolutionTest {
@Test
void lambdaFlatMapIssue() {
CompilationUnit cu = parseSample("Issue186");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "foo");
MethodCallExpr methodCallExpr =
Navigator.findMethodCall(methodDeclaration, "flatMap").get();
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
assertEquals(
"java.util.stream.Stream<java.lang.String>",
javaParserFacade.getType(methodCallExpr).describe());
}
@Test
void lambdaPrimitivesIssue() {
CompilationUnit cu = parseSample("Issue186");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "bar");
List<LambdaExpr> lambdas = methodDeclaration.findAll(LambdaExpr.class);
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
assertEquals(
"java.util.function.Predicate<? super java.lang.String>",
javaParserFacade.getType(lambdas.get(0)).describe());
assertEquals(
"java.util.function.Function<? super java.lang.String, ? extends java.lang.Integer>",
javaParserFacade.getType(lambdas.get(1)).describe());
assertEquals(
"java.util.function.Predicate<? super java.lang.Integer>",
javaParserFacade.getType(lambdas.get(2)).describe());
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
class Issue18Test extends AbstractResolutionTest {
@Test
void typeDeclarationSuperClassImplicitlyIncludeObject() {
CompilationUnit cu = parseSample("Issue18");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "bar");
ExpressionStmt expr = (ExpressionStmt)
methodDeclaration.getBody().get().getStatements().get(1);
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ResolvedType type = javaParserFacade.getType(expr.getExpression());
assertEquals("java.lang.Object", type.describe());
}
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class Issue1945Test extends AbstractResolutionTest {
private static final String code =
"import issue1945.implementations.Sheep;\n" + "import issue1945.interfaces.HairType;\n"
+ "import issue1945.interfaces.HairTypeRenderer;\n"
+ "import issue1945.interfaces.HairyAnimal;\n"
+ "\n"
+ "public class MainIssue1945 {\n"
+ " \n"
+ " private final HairyAnimal sheep = new Sheep();\n"
+ " \n"
+ " public void chokes3() {\n"
+ " HairType<?> hairType = sheep.getHairType();\n"
+ " hairType.getRenderer().renderHair(sheep.getHairType(), sheep);\n"
+ " hairType.getRenderer();\n"
+ " }\n"
+ " \n"
+ " public void chokes() {\n"
+ " sheep.getHairType().getRenderer().renderHair(sheep.getHairType(), sheep);\n"
+ " }\n"
+ " \n"
+ " public void chokes2() {\n"
+ " HairType<?> hairType = sheep.getHairType();\n"
+ " hairType.getRenderer().renderHair(hairType, sheep);\n"
+ " }\n"
+ "}";
// Expected Result MethodCallExpr in parsed code
private static final Map<String, String> resultsQualifiedName = new HashMap<>();
private static final Map<String, String> resultsResolvedType = new HashMap<>();
@BeforeAll
static void init() {
resultsQualifiedName.put("sheep.getHairType()", "issue1945.interfaces.HairyAnimal.getHairType");
resultsQualifiedName.put(
"hairType.getRenderer().renderHair(sheep.getHairType(), sheep)",
"issue1945.interfaces.HairTypeRenderer.renderHair");
resultsQualifiedName.put("hairType.getRenderer()", "issue1945.interfaces.HairType.getRenderer");
resultsQualifiedName.put(
"sheep.getHairType().getRenderer().renderHair(sheep.getHairType(), sheep)",
"issue1945.interfaces.HairTypeRenderer.renderHair");
resultsQualifiedName.put("sheep.getHairType().getRenderer()", "issue1945.interfaces.HairType.getRenderer");
resultsQualifiedName.put(
"hairType.getRenderer().renderHair(hairType, sheep)",
"issue1945.interfaces.HairTypeRenderer.renderHair");
resultsResolvedType.put("sheep.getHairType()", "issue1945.interfaces.HairType<?>");
resultsResolvedType.put("hairType.getRenderer().renderHair(sheep.getHairType(), sheep)", "void");
resultsResolvedType.put("hairType.getRenderer()", "R");
resultsResolvedType.put("sheep.getHairType().getRenderer().renderHair(sheep.getHairType(), sheep)", "void");
resultsResolvedType.put("sheep.getHairType().getRenderer()", "R");
resultsResolvedType.put("hairType.getRenderer().renderHair(hairType, sheep)", "void");
}
private static List<MethodCallExpr> parsedCodeMethodCalls() {
Path srcDir = adaptPath("src/test/resources/issue1945");
CombinedTypeSolver typeSolver =
new CombinedTypeSolver(new ReflectionTypeSolver(false), new JavaParserTypeSolver(srcDir));
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(code);
return cu.findAll(MethodCallExpr.class);
}
@ParameterizedTest
@MethodSource("parsedCodeMethodCalls")
void test(MethodCallExpr expr) {
String qName = expr.resolve().getQualifiedName();
String resolvedType = expr.calculateResolvedType().describe();
assertEquals(resultsQualifiedName.get(expr.toString()), qName);
assertEquals(resultsResolvedType.get(expr.toString()), resolvedType);
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParseStart;
import com.github.javaparser.StringProvider;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
class Issue1946Test {
@Test
void issueWithInternalEnumConstantReference() {
String code =
"package com.github.javaparser.symbolsolver.testingclasses; class Foo { void foo() { UtilityClass.method(SomeClass.InnerEnum.CONSTANT); } }";
JavaParser jp = new JavaParser();
CombinedTypeSolver typeSolver = new CombinedTypeSolver(new TypeSolver[] {new ReflectionTypeSolver(false)});
jp.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
ParseResult<CompilationUnit> pr = jp.parse(ParseStart.COMPILATION_UNIT, new StringProvider(code));
assertEquals(true, pr.isSuccessful());
MethodCallExpr methodCallExpr =
pr.getResult().get().findFirst(MethodCallExpr.class).get();
ResolvedMethodDeclaration rmd = methodCallExpr.resolve();
assertEquals(
"com.github.javaparser.symbolsolver.testingclasses.UtilityClass.method(com.github.javaparser.symbolsolver.testingclasses.SomeClass.InnerEnum)",
rmd.getQualifiedSignature());
FieldAccessExpr fieldAccessExpr =
methodCallExpr.findFirst(FieldAccessExpr.class).get();
ResolvedValueDeclaration rvd = fieldAccessExpr.resolve();
assertEquals("CONSTANT", rvd.getName());
assertEquals(
"com.github.javaparser.symbolsolver.testingclasses.SomeClass.InnerEnum",
rvd.getType().describe());
}
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue1950Test extends AbstractResolutionTest {
@Test
public void test() {
TypeSolver typeSolver = new ReflectionTypeSolver(false);
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);
String s = "import java.util.concurrent.Callable;\n" + "class Foo { \n"
+ " void foo() {\n"
+ " method(()->{});\n"
+ " }\n"
+ " public void method(Runnable lambda) {\n"
+ " }\n"
+ " public <T> void method(Callable<T> lambda) {\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
ResolvedMethodDeclaration resolved = mce.resolve();
// 15.12.2.5. Choosing the Most Specific Method
// One applicable method m1 is more specific than another applicable method m2, for an invocation with argument
// expressions e1, ..., ek, if any of the following are true:
// m2 is generic, and m1 is inferred to be more specific than m2 for argument expressions e1, ..., ek by
// §18.5.4.
assertEquals("java.lang.Runnable", resolved.getParam(0).getType().describe());
assertTrue(!resolved.isGeneric());
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
class Issue200Test extends AbstractResolutionTest {
@Test
void issue200() {
CompilationUnit cu = parseSample("Issue200");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "foo");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
List<ReturnStmt> nodesByType = methodDeclaration.findAll(ReturnStmt.class);
assertEquals(
"java.util.stream.Stream<JavaTest.Solved>",
javaParserFacade
.getType((nodesByType.get(0)).getExpression().get())
.describe());
}
}

View File

@@ -0,0 +1,158 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.Providers.provider;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParseStart;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class Issue2035Test {
private JavaParser javaParser;
@BeforeEach
void setUp() {
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
ParserConfiguration configuration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
javaParser = new JavaParser(configuration);
}
@Test
void test() {
String x = "" + "class X {\n"
+ " \n"
+ " private void a(int a){ }\n"
+ " private void b(Integer a){ }\n"
+ " \n"
+ " private void c(){\n"
+ " int x=0;\n"
+ " Integer y=0;\n"
+ " \n"
+ " a(x);\n"
+ " a(y);\n"
+ " \n"
+ " b(x);\n"
+ " b(y);\n"
+ " \n"
+ " }\n"
+ "}"
+ "";
ParseResult<CompilationUnit> parseResult = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(x));
parseResult.getResult().ifPresent(compilationUnit -> {
final List<MethodCallExpr> matches = compilationUnit.findAll(MethodCallExpr.class);
assumeFalse(matches.isEmpty(), "Cannot attempt resolving types if no matches.");
matches.forEach(methodCallExpr -> {
try {
methodCallExpr.resolve().getReturnType();
methodCallExpr.calculateResolvedType(); //
} catch (UnsupportedOperationException e) {
Assertions.fail("Resolution failed.", e);
}
});
});
}
@Test
void test_int() {
String x_int = "" + "import java.util.*;\n"
+ "\n"
+ "class X {\n"
+ " \n"
+ " private void a(){\n"
+ " ArrayList<String> abc = new ArrayList<>();\n"
+ " int x = 0; \n"
+ " abc.get(x);\n"
+ " }\n"
+ "}"
+ "";
ParseResult<CompilationUnit> parseResult = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(x_int));
parseResult.getResult().ifPresent(compilationUnit -> {
final List<MethodCallExpr> matches = compilationUnit.findAll(MethodCallExpr.class).stream()
.filter(methodCallExpr -> methodCallExpr.getNameAsString().equals("get"))
.collect(Collectors.toList());
assumeFalse(matches.isEmpty(), "Cannot attempt resolving types if no matches.");
matches.forEach(methodCallExpr -> {
try {
methodCallExpr.resolve().getReturnType();
methodCallExpr.calculateResolvedType();
} catch (UnsupportedOperationException e) {
Assertions.fail("Resolution failed.", e);
}
});
});
}
@Test
void test_Integer() {
String x_Integer = "" + "import java.util.*;\n"
+ "\n"
+ "class X {\n"
+ " \n"
+ " private void a(){\n"
+ " ArrayList<String> abc = new ArrayList<>();\n"
+ " Integer x = 0; \n"
+ " abc.get(x);\n"
+ " }\n"
+ "}"
+ "";
ParseResult<CompilationUnit> parseResult = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(x_Integer));
parseResult.getResult().ifPresent(compilationUnit -> {
final List<MethodCallExpr> matches = compilationUnit.findAll(MethodCallExpr.class).stream()
.filter(methodCallExpr -> methodCallExpr.getNameAsString().equals("get"))
.collect(Collectors.toList());
assumeFalse(matches.isEmpty(), "Cannot attempt resolving types if no matches.");
matches.forEach(methodCallExpr -> {
try {
methodCallExpr.resolve().getReturnType();
methodCallExpr.calculateResolvedType();
} catch (UnsupportedOperationException e) {
Assertions.fail("Resolution failed.", e);
}
});
});
}
}

View File

@@ -0,0 +1,214 @@
/*
* Copyright (C) 2007-2010 Júlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.Providers.provider;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParseStart;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2044Test {
@Test
public void issue2044_typeVariableExtendsObject() {
String x = "public class X <K extends Object> {\n" + " private int getPartition(final K key) {\n"
+ " int x = (new Object()).hashCode();\n"
+ " return key.hashCode() / getHashes().length;\n"
+ " }\n"
+ "}";
doTestSimple(x);
}
@Test
public void issue2044_simpleTypeVariable() {
String x = "public class X <K> {\n" + " private int getPartition(final K key) {\n"
+ " int x = (new Object()).hashCode();\n"
+ " return key.hashCode() / getHashes().length;\n"
+ " }\n"
+ "}";
doTestSimple(x);
}
private void doTestSimple(String x) {
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
ParserConfiguration configuration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
JavaParser javaParser = new JavaParser(configuration);
ParseResult<CompilationUnit> result = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(x));
assumeTrue(result.isSuccessful());
result.ifSuccessful(compilationUnit -> {
List<MethodCallExpr> methodCallExprs = compilationUnit.findAll(MethodCallExpr.class);
assertEquals(3, methodCallExprs.size());
MethodCallExpr methodCallExpr = methodCallExprs.get(1);
//// Exception-triggering method calls
// throws RuntimeException - stack trace below
methodCallExpr.calculateResolvedType();
/*
java.lang.RuntimeException: Method 'hashCode' cannot be resolved in context key.hashCode() (line: 3) MethodCallExprContext{wrapped=key.hashCode()}. Parameter types: []
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:586)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:267)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:44)
at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:115)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:447)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:310)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:292)
at com.github.javaparser.symbolsolver.JavaSymbolSolver.calculateType(JavaSymbolSolver.java:250)
at com.github.javaparser.ast.expr.Expression.calculateResolvedType(Expression.java:564)
at com.github.javaparser.symbolsolver.Issue2044.lambda$null$0(Issue2044.java:81)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at com.github.javaparser.symbolsolver.Issue2044.lambda$issue2044$1(Issue2044.java:49)
*/
// throws UnsolvedSymbolException - stack trace below
methodCallExpr.resolve();
/*
Unsolved symbol : We are unable to find the method declaration corresponding to key.hashCode()
UnsolvedSymbolException{context='null', name='We are unable to find the method declaration corresponding to key.hashCode()', cause='null'}
at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:146)
at com.github.javaparser.ast.expr.MethodCallExpr.resolve(MethodCallExpr.java:313)
at com.github.javaparser.symbolsolver.Issue2044.lambda$null$0(Issue2044.java:101)
*/
});
}
private void doTest(String x) {
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
ParserConfiguration configuration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
JavaParser javaParser = new JavaParser(configuration);
ParseResult<CompilationUnit> result = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(x));
result.ifSuccessful(compilationUnit -> {
final List<MethodDeclaration> methodDeclarations = compilationUnit.findAll(MethodDeclaration.class);
methodDeclarations.forEach(methodDeclaration -> {
// Method declaration
ResolvedMethodDeclaration resolvedMethodDeclaration = methodDeclaration.resolve();
String resolvedReturnType =
resolvedMethodDeclaration.getReturnType().describe();
assertEquals("int", resolvedReturnType);
// Parameters
NodeList<Parameter> parameters = methodDeclaration.getParameters();
assertEquals(1, parameters.size());
Parameter parameter = parameters.get(0);
assertEquals("K", parameter.getType().asString());
assertEquals("key", parameter.getName().asString());
assertEquals("K", parameter.resolve().getType().describe());
assertEquals("K", parameter.resolve().describeType());
// Method calls inside declaration
List<MethodCallExpr> methodCalls = methodDeclaration.findAll(MethodCallExpr.class);
assertEquals(3, methodCalls.size());
// (new Object()).hashCode()
final MethodCallExpr object_hashCode = methodCalls.get(0);
assertTrue(object_hashCode.hasScope());
Expression object_hashCode_scope = object_hashCode.getScope().get();
assertEquals(
"java.lang.Object",
object_hashCode_scope.calculateResolvedType().describe());
assertEquals("int", object_hashCode.resolve().getReturnType().describe());
assertEquals("int", object_hashCode.calculateResolvedType().describe());
// key.hashCode()
final MethodCallExpr key_hashCode = methodCalls.get(1);
assertTrue(key_hashCode.hasScope());
Expression key_hashCode_scope = key_hashCode.getScope().get();
assertEquals("K", key_hashCode_scope.calculateResolvedType().describe());
// These shouldn't pass...
// assertThrows(RuntimeException.class, key_hashCode::calculateResolvedType);
// assertThrows(UnsolvedSymbolException.class, key_hashCode::resolve);
// throws RuntimeException - stack trace below
ResolvedType key_hashCode_resolvedType = ((MethodCallExpr) key_hashCode).calculateResolvedType();
/*
java.lang.RuntimeException: Method 'hashCode' cannot be resolved in context key.hashCode() (line: 3) MethodCallExprContext{wrapped=key.hashCode()}. Parameter types: []
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solveMethodAsUsage(JavaParserFacade.java:586)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:267)
at com.github.javaparser.symbolsolver.javaparsermodel.TypeExtractor.visit(TypeExtractor.java:44)
at com.github.javaparser.ast.expr.MethodCallExpr.accept(MethodCallExpr.java:115)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:447)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:310)
at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.getType(JavaParserFacade.java:292)
at com.github.javaparser.symbolsolver.JavaSymbolSolver.calculateType(JavaSymbolSolver.java:250)
at com.github.javaparser.ast.expr.Expression.calculateResolvedType(Expression.java:564)
at com.github.javaparser.symbolsolver.Issue2044.lambda$null$0(Issue2044.java:81)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at com.github.javaparser.symbolsolver.Issue2044.lambda$issue2044$1(Issue2044.java:49)
*/
// throws UnsolvedSymbolException - stack trace below
ResolvedMethodDeclaration key_hashCode_resolved = ((MethodCallExpr) key_hashCode).resolve();
/*
Unsolved symbol : We are unable to find the method declaration corresponding to key.hashCode()
UnsolvedSymbolException{context='null', name='We are unable to find the method declaration corresponding to key.hashCode()', cause='null'}
at com.github.javaparser.symbolsolver.JavaSymbolSolver.resolveDeclaration(JavaSymbolSolver.java:146)
at com.github.javaparser.ast.expr.MethodCallExpr.resolve(MethodCallExpr.java:313)
at com.github.javaparser.symbolsolver.Issue2044.lambda$null$0(Issue2044.java:101)
*/
ResolvedType key_hashCode_resolvedReturnType = key_hashCode_resolved.getReturnType();
String key_hashCode_resolvedReturnTypeString = key_hashCode_resolvedReturnType.describe();
assertEquals("int", key_hashCode.resolve().getReturnType().describe());
});
});
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2062Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
String s = "import java.util.Optional;\n" + "public class Base{\n"
+ " class Derived extends Base{\n"
+ " }\n"
+ " \n"
+ " public void bar(Optional<Base> o) {\n"
+ " }\n"
+ " public void foo() {\n"
+ " bar(Optional.of(new Derived()));\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
List<MethodCallExpr> mces = cu.findAll(MethodCallExpr.class);
assertEquals("bar(Optional.of(new Derived()))", mces.get(0).toString());
assertEquals("Base.bar(java.util.Optional<Base>)", mces.get(0).resolve().getQualifiedSignature());
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2065Test extends AbstractResolutionTest {
@Test
void test() {
String code = "import java.util.stream.Stream;\n" + "\n"
+ "public class A {\n"
+ " public void test(){\n"
+ " Stream.of(1,2).reduce((a, b) -> Math.max(a, b));\n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(code);
List<MethodCallExpr> exprs = cu.findAll(MethodCallExpr.class);
for (MethodCallExpr expr : exprs) {
if (expr.getNameAsString().contentEquals("max")) {
assertEquals("java.lang.Math.max(int, int)", expr.resolve().getQualifiedSignature());
}
}
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue2083Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
TypeSolver typeSolver = new ReflectionTypeSolver(false);
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);
String s = "import static Simple.SPACES;\n" + "public class Simple {\n"
+ " public enum IndentType {\n"
+ " SPACES\n"
+ " }\n"
+ " public IndentType c = SPACES;\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
FieldDeclaration fd = cu.findFirst(FieldDeclaration.class).get();
assertEquals("Simple.IndentType", fd.resolve().getType().describe());
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue2132Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
TypeSolver typeSolver = new ReflectionTypeSolver();
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);
String s = "class A {\n" + " void method() {\n"
+ " String s = \"\";\n"
+ " {\n"
+ " s.length();\n"
+ " }\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
assertEquals("int", mce.calculateResolvedType().describe());
assertEquals("java.lang.String.length", mce.resolve().getQualifiedName());
assertEquals(
"java.lang.String", mce.getScope().get().calculateResolvedType().describe());
}
}

View File

@@ -0,0 +1,171 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.Providers.provider;
import static org.junit.jupiter.api.Assertions.*;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParseStart;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* @see <a href="https://github.com/javaparser/javaparser/issues/2162">https://github.com/javaparser/javaparser/issues/2162</a>
*/
public class Issue2162Test extends AbstractSymbolResolutionTest {
private JavaParser javaParser;
private CompilationUnit cu;
private TypeSolver typeSolver;
private ParserConfiguration configuration;
private List<MethodDeclaration> classMethods;
private List<MethodCallExpr> methodCallExprs;
@BeforeEach
public void setUp() {
typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
configuration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
javaParser = new JavaParser(configuration);
// language=JAVA
String src = "" + "import java.awt.*;\n"
+ "\n"
+ "abstract class Screen <V extends Component> {\n"
+ " abstract V getView();\n"
+ "}\n"
+ "\n"
+ "class D extends Component {\n"
+ " void getTest() {\n"
+ " }\n"
+ "}\n"
+ "\n"
+ "class B extends Screen<D> {\n"
+ " @Override\n"
+ " D getView() {\n"
+ " return new D();\n"
+ " }\n"
+ "}\n"
+ "\n"
+ "class Run {\n"
+ " public static void main(String[] args) {\n"
+ " B b1 = new B();\n"
+ " b1.getView(); // b1.getView() -> B#getView(), overriding Screen#getView() -> returns object of type D.\n"
+ " \n"
+ " // Note that if `b2.getView` is parsed as Screen#getView (as B extends Screen), it will return type `V extends Component` thus will fail to locate the method `Component#getTest()` \n"
+ " B b2 = new B();\n"
+ " b2.getView().getTest(); // b2.getView() -> returns object of type D, per above // D#getTest returns void.\n"
+ " \n"
+ " // This part is expected to fail as D#getView does not exist (where D is of type `V extends Component`)\n"
+ " B b3 = new B();\n"
+ " b3.getView().getView(); // b3.getView() -> returns object of type D, per above // D#getView doesn't exist, thus resolution will fail.\n"
+ " }\n"
+ "}\n"
+ "";
ParseResult<CompilationUnit> parseResult = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(src));
// parseResult.getProblems().forEach(problem -> System.out.println("problem.getVerboseMessage() = " +
// problem.getVerboseMessage()));
assertTrue(parseResult.isSuccessful());
assertEquals(
0, parseResult.getProblems().size(), "Expected zero errors when attempting to parse the input code.");
assertTrue(parseResult.getResult().isPresent(), "Must have a parse result to run this test.");
this.cu = parseResult.getResult().get();
classMethods = this.cu.getClassByName("Run").get().getMethods();
assertEquals(1, classMethods.size(), "Expected only one class with this matching name.");
methodCallExprs = classMethods.get(0).findAll(MethodCallExpr.class);
assertTrue(methodCallExprs.size() > 0, "Expected more than one method call.");
}
@Test
public void doTest_withJavaParserFacade_explicit() {
JavaParserFacade javaParserFacade = JavaParserFacade.get(this.typeSolver);
// assertEquals(5, methodCallExprs.size(), "Unexpected number of method calls -- has the test code been
// updated, without also updating this test case?");
// b1.getView()
assertEquals(
"D",
javaParserFacade
.solve(methodCallExprs.get(0))
.getCorrespondingDeclaration()
.getReturnType()
.describe());
// b2.getView()
assertEquals(
"D",
javaParserFacade
.solve(methodCallExprs.get(2))
.getCorrespondingDeclaration()
.getReturnType()
.describe());
// b2.getView().getTest()
assertEquals(
"void",
javaParserFacade
.solve(methodCallExprs.get(1))
.getCorrespondingDeclaration()
.getReturnType()
.describe());
// b3.getView()
assertEquals(
"D",
javaParserFacade
.solve(methodCallExprs.get(4))
.getCorrespondingDeclaration()
.getReturnType()
.describe());
assertThrows(
UnsolvedSymbolException.class,
() -> {
// b3.getView().getView() -- causing error
assertEquals(
"V",
javaParserFacade
.solve(methodCallExprs.get(3))
.getCorrespondingDeclaration()
.getReturnType()
.describe());
},
"Exected this resolution to fail due to the chained methods -- `getView()` shouldn't exist on the return value from the first call to `getView()`.");
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodReferenceExpr;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class Issue2210Test extends AbstractResolutionTest {
@BeforeEach
void setup() {}
@Test
void test2210Issue() {
// Source code
String sourceCode = "class A {" + " public void m() {\n"
+ " java.util.Arrays.asList(1, 2, 3).forEach(System.out::println);"
+ " }\n"
+ "}";
// Setup symbol solver
ParserConfiguration configuration = new ParserConfiguration()
.setSymbolResolver(new JavaSymbolSolver(new CombinedTypeSolver(new ReflectionTypeSolver())));
// Setup parser
JavaParser parser = new JavaParser(configuration);
CompilationUnit cu = parser.parse(sourceCode).getResult().get();
// Test
MethodReferenceExpr expr = Navigator.demandNodeOfGivenClass(cu, MethodReferenceExpr.class);
ResolvedType type = expr.calculateResolvedType();
assertEquals("java.util.function.Consumer<? super T>", type.describe());
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2236Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
final Path testRoot = adaptPath("src/test/resources/issue2236");
TypeSolver reflectionTypeSolver = new ReflectionTypeSolver();
JavaParserTypeSolver javaParserTypeSolver = new JavaParserTypeSolver(testRoot);
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(reflectionTypeSolver, javaParserTypeSolver);
ParserConfiguration configuration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver));
StaticJavaParser.setConfiguration(configuration);
String src = "class X {public void f(){ " + "new A<Object>(new Boolean(true)); }}";
CompilationUnit cu = StaticJavaParser.parse(src);
List<ObjectCreationExpr> oces = cu.findAll(ObjectCreationExpr.class);
assertEquals("A.A(java.lang.Boolean)", oces.get(0).resolve().getQualifiedSignature());
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class Issue2259Test extends AbstractResolutionTest {
@BeforeEach
void setup() {}
@Test
void test() throws IOException {
// Source code
String src = "public class TestClass2 {\n" + " public static void foo(Object o) {\n"
+ " }\n"
+ " public static void main(String[] args) {\n"
+ " foo(new Object[5]);\n"
+ " }\n"
+ "}";
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
// Setup symbol solver
ParserConfiguration configuration = new ParserConfiguration()
.setSymbolResolver(new JavaSymbolSolver(typeSolver))
.setLanguageLevel(LanguageLevel.JAVA_8);
// Setup parser
StaticJavaParser.setConfiguration(configuration);
CompilationUnit cu = StaticJavaParser.parse(src);
MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
assertEquals("foo(new Object[5])", mce.toString());
assertEquals("TestClass2.foo(java.lang.Object)", mce.resolve().getQualifiedSignature());
assertEquals("void", mce.calculateResolvedType().describe());
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2284Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
TypeSolver typeSolver = new ReflectionTypeSolver(false);
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);
String s = "public enum Enum {\n" + " CONSTANT_ENUM() {\n"
+ " @Override\n"
+ " String getEnumName() {\n"
+ " return \"CONSTANT_ENUM\";\n"
+ " }\n"
+ " };\n"
+ " \n"
+ " String getEnumName() {\n"
+ " return \"default\";\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
List<MethodDeclaration> mds = cu.findAll(MethodDeclaration.class);
mds.forEach(md -> {
assertEquals("Enum.getEnumName()", md.resolve().getQualifiedSignature());
});
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2289Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
TypeSolver typeSolver = new ReflectionTypeSolver(false);
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);
String s = "public class Test \n" + "{\n"
+ " public class InnerClass \n"
+ " {\n"
+ " public InnerClass(int i, int j) { \n"
+ " }\n"
+ "\n"
+ " public InnerClass(int i, int ...j) { \n"
+ " } \n"
+ " }\n"
+ " \n"
+ " public Test() { \n"
+ " new InnerClass(1,2);\n"
+ " new InnerClass(1,2,3);\n"
+ " } \n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
List<ObjectCreationExpr> exprs = cu.findAll(ObjectCreationExpr.class);
assertEquals(
"Test.InnerClass.InnerClass(int, int)", exprs.get(0).resolve().getQualifiedSignature());
assertEquals(
"Test.InnerClass.InnerClass(int, int...)",
exprs.get(1).resolve().getQualifiedSignature());
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.StaticJavaParser.parse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
class Issue228Test extends AbstractResolutionTest {
@Test
void testSolvingMethodWitPrimitiveParameterTypeAsUsage() {
String code = "class Test { "
+ " long l = call(1); "
+ " long call(final long i) { "
+ " return i; "
+ " }"
+ "}";
CompilationUnit cu = parse(code);
MethodCallExpr methodCall = cu.findAll(MethodCallExpr.class).get(0);
JavaParserFacade parserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
MethodUsage solvedCall = parserFacade.solveMethodAsUsage(methodCall);
assertEquals("long", solvedCall.getParamType(0).describe());
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.resolution.Context;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
import com.github.javaparser.resolution.model.SymbolReference;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
class Issue232Test extends AbstractResolutionTest {
@Test
void issue232() {
CompilationUnit cu = parseSample("Issue232");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "OfDouble");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
Context context = JavaParserFactory.getContext(cls, typeSolver);
SymbolReference<ResolvedTypeDeclaration> reference =
context.solveType("OfPrimitive<Double, DoubleConsumer, OfDouble>");
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.Arrays;
import java.util.Collection;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
class Issue235Test extends AbstractResolutionTest {
static Collection<String> data() {
return Arrays.asList("new_Bar_Baz_direct", "new_Bar_Baz", "new_Bar", "new_Foo_Bar");
}
@ParameterizedTest
@MethodSource("data")
void issue235(String method) {
CompilationUnit cu = parseSample("Issue235");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Foo");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
MethodDeclaration m = Navigator.demandMethod(cls, method);
ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(0);
ObjectCreationExpr expression = (ObjectCreationExpr) stmt.getExpression();
Assertions.assertNotNull(javaParserFacade.convertToUsage(expression.getType()));
}
}

View File

@@ -0,0 +1,108 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
class Issue2360 extends AbstractSymbolResolutionTest {
@Test
void testUnaryExprResolvedViaUnaryNumericPromotion_char() {
String source = "public class Test\n" + "{\n"
+ " public class InnerClass\n"
+ " {\n"
+ " public InnerClass(char c) {}\n"
+ " public InnerClass(int i) {}\n"
+ " }\n"
+ " \n"
+ " public Test() {\n"
+ " new InnerClass(+'.'); \n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(source);
ObjectCreationExpr expr = cu.findFirst(ObjectCreationExpr.class).get();
ResolvedConstructorDeclaration rcd = expr.resolve();
assertEquals("InnerClass(int)", rcd.getSignature());
}
@Test
void testUnaryExprResolvedViaUnaryNumericPromotion_byte() {
String source = "public class Test\n" + "{\n"
+ " public class InnerClass\n"
+ " {\n"
+ " public InnerClass(char c) {}\n"
+ " public InnerClass(int i) {}\n"
+ " }\n"
+ " \n"
+ " public Test() {\n"
+ " byte b = 0;\n"
+ " new InnerClass(+b); \n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(source);
ObjectCreationExpr expr = cu.findFirst(ObjectCreationExpr.class).get();
ResolvedConstructorDeclaration rcd = expr.resolve();
assertEquals("InnerClass(int)", rcd.getSignature());
}
@Test
void testUnaryExprResolvedViaUnaryNumericPromotion_short() {
String source = "public class Test\n" + "{\n"
+ " public class InnerClass\n"
+ " {\n"
+ " public InnerClass(char c) {}\n"
+ " public InnerClass(int i) {}\n"
+ " }\n"
+ " \n"
+ " public Test() {\n"
+ " short b = 0;\n"
+ " new InnerClass(+b); \n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(source);
ObjectCreationExpr expr = cu.findFirst(ObjectCreationExpr.class).get();
ResolvedConstructorDeclaration rcd = expr.resolve();
assertEquals("InnerClass(int)", rcd.getSignature());
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseStart;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ParserConfiguration.LanguageLevel;
import com.github.javaparser.StreamProvider;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
class Issue2362Test extends AbstractSymbolResolutionTest {
@Test
void issue2362() throws IOException {
Path dir = adaptPath("src/test/resources/issue2362");
Path file = adaptPath("src/test/resources/issue2362/Test.java");
CombinedTypeSolver combinedSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
ParserConfiguration pc = new ParserConfiguration()
.setSymbolResolver(new JavaSymbolSolver(combinedSolver))
.setLanguageLevel(LanguageLevel.JAVA_8);
JavaParser javaParser = new JavaParser(pc);
CompilationUnit unit = javaParser
.parse(
ParseStart.COMPILATION_UNIT,
new StreamProvider(Files.newInputStream(file), StandardCharsets.UTF_8.name()))
.getResult()
.get();
ObjectCreationExpr oce = unit.findFirst(ObjectCreationExpr.class).get();
assertEquals(oce.resolve().getSignature(), "InnerClass(int)");
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseStart;
import com.github.javaparser.StreamProvider;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
class Issue2367Test extends AbstractSymbolResolutionTest {
@Test
void issue2367() throws IOException {
Path dir = adaptPath("src/test/resources/issue2367");
Path file = adaptPath("src/test/resources/issue2367/Issue2367.java");
CombinedTypeSolver typeSolver = new CombinedTypeSolver();
typeSolver.add(new JavaParserTypeSolver(dir));
JavaParser javaParser = new JavaParser();
javaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
CompilationUnit unit = javaParser
.parse(
ParseStart.COMPILATION_UNIT,
new StreamProvider(Files.newInputStream(file), StandardCharsets.UTF_8.name()))
.getResult()
.get();
NameExpr nameExpr = unit.findFirst(
NameExpr.class, m -> m.getName().getIdentifier().equals("privateField"))
.get();
ResolvedValueDeclaration resolvedValueDeclaration = nameExpr.resolve();
assertEquals("double", resolvedValueDeclaration.getType().describe());
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue2397Test extends AbstractSymbolResolutionTest {
@Test
public void testProvided1() {
String sourceCode =
"static final class ConstantFuture<T> implements Future<T> {\n" + " private final T value;\n"
+ " \n"
+ " @Override\n"
+ " public T get() {\n"
+ " return value;\n"
+ " }\n"
+ "}";
testIssue(sourceCode);
}
@Test
public void testProvided2() {
String sourceCode = "class A {\n" + " public static <T> T[] toArray(final T... items) {\n"
+ " return items;\n"
+ " }\n"
+ "}";
testIssue(sourceCode);
}
public void testIssue(String sourceCode) {
TypeSolver solver = new ReflectionTypeSolver();
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(solver));
JavaParser parser = new JavaParser(parserConfiguration);
ParseResult<CompilationUnit> cu = parser.parse(sourceCode);
cu.ifSuccessful(c -> c.accept(
new VoidVisitorAdapter<Void>() {
@Override
public void visit(ClassOrInterfaceType classOrInterfaceType, Void arg) {
super.visit(classOrInterfaceType, arg);
ResolvedType resolved = classOrInterfaceType.resolve();
assertTrue(resolved.isTypeVariable());
}
},
null));
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import java.util.function.Predicate;
import org.junit.jupiter.api.Test;
public class Issue2406Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
ParserConfiguration config =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
String s = "import java.lang.reflect.Array;\n" + "\n"
+ "public class Main {\n"
+ " public static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {\n"
+ " @SuppressWarnings(\"unchecked\")\n"
+ " T[] copy = ((Object) newType == (Object)Object[].class)\n"
+ " ? (T[]) new Object[newLength]\n"
+ " : (T[]) Array.newInstance(newType.getComponentType(), newLength);\n"
+ " System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));\n"
+ " return copy;\n"
+ " }\n"
+ "\n"
+ " public static void main(String[] args) {\n"
+ " String[] source = {\"a\", \"b\", \"c\"};\n"
+ " String[] target = copyOf(source, 2, source.getClass());\n"
+ " for (String e : target) System.out.println(e);\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
List<MethodCallExpr> mces = cu.findAll(MethodCallExpr.class, new Predicate() {
@Override
public boolean test(Object t) {
return ((MethodCallExpr) t).getNameAsString().equals("copyOf");
}
});
assertEquals(
"Main.copyOf(U[], int, java.lang.Class<? extends T[]>)",
mces.get(0).resolve().getQualifiedSignature());
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
class Issue241Test extends AbstractResolutionTest {
@Test
void testSolveStaticallyImportedMemberType() {
Path src = adaptPath("src/test/resources");
TypeSolver typeSolver = new CombinedTypeSolver(
new ReflectionTypeSolver(), new JavaParserTypeSolver(src, new LeanParserConfiguration()));
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
CompilationUnit cu = parseSample("Issue241");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Main");
VariableDeclarator v = Navigator.demandVariableDeclaration(cls, "foo").get();
Type t = v.getType();
ResolvedType t2 = javaParserFacade.convert(t, t);
String typeName = t2.asReferenceType().getQualifiedName();
assertEquals("issue241.TypeWithMemberType.MemberInterface", typeName);
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue2477Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
TypeSolver typeSolver = new ReflectionTypeSolver();
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
StaticJavaParser.setConfiguration(config);
String s = "class A {\n" + " void method() {\n"
+ " A a = this;\n"
+ " {\n"
+ " a.method();\n"
+ " }\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
MethodCallExpr mce = cu.findFirst(MethodCallExpr.class).get();
assertEquals("A.method", mce.resolve().getQualifiedName());
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue2481Test {
@Test
public void test() {
TypeSolver solver = new ReflectionTypeSolver();
ParserConfiguration parserConfiguration = new ParserConfiguration();
parserConfiguration.setSymbolResolver(new JavaSymbolSolver(solver));
JavaParser parser = new JavaParser(parserConfiguration);
ParseResult<CompilationUnit> cu = parser.parse("class A<T> { T t; }");
cu.ifSuccessful(c -> {
c.accept(
new VoidVisitorAdapter<Void>() {
@Override
public void visit(ClassOrInterfaceType n, Void arg) {
super.visit(n, arg);
n.resolve();
}
},
null);
});
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2489Test extends AbstractSymbolResolutionTest {
@Test
public void test() {
final Path testRoot = adaptPath("src/test/resources/issue2489");
TypeSolver reflectionTypeSolver = new ReflectionTypeSolver(false);
JavaParserTypeSolver javaParserTypeSolver = new JavaParserTypeSolver(testRoot);
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(reflectionTypeSolver, javaParserTypeSolver);
ParserConfiguration configuration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver));
StaticJavaParser.setConfiguration(configuration);
String src = "public class B {\n" + " public void m() {\n"
+ " ComponentBase otm4e = new ComponentBase();\n"
+ " otm4e.set(\"OTM4E_EFFLEVEL\", \"IE1 / STD\", true);\n"
+ " }\n"
+ "\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(src);
List<MethodCallExpr> mces = cu.findAll(MethodCallExpr.class);
assertEquals(
"ObjectContext.set(java.lang.String, java.lang.Object, boolean)",
mces.get(0).resolve().getQualifiedSignature());
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class Issue251Test extends AbstractResolutionTest {
@Test
void testSolveStaticallyImportedMemberType() {
CompilationUnit cu = parseSample("Issue251");
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Main");
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
MethodDeclaration m = Navigator.demandMethod(cls, "bar");
ExpressionStmt stmt = (ExpressionStmt) m.getBody().get().getStatements().get(1);
MethodCallExpr expression = (MethodCallExpr) stmt.getExpression();
Assertions.assertNotNull(javaParserFacade.solve(expression));
}
}

View File

@@ -0,0 +1,164 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.Providers.provider;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParseStart;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2595Test {
@Test
public void issue2595ImplicitTypeLambdaTest() {
String sourceCode = "" + "import java.util.ArrayList;\n"
+ "import java.util.List;\n"
+ "import java.util.function.Function;\n"
+ "\n"
+ "public class Test {\n"
+ "\n"
+ " ClassMetric<Integer> fdp = c -> {\n"
+ " List<String> classFieldNames = getAllClassFieldNames(c);\n"
+ " return classFieldNames.size();\n"
+ " };\n"
+ "\n"
+ "\n"
+ " private List<String> getAllClassFieldNames(final String c) {\n"
+ " return new ArrayList<>();\n"
+ " }\n"
+ "\n"
+ "\n"
+ " @FunctionalInterface\n"
+ " public interface ClassMetric<T> extends Function<String, T> {\n"
+ " @Override\n"
+ " T apply(String c);\n"
+ " }\n"
+ "\n"
+ "}\n";
parse(sourceCode);
}
@Test
public void issue2595ExplicitTypeLambdaTest() {
String sourceCode = "import java.util.ArrayList;\n" + "import java.util.List;\n"
+ "import java.util.function.Function;\n"
+ "\n"
+ "public class TestIssue2595 {\n"
+ " ClassMetric fdp = (String c) -> {\n"
+ " List<String> classFieldNames = getAllClassFieldNames(c);\n"
+ " return classFieldNames.size();\n"
+ " };\n"
+ " \n"
+ "\n"
+ " private List<String> getAllClassFieldNames(final String c) {\n"
+ " return new ArrayList<>();\n"
+ " }\n"
+ "\n"
+ " @FunctionalInterface\n"
+ " public interface ClassMetric extends Function<String, Integer> {\n"
+ " @Override\n"
+ " Integer apply(String c);\n"
+ " }\n"
+ "}";
parse(sourceCode);
}
@Test
public void issue2595NoParameterLambdaTest() {
String sourceCode = "import java.util.ArrayList;\n" + "import java.util.List;\n"
+ "\n"
+ "public class TestIssue2595 {\n"
+ " ClassMetric fdp = () -> {\n"
+ " List<String> classFieldNames = getAllClassFieldNames();\n"
+ " return classFieldNames.size();\n"
+ " };\n"
+ "\n"
+ "\n"
+ " private List<String> getAllClassFieldNames() {\n"
+ " return new ArrayList<>();\n"
+ " }\n"
+ "\n"
+ " @FunctionalInterface\n"
+ " public interface ClassMetric {\n"
+ " Integer apply();\n"
+ " }\n"
+ "}";
parse(sourceCode);
}
@Test
public void issue2595AnonymousInnerClassTest() {
String sourceCode = "import java.util.ArrayList;\n" + "import java.util.List;\n"
+ "import java.util.function.Function;\n"
+ "\n"
+ "public class TestIssue2595 {\n"
+ " ClassMetric fdp = new ClassMetric() {\n"
+ " @Override\n"
+ " public Integer apply(String c) {\n"
+ " List<String> classFieldNames = getAllClassFieldNames(c);\n"
+ " return classFieldNames.size();\n"
+ " }\n"
+ " };\n"
+ "\n"
+ " private List<String> getAllClassFieldNames(final String c) {\n"
+ " return new ArrayList<>();\n"
+ " }\n"
+ "\n"
+ " @FunctionalInterface\n"
+ " public interface ClassMetric extends Function<String, Integer> {\n"
+ " @Override\n"
+ " Integer apply(String c);\n"
+ " }\n"
+ "}";
parse(sourceCode);
}
private void parse(String sourceCode) {
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver());
ParserConfiguration configuration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
JavaParser javaParser = new JavaParser(configuration);
ParseResult<CompilationUnit> result = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(sourceCode));
assumeTrue(result.isSuccessful());
assumeTrue(result.getResult().isPresent());
CompilationUnit cu = result.getResult().get();
List<MethodCallExpr> methodCalls = cu.findAll(MethodCallExpr.class);
assumeFalse(methodCalls.isEmpty());
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.Providers.provider;
import static org.junit.jupiter.api.Assertions.*;
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ParseStart;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.MemoryTypeSolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class Issue2602Test extends AbstractSymbolResolutionTest {
private JavaParser javaParser;
private CompilationUnit cu;
private MemoryTypeSolver typeSolver;
private ParserConfiguration configuration;
@BeforeEach
public void setUp() {
typeSolver = new MemoryTypeSolver();
configuration = new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver));
javaParser = new JavaParser(configuration);
// language=JAVA
String src = "package java.lang;" + " class Object {}\n"
+ "\n"
+ "class A extends Object {}\n"
+ "\n"
+ "class B extends Object {}\n";
ParseResult<CompilationUnit> parseResult = javaParser.parse(ParseStart.COMPILATION_UNIT, provider(src));
// parseResult.getProblems().forEach(problem -> System.out.println("problem.getVerboseMessage() = " +
// problem.getVerboseMessage()));
assertTrue(parseResult.isSuccessful());
assertEquals(
0, parseResult.getProblems().size(), "Expected zero errors when attempting to parse the input code.");
assertTrue(parseResult.getResult().isPresent(), "Must have a parse result to run this test.");
this.cu = parseResult.getResult().get();
JavaParserFacade javaParserFacade = JavaParserFacade.get(this.typeSolver);
for (TypeDeclaration t : this.cu.getTypes()) {
JavaParserClassDeclaration classDecl =
new JavaParserClassDeclaration((ClassOrInterfaceDeclaration) t, this.typeSolver);
this.typeSolver.addDeclaration((String) t.getFullyQualifiedName().get(), classDecl);
}
}
@Test
public void doTest_checkForRecursionWhen_java_lang_Object_IsA_JavaParserClassDeclaration() {
ResolvedReferenceTypeDeclaration thisDeclaration = typeSolver.solveType("java.lang.A");
ResolvedReferenceTypeDeclaration secondDeclaration = typeSolver.solveType("java.lang.B");
assertFalse(thisDeclaration.canBeAssignedTo(secondDeclaration), "Both types should not be assignable");
}
}

View File

@@ -0,0 +1,31 @@
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
public class Issue2738Test extends AbstractSymbolResolutionTest {
@Test
void test() throws IOException {
ParserConfiguration config = new ParserConfiguration();
Path pathToSourceFile = adaptPath("src/test/resources/issue2738");
TypeSolver cts = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(pathToSourceFile));
config.setSymbolResolver(new JavaSymbolSolver(cts));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(pathToSourceFile.resolve("B.java"));
// We shouldn't throw an exception
assertDoesNotThrow(() -> cu.findAll(MethodCallExpr.class).stream().map(MethodCallExpr::resolve));
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2740Test extends AbstractResolutionTest {
@Test()
void test() {
String code = "import java.util.function.Consumer;\n" + "import java.util.ArrayList;\n"
+ "\n"
+ "public class A {\n"
+ " \n"
+ " void m() {\n"
+ " new Consumer<String>() {\n"
+ " private ArrayList<Integer> t = new ArrayList<>();\n"
+ " @Override\n"
+ " public void accept(String s) {\n"
+ " t.add(s);\n"
+ " }\n"
+ " \n"
+ " };"
+ " }\n"
+ "\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(code);
List<MethodCallExpr> methodCallExpr = cu.findAll(MethodCallExpr.class);
for (MethodCallExpr expr : methodCallExpr) {
ResolvedMethodDeclaration rd = expr.resolve();
}
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.expr.UnaryExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class Issue2764Test {
private JavaParser javaParser;
@BeforeEach
void setUp() {
TypeSolver typeSolver = new ReflectionTypeSolver();
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(typeSolver));
javaParser = new JavaParser(config);
}
@Test
void resolveUnaryExpr() {
String code = "class A {" + " void a() {" + " int e;" + " for(e++;;){}" + " }" + "}";
ParseResult<CompilationUnit> parseResult =
javaParser.parse(ParseStart.COMPILATION_UNIT, Providers.provider(code));
assertTrue(parseResult.isSuccessful());
assertTrue(parseResult.getResult().isPresent());
CompilationUnit cu = parseResult.getResult().get();
NameExpr name = (NameExpr) cu.findFirst(UnaryExpr.class).get().getExpression();
ResolvedValueDeclaration resolve = name.resolve();
assertTrue("int".contentEquals(resolve.getType().describe()));
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.StaticJavaParser.parse;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodContext;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class Issue276Test extends AbstractResolutionTest {
@Test
void testSolveStaticallyImportedMemberType() throws IOException {
CompilationUnit cu = parse(adaptPath("src/test/resources/issue276/foo/C.java"));
ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "C");
TypeSolver typeSolver = new CombinedTypeSolver(
new ReflectionTypeSolver(),
new JavaParserTypeSolver(adaptPath("src/test/resources/issue276"), new LeanParserConfiguration()));
List<MethodDeclaration> methods = cls.findAll(MethodDeclaration.class);
boolean isSolved = false;
for (MethodDeclaration method : methods) {
if (method.getNameAsString().equals("overrideMe")) {
MethodContext context = new MethodContext(method, typeSolver);
isSolved = context.solveType("FindMeIfYouCan").isSolved();
}
}
Assertions.assertTrue(isSolved);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.ConstructorDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.FileNotFoundException;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2781Test extends AbstractResolutionTest {
@Test()
void test() throws FileNotFoundException {
String code = "public class A implements AnInterface {\n" + " private AnInterface field;\n"
+ "\n"
+ " protected AnInterface getContainer() {\n"
+ " return this.field;\n"
+ " }\n"
+ " protected static class AnInterface {\n"
+ " }\n"
+ "}";
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
combinedTypeSolver.add(new JavaParserTypeSolver(adaptPath("src/test/resources")));
StaticJavaParser.getConfiguration().setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver));
CompilationUnit cu = StaticJavaParser.parse(code);
List<ConstructorDeclaration> constructorDeclarations = cu.findAll(ConstructorDeclaration.class);
constructorDeclarations.forEach(constructorDeclaration -> {
constructorDeclaration.findAll(MethodCallExpr.class).forEach(methodCallExpr -> {
// Exception in thread "main" java.lang.StackOverflowError
ResolvedMethodDeclaration rmd = methodCallExpr.resolve();
});
});
}
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
public class Issue2823Test extends AbstractSymbolResolutionTest {
@Test
void test() {
final Path testRoot = adaptPath("src/test/resources/issue2823");
TypeSolver reflectionTypeSolver = new ReflectionTypeSolver();
JavaParserTypeSolver javaParserTypeSolver = new JavaParserTypeSolver(testRoot);
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver(reflectionTypeSolver, javaParserTypeSolver);
ParserConfiguration configuration =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(combinedTypeSolver));
StaticJavaParser.setConfiguration(configuration);
String src = "import java.util.Optional;\n"
+ "public class TestClass {\n"
+ " public Long getValue() {\n"
+ " Optional<ClassA> classA = Optional.of(new ClassA());\n"
+ " return classA.map(a -> a.obj)\n"
+ " .map(b -> b.value)\n"
+ " .orElse(null);\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(src);
// verify there is no exception thrown when we try to resolve all field access expressions
cu.findAll(FieldAccessExpr.class).forEach(fd -> fd.resolve());
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
class Issue2878Test extends AbstractResolutionTest {
@Test()
void test() throws IOException {
Path rootSourceDir = adaptPath("src/test/resources/issue2878");
String src = "import java.util.Optional;\n" + "\n"
+ "public class U10 {\n"
+ " private final String file;\n"
+ "\n"
+ " public U10(String file) {\n"
+ " this.file = file;\n"
+ " }\n"
+ " public void main(String[] args) {\n"
+ " U9 u1 = new U9(Optional.empty(), Optional.empty(), 1); // failed\n"
+ " U9 u2 = new U9(Optional.of(file), Optional.empty(), 1); // success\n"
+ " U9 u3 = new U9(Optional.empty(), Optional.empty(), \"/\"); // success\n"
+ " U9 u4 = new U9(Optional.empty(), Optional.empty(), true); // success\n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
CombinedTypeSolver cts = new CombinedTypeSolver(
new ReflectionTypeSolver(false), new JavaParserTypeSolver(rootSourceDir.toFile()));
config.setSymbolResolver(new JavaSymbolSolver(cts));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(src);
List<ObjectCreationExpr> oces = cu.findAll(ObjectCreationExpr.class);
assertEquals(
"U9.U9(java.util.Optional<java.lang.String>, java.util.Optional<U9>, int)",
oces.get(0).resolve().getQualifiedSignature());
assertEquals(
"U9.U9(java.util.Optional<java.lang.String>, java.util.Optional<U9>, int)",
oces.get(1).resolve().getQualifiedSignature());
assertEquals(
"U9.U9(java.util.Optional<java.lang.String>, java.util.Optional<java.lang.String>, java.lang.String)",
oces.get(2).resolve().getQualifiedSignature());
assertEquals(
"U9.U9(java.util.Optional<U9>, java.util.Optional<U9>, boolean)",
oces.get(3).resolve().getQualifiedSignature());
}
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.nio.file.Path;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2909Test extends AbstractResolutionTest {
@Test
void testResolvingLocallyFromCompleteReferenceToInnerClass() {
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
String s = "public class Program {\n" + "\n"
+ " public class OuterClass {\n"
+ " int field = 0;\n"
+ "\n"
+ " public class InnerClass {\n"
+ " InnerClass() {\n"
+ " OuterClass outer = Program.OuterClass.this;\n"
+ " Program.OuterClass.this.field = 1;\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
List<ThisExpr> exprs = cu.findAll(ThisExpr.class);
exprs.forEach(expr -> {
assertEquals("Program.OuterClass", expr.calculateResolvedType().describe());
});
}
@Test
void testResolvingLocallyFromPartialReferenceToInnerClass() {
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
String s = "public class Program {\n" + "\n"
+ " public class OuterClass {\n"
+ " int field = 0;\n"
+ "\n"
+ " public class InnerClass {\n"
+ " InnerClass() {\n"
+ " OuterClass outer = OuterClass.this;\n"
+ " OuterClass.this.field = 1;\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
List<ThisExpr> exprs = cu.findAll(ThisExpr.class);
exprs.forEach(expr -> {
assertEquals("Program.OuterClass", expr.calculateResolvedType().describe());
});
}
@Test
void testInDepth() {
Path rootSourceDir = adaptPath("src/test/resources/issue2909");
ParserConfiguration config = new ParserConfiguration();
CombinedTypeSolver cts = new CombinedTypeSolver(
new ReflectionTypeSolver(false), new JavaParserTypeSolver(rootSourceDir.toFile()));
config.setSymbolResolver(new JavaSymbolSolver(cts));
StaticJavaParser.setConfiguration(config);
String s = "package test;\n" + "\n"
+ "public class Program {\n"
+
// "\n" +
// " public class OuterClass {\n" +
// " }\n" +
"\n"
+ " public class FarOuterClass {\n"
+ "\n"
+ " public class OuterClass {\n"
+ " int field = 0;\n"
+ "\n"
+ " public class InnerClass {\n"
+ " InnerClass() {\n"
+ " // Different cases to refer to enclosing type\n"
+ " OuterClass outer1 = OuterClass.this; // case1\n"
+ " OuterClass.this.field = 1; // case1\n"
+ " OuterClass outer2 = FarOuterClass.OuterClass.this; // case2\n"
+ " FarOuterClass.OuterClass.this.field = 1; // case2\n"
+ " OuterClass outer3 = Program.FarOuterClass.OuterClass.this; // case3\n"
+ " Program.FarOuterClass.OuterClass.this.field = 1; // case3\n"
+ " OuterClass outer4 = test.Program.FarOuterClass.OuterClass.this; // case4\n"
+ " test.Program.FarOuterClass.OuterClass.this.field = 1; // case4\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "\n"
+ " public class OuterClass {\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(s);
List<ThisExpr> exprs = cu.findAll(ThisExpr.class);
exprs.forEach(expr -> {
assertEquals(
"test.Program.FarOuterClass.OuterClass",
expr.calculateResolvedType().describe());
});
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue2943Test {
@Test
public void testPeek() {
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse("package foo;\n"
+ "import java.util.stream.Collectors;\n"
+ "import java.util.stream.IntStream;\n"
+ "import java.util.stream.Stream;\n"
+ "public class TestPeek {\n"
+ " public void foo() {\n"
+ " Stream.of(1,2,3).peek(info -> { System.out.println(info); }).collect(Collectors.toList());\n"
+ " }\n"
+ "}");
for (MethodCallExpr methodCallExpr : cu.findAll(MethodCallExpr.class)) {
assertDoesNotThrow(methodCallExpr::resolve);
}
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.resolution.types.ResolvedArrayType;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.resolution.types.parametrization.ResolvedTypeParametersMap;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import java.util.Optional;
import org.junit.jupiter.api.Test;
public class Issue2951Test {
@Test
public void testResolveListOfByteArray() throws IOException {
ParserConfiguration config = new ParserConfiguration();
CombinedTypeSolver typeResolver = new CombinedTypeSolver(new ReflectionTypeSolver(false));
typeResolver.add(new JarTypeSolver("src/test/resources/issue2951/a.jar"));
config.setSymbolResolver(new JavaSymbolSolver(typeResolver));
StaticJavaParser.setConfiguration(config);
ResolvedReferenceTypeDeclaration clazzA = typeResolver.solveType("foo.A");
Optional<ResolvedMethodDeclaration> optionalMethod = clazzA.getDeclaredMethods().stream()
.filter(m -> m.getName().equals("get"))
.findFirst();
assertTrue(optionalMethod.isPresent());
ResolvedMethodDeclaration method = optionalMethod.get();
ResolvedType paramType = method.getParam(0).getType();
assertTrue(paramType.isReferenceType());
ResolvedReferenceType referenceType = paramType.asReferenceType();
ResolvedTypeParametersMap typeParametersMap = referenceType.typeParametersMap();
ResolvedType type = typeParametersMap.getTypes().get(0);
assertTrue(type instanceof ResolvedArrayType);
}
@Test
public void testIssue2951() throws IOException {
ParserConfiguration config = new ParserConfiguration();
CombinedTypeSolver typeResolver = new CombinedTypeSolver(new ReflectionTypeSolver(false));
typeResolver.add(new JarTypeSolver("src/test/resources/issue2951/a.jar"));
config.setSymbolResolver(new JavaSymbolSolver(typeResolver));
StaticJavaParser.setConfiguration(config);
String code = "package foo;\n"
+ "import java.util.List;\n"
+ "import foo.A;\n"
+ "public class Test {\n"
+ " public void foo() {\n"
+ " List<byte[]> keys = new ArrayList<>();\n"
+ " A a = new A();\n"
+ " a.get(keys);\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(code);
for (TypeDeclaration<?> type : cu.getTypes()) {
type.ifClassOrInterfaceDeclaration(classDecl -> {
for (MethodDeclaration method : classDecl.getMethods()) {
method.getBody().ifPresent(body -> {
for (Statement stmt : body.getStatements()) {
for (MethodCallExpr methodCallExpr : stmt.findAll(MethodCallExpr.class)) {
ResolvedMethodDeclaration resolvedMethodCall = methodCallExpr.resolve();
String methodSig = resolvedMethodCall.getQualifiedSignature();
assertEquals("foo.A.get(java.util.List<byte[]>)", methodSig);
}
}
});
}
});
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.model.SymbolReference;
import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
import com.github.javaparser.symbolsolver.javassistmodel.JavassistEnumDeclaration;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.io.IOException;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
public class Issue2953Test {
@Test
public void testResolveMethodOfEnumInheritedFromInterface() throws IOException {
CombinedTypeSolver typeResolver = new CombinedTypeSolver(new ReflectionTypeSolver(false));
typeResolver.add(new JarTypeSolver("src/test/resources/issue2953/a.jar"));
JavassistEnumDeclaration enumA = (JavassistEnumDeclaration) typeResolver.solveType("foo.A");
SymbolReference<ResolvedMethodDeclaration> method =
enumA.solveMethod("equalByCode", Arrays.asList(ResolvedPrimitiveType.INT), false);
assertTrue(method.isSolved());
}
@Test
public void testIssue2953() throws IOException {
ParserConfiguration config = new ParserConfiguration();
CombinedTypeSolver typeResolver = new CombinedTypeSolver(new ReflectionTypeSolver(false));
typeResolver.add(new JarTypeSolver("src/test/resources/issue2953/a.jar"));
config.setSymbolResolver(new JavaSymbolSolver(typeResolver));
StaticJavaParser.setConfiguration(config);
String code = "package foo;\n"
+ "import foo.A;\n"
+ "public class Test {\n"
+ " public void foo() {\n"
+ " A.X.equalByCode(0);\n"
+ " }\n"
+ "}";
CompilationUnit cu = StaticJavaParser.parse(code);
for (TypeDeclaration<?> type : cu.getTypes()) {
type.ifClassOrInterfaceDeclaration(classDecl -> {
for (MethodDeclaration method : classDecl.getMethods()) {
method.getBody().ifPresent(body -> {
for (Statement stmt : body.getStatements()) {
for (MethodCallExpr methodCallExpr : stmt.findAll(MethodCallExpr.class)) {
ResolvedMethodDeclaration resolvedMethodCall = methodCallExpr.resolve();
String methodSig = resolvedMethodCall.getQualifiedSignature();
assertEquals("foo.IB.equalByCode(java.lang.Integer)", methodSig);
}
}
});
}
});
}
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import org.junit.jupiter.api.Test;
public class Issue2987Test extends AbstractResolutionTest {
@Test
void test() {
String code = "class ClassA {\n" + " void f() {\n"
+ " String s = \"\";\n"
+ " String t = (s.length() == 0 ? \"*\" : s) + \"\" ;\n"
+ " }\n"
+ "}";
ParserConfiguration config = new ParserConfiguration();
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
CompilationUnit cu = StaticJavaParser.parse(code);
MethodCallExpr expr = cu.findFirst(MethodCallExpr.class).get();
assertEquals("java.lang.String.length()", expr.resolve().getQualifiedSignature());
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (C) 2013-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import java.util.List;
import org.junit.jupiter.api.Test;
public class Issue2995Test extends AbstractResolutionTest {
@Test
public void test() {
ParserConfiguration config =
new ParserConfiguration().setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
StaticJavaParser.setConfiguration(config);
String str = "public class MyClass {\n" + " class Inner1 {\n"
+ " class Inner2 {\n"
+ " }\n"
+ " }\n"
+ " {\n"
+ " new Inner1().new Inner2();\n"
+ " }\n"
+ "}\n";
CompilationUnit cu = StaticJavaParser.parse(str);
List<ObjectCreationExpr> exprs = cu.findAll(ObjectCreationExpr.class);
assertEquals("new Inner1().new Inner2()", exprs.get(0).toString());
assertEquals("MyClass.Inner1.Inner2", exprs.get(0).getType().resolve().describe());
assertEquals("new Inner1()", exprs.get(1).toString());
assertEquals("MyClass.Inner1", exprs.get(1).getType().resolve().describe());
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2015-2016 Federico Tomassetti
* Copyright (C) 2017-2024 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.symbolsolver;
import static com.github.javaparser.StaticJavaParser.parse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.resolution.Navigator;
import com.github.javaparser.resolution.TypeSolver;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.resolution.model.SymbolReference;
import com.github.javaparser.resolution.types.ResolvedPrimitiveType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.LeanParserConfiguration;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
class Issue300Test extends AbstractResolutionTest {
@Test
void fieldAccessIssue() throws IOException {
Path pathToSourceFile = adaptPath("src/test/resources/issue300/Issue300.java");
CompilationUnit cu = parse(pathToSourceFile);
final FieldAccessExpr fieldAccess = Navigator.demandNodeOfGivenClass(cu, FieldAccessExpr.class);
assertNotNull(fieldAccess);
TypeSolver typeSolver = new CombinedTypeSolver(
new ReflectionTypeSolver(),
new JavaParserTypeSolver(adaptPath("src/test/resources/issue300"), new LeanParserConfiguration()));
final JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
final SymbolReference<? extends ResolvedValueDeclaration> ref = javaParserFacade.solve(fieldAccess);
assertEquals(
ResolvedPrimitiveType.INT,
ref.getCorrespondingDeclaration().getType().asPrimitive());
}
}

Some files were not shown because too many files have changed in this diff Show More