From 456764468add43c298a66ec8fdc12603f4dfeebd Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Tue, 20 Nov 2012 03:50:44 +0100 Subject: [PATCH] Always generate classes with legal identifiers. Certain types, such as ItemStack[], would cause the StructureCompiler to generate classes with the name CompiledStructure@ParentItemStack[], which are not legal names. Instead, we'll replace the brackets with the word Array. In addition, to accomodate classes with identical names, we'll use the following naming convention instead: CompiledStructure$[Canonical name of target]$Canonical name of type], where the canonical name (net.minecraft.server.ItemStack[]) is transformed to a legal name by replacing "." to "_" and "[]" to array. In our example, that would result in the following class name: net_minecraft_server_ItemStackArray --- .../reflect/compiler/StructureCompiler.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/StructureCompiler.java b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/StructureCompiler.java index 266bb45e..87175da6 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/StructureCompiler.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/reflect/compiler/StructureCompiler.java @@ -186,6 +186,15 @@ public final class StructureCompiler { } } + /** + * Retrieve a variable identifier that can uniquely represent the given type. + * @param type - a type. + * @return A unique and legal identifier for the given type. + */ + private String getSafeTypeName(Class type) { + return type.getCanonicalName().replace("[]", "Array").replace(".", "_"); + } + private Class generateClass(StructureModifier source) { ClassWriter cw = new ClassWriter(0); @@ -193,7 +202,9 @@ public final class StructureCompiler { @SuppressWarnings("rawtypes") Class targetType = source.getTargetType(); - String className = "CompiledStructure$" + targetType.getSimpleName() + source.getFieldType().getSimpleName(); + String className = "CompiledStructure$" + + getSafeTypeName(targetType) + "$" + + getSafeTypeName(source.getFieldType()); String targetSignature = Type.getDescriptor(targetType); String targetName = targetType.getName().replace('.', '/');