Guest User

Untitled

a guest
Jul 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. package org.warbo.updater.generic;
  2.  
  3. import com.sun.org.apache.bcel.internal.Constants;
  4. import com.sun.org.apache.bcel.internal.classfile.Field;
  5. import com.sun.org.apache.bcel.internal.classfile.Method;
  6. import com.sun.org.apache.bcel.internal.generic.*;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10. import java.util.Map;
  11.  
  12. /**
  13. * Created by IntelliJ IDEA.
  14. * User: Warbo
  15. * Date: 03-Dec-2009
  16. * Time: 17:53:47
  17. * To change this template use File | Settings | File Templates.
  18. */
  19. public abstract class Transform {
  20. private static String hooksPackage = "org.warbo.updater.accessors.";
  21.  
  22. public HashMap<String, ClassGen> myClasses = new HashMap<String, ClassGen>();
  23. protected static HashMap<String, String> interfaceMap = new HashMap<String, String>();
  24. public HashMap<ClassGen, Identifier> myIdents = new HashMap<ClassGen, Identifier>();
  25.  
  26.  
  27. public Transform(HashMap<String, ClassGen> myClasses) {
  28. this.myClasses = myClasses;
  29. }
  30.  
  31. protected abstract boolean processTransform(ClassGen cg);
  32.  
  33. public boolean execute() {
  34. for (ClassGen cg : myClasses.values()) {
  35. if (identify(cg)) {
  36. if (processTransform(cg)) {
  37. return true;
  38. }
  39. }
  40. }
  41. return false;
  42. }
  43.  
  44. protected void addInterface(ClassGen cg, String i, String comment) {
  45. cg.addInterface(hooksPackage + i);
  46. if (!i.equals("Client") && !interfaceMap.containsKey(i))
  47. interfaceMap.put(i, cg.getClassName());
  48. System.out.println(" > " + cg.getClassName() + " implements " + hooksPackage + i);
  49. }
  50.  
  51. protected void addGetter(ClassGen cg, Field f, String name) {
  52. try {
  53. Type retType = f.getType();
  54. if (retType.getSignature().endsWith(";")) {
  55. String classType = retType.toString().split("\\[")[0];
  56. if (interfaceMap.containsValue(classType))
  57. for (Map.Entry<String, String> s : interfaceMap.entrySet())
  58. if (s.getValue().equals(classType)) {
  59. retType = Type.getType(retType.getSignature().split("L")[0] + "L" +
  60. (hooksPackage + s.getKey()).replace('.', '/') + ";");
  61. break;
  62. }
  63. }
  64. ClassGen injCG = cg;
  65. if (f.isStatic())
  66. injCG = myClasses.get(hooksPackage + "client");
  67. ConstantPoolGen cpg = injCG.getConstantPool();
  68. InstructionList il = new InstructionList();
  69. MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, retType, Type.NO_ARGS, null,
  70. name, injCG.getClassName(), il, cpg);
  71. InstructionFactory instrF = new InstructionFactory(injCG, cpg);
  72. if (!f.isStatic()) il.append(new ALOAD(0));
  73. il.append(instrF.createFieldAccess(cg.getClassName(), f.getName(), f.getType(),
  74. (f.isStatic() ? Constants.GETSTATIC : Constants.GETFIELD)));
  75. il.append(InstructionFactory.createReturn(retType));
  76. mg.setMaxLocals();
  77. mg.setMaxStack();
  78. injCG.addMethod(mg.getMethod());
  79. System.out.println(" * " + name + "() > " + (f.isStatic() ? "static" : "field") + " " + f.getType() + " " + cg.getClassName() + "." + f.getName());
  80. } catch (Exception e) {
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. protected ArrayList<Field> getInstanceFields(ClassGen classGen) {
  86. ArrayList<Field> fields = new ArrayList<Field>();
  87. for (Field field : classGen.getFields())
  88. if (!field.isStatic())
  89. fields.add(field);
  90. return fields;
  91. }
  92.  
  93. protected ArrayList<Method> getInstanceMethods(ClassGen classGen) {
  94. ArrayList<Method> methods = new ArrayList<Method>();
  95. for (Method method : classGen.getMethods())
  96. if (!method.isStatic() && !method.getName().equals("<init>"))
  97. methods.add(method);
  98. return methods;
  99. }
  100.  
  101. public ClassGen getClass(String className) {
  102. return myClasses.get(className);
  103. }
  104. }
  105.  
  106. # Ident Class
  107.  
  108. package org.warbo.updater.generic;
  109.  
  110. import com.sun.org.apache.bcel.internal.generic.ClassGen;
  111.  
  112. /**
  113. * Created by IntelliJ IDEA.
  114. * User: Warbo
  115. * Date: 03-Dec-2009
  116. * Time: 17:54:44
  117. * To change this template use File | Settings | File Templates.
  118. */
  119. public abstract class Identifier {
  120. public abstract boolean identify(ClassGen cg);
  121. }
  122.  
  123. # Sample Class.
  124.  
  125. package org.warbo.updater.identifiers;
  126.  
  127. import com.sun.org.apache.bcel.internal.generic.ClassGen;
  128. import org.warbo.updater.generic.Identifier;
  129.  
  130. /**
  131. * Created by IntelliJ IDEA.
  132. * User: Warbo
  133. * Date: 03-Dec-2009
  134. * Time: 18:06:42
  135. * To change this template use File | Settings | File Templates.
  136. */
  137. public class FileOnDisk extends Identifier {
  138. @Override
  139. public boolean identify(ClassGen cg) {
  140. return (cg.getConstantPool().lookupString("Warning! fileondisk ") > 0); //To change body of implemented methods use File | Settings | File Templates.
  141. }
  142. }
  143.  
  144. # Sample Transform
  145.  
  146. package org.warbo.updater.transforms;
  147.  
  148. import com.sun.org.apache.bcel.internal.generic.ClassGen;
  149. import org.warbo.updater.generic.Transform;
  150.  
  151. import java.util.HashMap;
  152.  
  153. /**
  154. * Created by IntelliJ IDEA.
  155. * User: Warbo
  156. * Date: 03-Dec-2009
  157. * Time: 18:06:35
  158. * To change this template use File | Settings | File Templates.
  159. */
  160. public class FileOnDisk extends Transform {
  161. public FileOnDisk(HashMap<String, ClassGen> myClasses) {
  162. super(myClasses);
  163. }
  164.  
  165. @Override
  166. protected boolean processTransform(ClassGen cg) {
  167. return false; //To change body of implemented methods use File | Settings | File Templates.
  168. }
  169. }
Add Comment
Please, Sign In to add comment