Extra dot in type name
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
Description
So I wrote some code that basically remaps some files
`SourceAnalysis.java`
```java
public class SourceAnalysis {
public static String remapClass(String source, Mappings mappings) {
CtClass ctClass = Launcher.parseClass(source);
CtModel ctModel = ctClass.getFactory().getModel();
CtType ctType = ctModel.getRootPackage().getElements(new TypeFilter<>(CtType.class)).get(0);
CtCompilationUnit unit = ctType.getFactory().CompilationUnit().getOrCreate(ctType);
ModelList imports = unit.getImports();
// rename class in import statement
for(CtImport ctImport : imports) {
CtUnresolvedImport imp = (CtUnresolvedImport) ctImport;
if(mappings.mapClass(imp.getUnresolvedReference().replace(".", "/")) != null)
imp.setUnresolvedReference(mappings.mapClass(imp.getUnresolvedReference().replace(".", "/")).replace("/", "."));
}
List list = ctModel.getRootPackage().getElements(new TypeFilter<>(CtTypedElement.class));
for(CtTypedElement e : list) {
String name = e.getType().getQualifiedName();
if(mappings.mapClass(name.replace(".", "/")) != null) {
e.setType(Utils.remapRef(
e.getType(),
mappings.mapClass(e.getType().getQualifiedName().replace(".", "/")).replace("/", "."),
CtTypeReference::setPackage
));
}
}
return Utils.modelToString(ctModel, ctType);
}
}
```
`Utils.java`
```java
class Utils {
public static String modelToString(CtModel ctModel, CtType ctType) {
HashSet set = new HashSet<>();
StringBuffer sb = new StringBuffer();
DefaultJavaPrettyPrinter defaultJavaPrettyPrinter = new DefaultJavaPrettyPrinter(new Launcher().getEnvironment());
for(CtType type : ctModel.getElements(new TypeFilter<>(CtType.class))){
CtCompilationUnit u = ctType.getFactory().CompilationUnit().getOrCreate(type);
if(set.contains(u.getDeclaredTypeReferences().get(0).getQualifiedName()))
continue; // otherwise inner classes will cause duplicate compilation units in the buffer
sb.append(defaultJavaPrettyPrinter.printCompilationUnit(u) + "\n\n");
for(CtTypeReference reference : u.getDeclaredTypeReferences()){
set.add(reference.getQualifiedName());
}
}
return sb.toString();
}
public static T remapRef(T ref,
String newName,
BiConsumer setPackage
) {
int i = newName.lastIndexOf('.');
setPackage.accept(ref, new Launcher().getFactory().Package().createReference(newName.substring(0, i)));
ref.setSimpleName(newName.substring(i));
return ref;
}
}
```
`MethodSignature.java`
```java
public class MethodSignature {
List params;
String returnValue;
private static Map JVM_TYPES = ImmutableMap.copyOf(
Stream.of(
new AbstractMap.SimpleEntry<>("boolean","Z"),
new AbstractMap.SimpleEntry<>("byte", "B"),
new AbstractMap.SimpleEntry<>("char", "C"),
new AbstractMap.SimpleEntry<>("short", "S"),
new AbstractMap.SimpleEntry<>("int", "I"),
new AbstractMap.SimpleEntry<>("long", "J"),
new AbstractMap.SimpleEntry<>("float", "F"),
new AbstractMap.SimpleEntry<>("double", "D")
).collect(Collectors.toMap(AbstractMap.SimpleEntry::getKey, AbstractMap.SimpleEntry::getValue))
);
private static void appendType(String str, StringBuilder b) {
int c = StringUtils.countMatches(str, "[]");
str = str .replace("[]", "");
if(JVM_TYPES.containsKey(str))
str = JVM_TYPES.get(str);
else
str = "L" + str .replace(".", "/") + ";";
b.append(StringUtils.repeat('[', c)).append(str);
}
public static String sourceToJVM(MethodSignature source) {
StringBuilder b = new StringBuilder("(");
source.params.forEach(i -> {
appendType(i, b);
});
b.append(")");
appendType(source.returnValue, b);
return b.toString();
}
}
```
So the internal string buffer is:
```java
import com.example.Test;
import com.example.TestArg;
public class Example {
void main(String... args) {
TestArg m = Test.create();
}
}
```
But the output is:
```java
import com.floweytf.Test;
import com.floweytf.TestArg;
public class Example {
void main(java.lang.String... args) {
com.floweytf..TestArg m = com.example.Test.create();
}
}
```
Why is there an extra dot??
Contributor guide
Assessment
This issue has not been assessed yet.