Created
July 27, 2014 14:58
-
-
Save FormallyMyles/ab29034666eb3afd1f58 to your computer and use it in GitHub Desktop.
Code used to generate classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.myles.trapexamples; | |
import java.io.*; | |
import java.lang.reflect.Method; | |
import java.lang.reflect.Modifier; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class InterfaceGenerator { | |
private static File dir = new File("D:\\classes"); | |
public static void main(String[] args) throws IOException { | |
createInterface(net.minecraft.server.v1_7_R3.EntityPlayer.class); | |
} | |
public static int uniqueHash(Class[] a) { | |
int h = 0; | |
for (Class s : a) | |
h += s.hashCode(); | |
return h; | |
} | |
public static File getHighestNotFound(File highestNotFound) { | |
while (!highestNotFound.exists()) { | |
if (highestNotFound.getParentFile().exists()) | |
return highestNotFound; | |
highestNotFound = highestNotFound.getParentFile(); | |
} | |
return null; | |
} | |
public static boolean createInterface(Class c) throws IOException { | |
if (c.isArray()) c = c.getComponentType(); | |
if (!c.getName().startsWith("net.minecraft") && !c.getName().startsWith("org.bukkit.craftbukkit.v")) return false; | |
String type = c.getName().startsWith("net.minecraft") ? "nms" : "cb"; | |
String folder = type.equals("nms") ? "" : c.getPackage().getName().replaceAll("org\\.bukkit\\.craftbukkit\\..*\\.", "").replace('.', '\\'); | |
if (folder.startsWith("org\\bukkit")) folder = ""; | |
File dir = new File("D:\\classes\\" + type + "\\" + folder + "\\"); | |
while (!dir.exists()) { | |
getHighestNotFound(dir).mkdir(); | |
} | |
System.out.println(dir.toString()); | |
File file = new File(dir, c.getSimpleName() + ".java"); | |
if (file.exists()) return true; | |
System.out.println("Creating interface for " + c.getSimpleName()); | |
file.createNewFile(); | |
PrintWriter pw = new PrintWriter(file); | |
pw.println("package us.myles." + type + ";"); | |
pw.println(); | |
Map<String, Integer> methodNames2 = new HashMap<>(); | |
boolean multiple = false; | |
for (Method m : c.getDeclaredMethods()) { | |
int hash = uniqueHash(m.getParameterTypes()); | |
methodNames2.put(m.getName() + hash, methodNames2.containsKey(m.getName() + hash) ? methodNames2.get(m.getName() + hash) + 1 : 1); | |
if (methodNames2.get(m.getName() + hash) > 1) | |
multiple = true; | |
} | |
pw.println("import us.myles.classtrap.TrapTag;"); | |
if (multiple) | |
pw.println("import us.myles.classtrap.TrapTagType;"); | |
pw.println(); | |
pw.println("//" + c.getName()); | |
pw.println("@TrapTag"); | |
pw.println("public interface " + c.getSimpleName() + " {"); | |
Map<String, Integer> methodNames = new HashMap<>(); | |
for (Method m : c.getDeclaredMethods()) { | |
if (Modifier.isPublic(m.getModifiers()) && !Modifier.isStatic(m.getModifiers())) { | |
int hash = uniqueHash(m.getParameterTypes()); | |
int currentCount = methodNames.containsKey(m.getName() + hash) ? methodNames.get(m.getName() + hash) : 0; | |
if (currentCount != 0) { | |
pw.println("\t@TrapTag(type = TrapTagType.METHOD, value = \"" + m.getName() + "\")"); | |
} | |
pw.print("\tpublic "); | |
if (m.getReturnType() != null) { | |
boolean createdInterface = createInterface(m.getReturnType()); | |
pw.print(m.getReturnType().isArray() ? (createdInterface ? getNewDeclaration(m.getReturnType().getComponentType(), type) : m.getReturnType().getComponentType().getName()).replace("$", ".") + "[]" : | |
(createdInterface ? getNewDeclaration(m.getReturnType(), type) : m.getReturnType().getName()).replace("$", ".")); | |
} else { | |
pw.print("void"); | |
} | |
pw.print(" " + (currentCount != 0 ? m.getName() + (currentCount + 1) : m.getName()) + "("); | |
if (m.getParameterTypes().length != 0) { | |
String params = ""; | |
int x = 0; | |
for (Class mc : m.getParameterTypes()) { | |
boolean createdInterface = createInterface(mc); | |
params = params + (params.length() != 0 ? ", " : "") + (mc.isArray() ? (createdInterface ? getNewDeclaration(mc.getComponentType(), type) : mc.getComponentType().getName()) + "[]" : | |
(createdInterface ? getNewDeclaration(mc, type) : mc | |
.getName())).replace("$", ".") + " arg" + x++; | |
} | |
pw.write(params); | |
} | |
pw.print(");"); | |
pw.println(); | |
methodNames.put(m.getName() + hash, methodNames.containsKey(m.getName() + hash) ? methodNames.get(m.getName() + hash) + 1 : 1); | |
} | |
} | |
pw.println("}"); | |
pw.close(); | |
return true; | |
} | |
private static String getNewDeclaration(Class clazz, String currentType) { | |
String type = clazz.getName().startsWith("net.minecraft") ? "nms" : "cb"; | |
if (clazz == null) return ""; | |
if (clazz.getPackage() == null) return ""; | |
String folder = type.equals("nms") ? "" : clazz.getPackage().getName().replaceAll("org\\.bukkit\\.craftbukkit\\..*\\.", ""); | |
if (folder.startsWith("org.bukkit")) folder = ""; | |
if (!currentType.equals(type) || type.equals("cb")) return "us.myles." + type + "." + folder + (folder.length() == 0 ? "" : ".") + clazz.getSimpleName(); | |
return clazz.getSimpleName(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment