Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.warbo.updater.generic;
- import com.sun.org.apache.bcel.internal.Constants;
- import com.sun.org.apache.bcel.internal.classfile.Field;
- import com.sun.org.apache.bcel.internal.classfile.Method;
- import com.sun.org.apache.bcel.internal.generic.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by IntelliJ IDEA.
- * User: Warbo
- * Date: 03-Dec-2009
- * Time: 17:53:47
- * To change this template use File | Settings | File Templates.
- */
- public abstract class Transform {
- private static String hooksPackage = "org.warbo.updater.accessors.";
- public HashMap<String, ClassGen> myClasses = new HashMap<String, ClassGen>();
- protected static HashMap<String, String> interfaceMap = new HashMap<String, String>();
- public HashMap<ClassGen, Identifier> myIdents = new HashMap<ClassGen, Identifier>();
- public Transform(HashMap<String, ClassGen> myClasses) {
- this.myClasses = myClasses;
- }
- protected abstract boolean processTransform(ClassGen cg);
- public boolean execute() {
- for (ClassGen cg : myClasses.values()) {
- if (identify(cg)) {
- if (processTransform(cg)) {
- return true;
- }
- }
- }
- return false;
- }
- protected void addInterface(ClassGen cg, String i, String comment) {
- cg.addInterface(hooksPackage + i);
- if (!i.equals("Client") && !interfaceMap.containsKey(i))
- interfaceMap.put(i, cg.getClassName());
- System.out.println(" > " + cg.getClassName() + " implements " + hooksPackage + i);
- }
- protected void addGetter(ClassGen cg, Field f, String name) {
- try {
- Type retType = f.getType();
- if (retType.getSignature().endsWith(";")) {
- String classType = retType.toString().split("\\[")[0];
- if (interfaceMap.containsValue(classType))
- for (Map.Entry<String, String> s : interfaceMap.entrySet())
- if (s.getValue().equals(classType)) {
- retType = Type.getType(retType.getSignature().split("L")[0] + "L" +
- (hooksPackage + s.getKey()).replace('.', '/') + ";");
- break;
- }
- }
- ClassGen injCG = cg;
- if (f.isStatic())
- injCG = myClasses.get(hooksPackage + "client");
- ConstantPoolGen cpg = injCG.getConstantPool();
- InstructionList il = new InstructionList();
- MethodGen mg = new MethodGen(Constants.ACC_PUBLIC, retType, Type.NO_ARGS, null,
- name, injCG.getClassName(), il, cpg);
- InstructionFactory instrF = new InstructionFactory(injCG, cpg);
- if (!f.isStatic()) il.append(new ALOAD(0));
- il.append(instrF.createFieldAccess(cg.getClassName(), f.getName(), f.getType(),
- (f.isStatic() ? Constants.GETSTATIC : Constants.GETFIELD)));
- il.append(InstructionFactory.createReturn(retType));
- mg.setMaxLocals();
- mg.setMaxStack();
- injCG.addMethod(mg.getMethod());
- System.out.println(" * " + name + "() > " + (f.isStatic() ? "static" : "field") + " " + f.getType() + " " + cg.getClassName() + "." + f.getName());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- protected ArrayList<Field> getInstanceFields(ClassGen classGen) {
- ArrayList<Field> fields = new ArrayList<Field>();
- for (Field field : classGen.getFields())
- if (!field.isStatic())
- fields.add(field);
- return fields;
- }
- protected ArrayList<Method> getInstanceMethods(ClassGen classGen) {
- ArrayList<Method> methods = new ArrayList<Method>();
- for (Method method : classGen.getMethods())
- if (!method.isStatic() && !method.getName().equals("<init>"))
- methods.add(method);
- return methods;
- }
- public ClassGen getClass(String className) {
- return myClasses.get(className);
- }
- }
- # Ident Class
- package org.warbo.updater.generic;
- import com.sun.org.apache.bcel.internal.generic.ClassGen;
- /**
- * Created by IntelliJ IDEA.
- * User: Warbo
- * Date: 03-Dec-2009
- * Time: 17:54:44
- * To change this template use File | Settings | File Templates.
- */
- public abstract class Identifier {
- public abstract boolean identify(ClassGen cg);
- }
- # Sample Class.
- package org.warbo.updater.identifiers;
- import com.sun.org.apache.bcel.internal.generic.ClassGen;
- import org.warbo.updater.generic.Identifier;
- /**
- * Created by IntelliJ IDEA.
- * User: Warbo
- * Date: 03-Dec-2009
- * Time: 18:06:42
- * To change this template use File | Settings | File Templates.
- */
- public class FileOnDisk extends Identifier {
- @Override
- public boolean identify(ClassGen cg) {
- return (cg.getConstantPool().lookupString("Warning! fileondisk ") > 0); //To change body of implemented methods use File | Settings | File Templates.
- }
- }
- # Sample Transform
- package org.warbo.updater.transforms;
- import com.sun.org.apache.bcel.internal.generic.ClassGen;
- import org.warbo.updater.generic.Transform;
- import java.util.HashMap;
- /**
- * Created by IntelliJ IDEA.
- * User: Warbo
- * Date: 03-Dec-2009
- * Time: 18:06:35
- * To change this template use File | Settings | File Templates.
- */
- public class FileOnDisk extends Transform {
- public FileOnDisk(HashMap<String, ClassGen> myClasses) {
- super(myClasses);
- }
- @Override
- protected boolean processTransform(ClassGen cg) {
- return false; //To change body of implemented methods use File | Settings | File Templates.
- }
- }
Add Comment
Please, Sign In to add comment