Skip to content

Commit 0dec337

Browse files
committed
handle default package
1 parent 00e11d1 commit 0dec337

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/main/java/io/papermc/typewriter/ClassNamed.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ClassNamed(Class<?> knownClass) {
1818
}
1919

2020
public ClassNamed {
21-
Preconditions.checkArgument(SourceVersion.isName(packageName), "Package name contains syntax errors");
21+
Preconditions.checkArgument(packageName.isEmpty() || SourceVersion.isName(packageName), "Package name contains syntax errors");
2222
Preconditions.checkArgument(SourceVersion.isName(dottedNestedName), "Class name contains syntax errors");
2323
if (knownClass != null) {
2424
Preconditions.checkArgument(!knownClass.isPrimitive(), "Invalid class, primitive types and 'void' type are not allowed");
@@ -29,7 +29,7 @@ public ClassNamed(Class<?> knownClass) {
2929
/**
3030
* Creates a class named object.
3131
*
32-
* @param packageName the package name
32+
* @param packageName the package name or an empty string for the default package
3333
* @param name the class name
3434
* @return the new object
3535
* @apiNote nested classes are delimited by '$' character and
@@ -54,7 +54,7 @@ public static ClassNamed of(String packageName, String name) {
5454
/**
5555
* Creates a class named object.
5656
*
57-
* @param packageName the package name
57+
* @param packageName the package name or an empty string for the default package
5858
* @param name the root class name
5959
* @param nestedNames the nested class names
6060
* @return the new object
@@ -124,6 +124,10 @@ public String canonicalName() {
124124
return this.knownClass.getCanonicalName();
125125
}
126126

127+
if (this.packageName.isEmpty()) {
128+
return this.dottedNestedName;
129+
}
130+
127131
return this.packageName + '.' + this.dottedNestedName;
128132
}
129133

src/main/java/io/papermc/typewriter/utils/ClassHelper.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ public static Class<?> getRootClass(Class<?> clazz) {
1717

1818
public static String retrieveFullNestedName(Class<?> clazz) {
1919
String fqn = clazz.getCanonicalName();
20-
return fqn.substring(clazz.getPackageName().length() + 1);
20+
String packageName = clazz.getPackageName();
21+
if (packageName.isEmpty()) {
22+
return fqn;
23+
}
24+
return fqn.substring(packageName.length() + 1);
2125
}
2226

2327
private ClassHelper() {

0 commit comments

Comments
 (0)