Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

review: fix: Refactor code to fix runtime exception caused by package name with CtType.INNERTTYPE_SEPARATOR #5237

Merged
merged 6 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/main/java/spoon/reflect/factory/PackageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import spoon.reflect.declaration.CtModule;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtPackageDeclaration;
import spoon.reflect.declaration.CtType;
import spoon.reflect.reference.CtPackageReference;


Expand Down Expand Up @@ -155,9 +154,6 @@ public CtPackage getOrCreate(String qualifiedName, CtModule rootModule) {
* @return a found package or null
*/
public CtPackage get(String qualifiedName) {
if (qualifiedName.contains(CtType.INNERTTYPE_SEPARATOR)) {
throw new RuntimeException("Invalid package name " + qualifiedName);
}

// Find package with the most contained types. If a module exports package "foo.bar" and the
// other "foo.bar.baz", *both modules* will contain a "foo.bar" package in spoon. As
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/spoon/reflect/factory/PackageFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import spoon.Launcher;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtPackage;
import spoon.testing.utils.GitHubIssue;

Expand All @@ -29,4 +31,20 @@ void getOrCreate_returnsNestedPackageStructure_whenQualifiedNameRepeatsSimpleNam
assertThat(topLevelPackage.getPackage(topLevelPackageName), sameInstance(packageWithDuplicatedSimpleNames));
assertThat(packageWithDuplicatedSimpleNames.getParent(), sameInstance(topLevelPackage));
}

@Test
@GitHubIssue(issueNumber = 5140, fixed = true)
void testGetPackageWithNameContainingDollarSign() {
// contract: A package with a name containing a dollar sign can be retrieved using the PackageFactory
// Create a package with a name containing a dollar sign
String packageName = "com.example.package$with$dollar$sign";
CtClass<?> clazz = Launcher.parseClass("package " + packageName + ";" + "\n" + "enum Foo { }");

// Get the package using the PackageFactory
CtPackage ctPackage = clazz.getFactory().Package().get(packageName);

// Verify that the package was found
assertNotNull(ctPackage);
assertEquals(packageName, ctPackage.getQualifiedName());
}
}