Recent Posts
None | 23 sec ago
Python | 27 sec ago
None | 28 sec ago
None | 34 sec ago
None | 36 sec ago
ActionScript 3 | 1 min ago
XML | 1 min ago
None | 1 min ago
JavaScript | 1 min ago
None | 2 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 9th of Feb 2010 09:01:06 PM Download | Raw | Embed | Report
  1. //----------------------------------- Injectable.java
  2.  
  3. package com.zetabot.updater.objects;
  4.  
  5. import org.jdom.Document;
  6.  
  7. /**
  8.  * User: Johan
  9.  * Date: 2010-feb-09
  10.  * Time: 18:15:33
  11.  */
  12. public interface Injectable {
  13.  
  14.     public void inject();
  15.  
  16.     public void toXml(Document doc);
  17.  
  18. }
  19.  
  20. //----------------------------------- InterfaceInjector.java
  21.  
  22. package com.zetabot.updater.objects;
  23.  
  24. import com.zetabot.updater.Updater;
  25. import org.apache.bcel.generic.ClassGen;
  26. import org.apache.log4j.Logger;
  27. import org.jdom.Document;
  28. import org.jdom.Element;
  29.  
  30. /**
  31.  * User: Johan
  32.  * Date: 2010-feb-09
  33.  * Time: 18:15:21
  34.  */
  35. public class InterfaceInjector implements Injectable {
  36.  
  37.     private static final Logger log = Logger.getLogger(InterfaceInjector.class);
  38.  
  39.     private static final String PACKAGE_NAME = "com.zetabot.hooks.";
  40.  
  41.     private ClassGen cg;
  42.     private String name;
  43.  
  44.     public InterfaceInjector(ClassGen cg, String name) {
  45.         this.cg = cg;
  46.         this.name = name;
  47.     }
  48.  
  49.     public ClassGen getClazz() {
  50.         return cg;
  51.     }
  52.  
  53.     public void inject() {
  54.         cg.addInterface(name);
  55.         log.info("Interface " + PACKAGE_NAME + name + " -> " + cg.getClassName());
  56.         Updater.interfaceCount++;
  57.     }
  58.  
  59.     public void toXml(Document doc) {
  60.         Element iface = new Element("Class");
  61.         iface.setAttribute("name", name);
  62.         iface.setAttribute("class", cg.getClassName());
  63.         doc.getRootElement().addContent(iface);
  64.     }
  65.  
  66. }
  67.  
  68. //----------------------------------- MemberInjector.java
  69.  
  70. package com.zetabot.updater.objects;
  71.  
  72. import com.zetabot.updater.Updater;
  73. import org.apache.bcel.Constants;
  74. import org.apache.bcel.classfile.Field;
  75. import org.apache.bcel.generic.*;
  76. import org.apache.log4j.Logger;
  77. import org.jdom.Document;
  78. import org.jdom.Element;
  79.  
  80. import java.util.List;
  81.  
  82. /**
  83.  * User: Johan
  84.  * Date: 2010-feb-09
  85.  * Time: 18:15:14
  86.  */
  87. public class MemberInjector implements Injectable {
  88.  
  89.     private static final Logger log = Logger.getLogger(MemberInjector.class);
  90.  
  91.     private ClassGen target;
  92.     private Field field;
  93.     private String name;
  94.     private Type returnType;
  95.  
  96.     public MemberInjector(String name, ClassGen target, Field field, Type returnType) {
  97.         this.name = name;
  98.         this.target = target;
  99.         this.field = field;
  100.         this.returnType = returnType;
  101.     }
  102.  
  103.     public ClassGen getTarget() {
  104.         return target;
  105.     }
  106.      
  107.     public void inject() {
  108.         if(field == null || target == null) {
  109.             log.info("Broken: " + name + "()");
  110.             return;
  111.         }
  112.         InstructionFactory ifactory = new InstructionFactory(target.getConstantPool());
  113.         InstructionList il = new InstructionList();
  114.         MethodGen methodGen = new MethodGen(Constants.ACC_PUBLIC, returnType, Type.NO_ARGS, new String[]{}, name, target.getClassName(), il, target.getConstantPool());
  115.         il.append(new ALOAD(0));
  116.         il.append(ifactory.createFieldAccess(target.getClassName(), field.getName(), field.getType(), Constants.GETFIELD));
  117.         il.append(InstructionFactory.createReturn(field.getType()));
  118.         methodGen.setMaxLocals();
  119.         methodGen.setMaxStack();
  120.         target.addMethod(methodGen.getMethod());
  121.         log.info("Field " + name + "() -> " + field.getType() + " " + target.getClassName() + "." + field.getName());
  122.         Updater.fieldCount++;
  123.     }
  124.  
  125.     public void toXml(Document doc) {
  126.         if(field == null || target == null) {
  127.             return;
  128.         }
  129.         List list = doc.getRootElement().getChildren("Class");
  130.         int elementIndex = 0;
  131.         Element[] elements = (Element[]) list.toArray(new Element[list.size()]);
  132.         for(int i = 0; i < elements.length; i++) {
  133.             if(elements[i].getAttribute("class").getValue().equals(target.getClassName())) {
  134.                 elementIndex = i;
  135.             }
  136.         }
  137.         String fixedId = name;
  138.         if(name.contains("get")) {
  139.             fixedId = name.split("get")[1];
  140.             String firstChar = fixedId.substring(0, 1);
  141.             String fixedChar = firstChar.toLowerCase();
  142.             fixedId = fixedId.replaceFirst(firstChar, fixedChar);
  143.         }
  144.         Element val = new Element("Member");
  145.         val.setAttribute("id", fixedId);
  146.         val.setAttribute("name", field.getName());
  147.         val.setAttribute("type", field.getType().toString());
  148.         ((Element)doc.getRootElement().getChildren().get(elementIndex)).addContent(val);
  149.     }
  150.  
  151. }
  152.  
  153. //----------------------------------- StaticMemberInjector.java
  154.  
  155. package com.zetabot.updater.objects;
  156.  
  157. import com.zetabot.updater.Updater;
  158. import org.apache.bcel.Constants;
  159. import org.apache.bcel.classfile.Field;
  160. import org.apache.bcel.generic.*;
  161. import org.apache.log4j.Logger;
  162. import org.jdom.Document;
  163. import org.jdom.Element;
  164.  
  165. /**
  166.  * User: Johan
  167.  * Date: 2010-feb-09
  168.  * Time: 18:13:13
  169.  */
  170. public class StaticMemberInjector implements Injectable {
  171.  
  172.     private static final Logger log = Logger.getLogger(StaticMemberInjector.class);
  173.  
  174.  
  175.     private ClassGen clazz;
  176.     private Field field;
  177.     private Type returnType;
  178.     private String name;
  179.  
  180.     public StaticMemberInjector(String name, ClassGen clazz, Field field, Type returnType) {
  181.         this.name = name;
  182.         this.clazz = clazz;
  183.         this.field = field;
  184.         this.returnType = returnType;
  185.     }
  186.  
  187.     public void inject() {
  188.         if(field == null || clazz == null) {
  189.             log.info("Broken: " + name + "()");
  190.             return;
  191.         }
  192.         ClassGen client = Updater.getClassByName("client");
  193.  
  194.         InstructionFactory ifactory = new InstructionFactory(client, client.getConstantPool());
  195.         InstructionList il = new InstructionList();
  196.  
  197.         MethodGen methodGen = new MethodGen(Constants.ACC_PUBLIC, returnType, Type.NO_ARGS, new String[]{}, name, client.getClassName(), il, client.getConstantPool());
  198.         il.append(ifactory.createFieldAccess(clazz.getClassName(), field.getName(), field.getType(), Constants.GETSTATIC));
  199.         il.append(InstructionFactory.createReturn(field.getType()));
  200.         methodGen.setMaxLocals();
  201.         methodGen.setMaxStack();
  202.         client.addMethod(methodGen.getMethod());
  203.         log.info("Static Field " + name + "() -> " + field.getType() + " " + clazz.getClassName() + "." + field.getName());
  204.         Updater.staticCount++;
  205.     }
  206.  
  207.     public void toXml(Document doc) {
  208.         Element data = new Element("Static");
  209.         String fixedId = name;
  210.         if(name.contains("get")) {
  211.             fixedId = name.split("get")[1];
  212.             String firstChar = fixedId.substring(0, 1);
  213.             String fixedChar = firstChar.toLowerCase();
  214.             fixedId = fixedId.replaceFirst(firstChar, fixedChar);
  215.         }
  216.         data.setAttribute("id", fixedId);
  217.         data.setAttribute("name", field.getName());
  218.         data.setAttribute("class", clazz.getClassName());
  219.         data.setAttribute("type", field.getType().toString());
  220.         doc.getRootElement().getChild("Static").addContent(data);
  221.     }
  222.  
  223. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: