public CtBehavior getEnclosingBehavior() throws NotFoundException { ClassFile cf = getClassFile2(); EnclosingMethodAttribute ema = (EnclosingMethodAttribute)cf.getAttribute( EnclosingMethodAttribute.tag); if (ema == null) return null; else { CtClass enc = classPool.get(ema.className()); String name = ema.methodName(); if (MethodInfo.nameInit.equals(name)) return enc.getConstructor(ema.methodDescriptor()); else if(MethodInfo.nameClinit.equals(name)) return enc.getClassInitializer(); else return enc.getMethod(name, ema.methodDescriptor()); } }
/** * Returns the constructor or method containing the expression. */ public CtBehavior where() { MethodInfo mi = thisMethod; CtBehavior[] cb = thisClass.getDeclaredBehaviors(); for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi) return cb[i]; CtConstructor init = thisClass.getClassInitializer(); if (init != null && init.getMethodInfo2() == mi) return init; /* getDeclaredBehaviors() returns a list of methods/constructors. * Although the list is cached in a CtClass object, it might be * recreated for some reason. Thus, the member name and the signature * must be also checked. */ for (int i = cb.length - 1; i >= 0; --i) { if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor() .equals(cb[i].getMethodInfo2().getDescriptor())) { return cb[i]; } } throw new RuntimeException("fatal: not found"); }
@Override public CtBehavior getEnclosingBehavior() throws NotFoundException { ClassFile cf = getClassFile2(); EnclosingMethodAttribute ema = (EnclosingMethodAttribute)cf.getAttribute( EnclosingMethodAttribute.tag); if (ema == null) return null; CtClass enc = classPool.get(ema.className()); String name = ema.methodName(); if (MethodInfo.nameInit.equals(name)) return enc.getConstructor(ema.methodDescriptor()); else if(MethodInfo.nameClinit.equals(name)) return enc.getClassInitializer(); else return enc.getMethod(name, ema.methodDescriptor()); }
/** * Returns the constructor or method containing the expression. */ public CtBehavior where() { MethodInfo mi = thisMethod; CtBehavior[] cb = thisClass.getDeclaredBehaviors(); for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi) return cb[i]; CtConstructor init = thisClass.getClassInitializer(); if (init != null && init.getMethodInfo2() == mi) return init; /* getDeclaredBehaviors() returns a list of methods/constructors. * Although the list is cached in a CtClass object, it might be * recreated for some reason. Thus, the member name and the signature * must be also checked. */ for (int i = cb.length - 1; i >= 0; --i) { if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor() .equals(cb[i].getMethodInfo2().getDescriptor())) { return cb[i]; } } throw new RuntimeException("fatal: not found"); }
private void addClassInitializerNotification(CtClass clazz) throws CannotCompileException { if (null == clazz.getClassInitializer()) { clazz.makeClassInitializer(); } clazz.getClassInitializer().insertBefore( GlobalNotificationBuildSupport.class.getName() + ".testClassInitiated(" + clazz.getName() + ".class);"); }
private void injectClassHandlerToInstrumentedClasses(ClassPool classPool) throws NotFoundException, CannotCompileException { int index; synchronized (CLASS_HANDLERS) { CLASS_HANDLERS.add(classHandler); index = CLASS_HANDLERS.size() - 1; } CtClass robolectricInternalsCtClass = classPool.get(RobolectricInternals.class.getName()); robolectricInternalsCtClass.setModifiers(Modifier.PUBLIC); robolectricInternalsCtClass.getClassInitializer().insertBefore("{\n" + "classHandler = " + AndroidTranslator.class.getName() + ".getClassHandler(" + index + ");\n" + "}"); }
private byte[] doClass(String name, Class clazz, byte[] b) { CtClass cl = null; ClassPool pool = ClassPool.getDefault(); try { cl = pool.makeClass(new java.io.ByteArrayInputStream(b)); if(agentArgument!=null && agentArgument.equals(PRINT)) { CtMethod method = cl.getDeclaredMethod("getJavaKeycode"); method.insertBefore("System.out.println(\"LinuxJavaPatchAgent.keysym=\"+Long.toHexString($1));"); } else { Properties props = LinuxJavaPatchAgent.getProperties(PATCH_KEY_MAPPING_PROPERTIES, agentArgument); for(Object key: props.keySet()) { String value = props.getProperty((String) key); cl.getClassInitializer().insertAfter("keysym2JavaKeycodeHash.put( Long.valueOf(0x"+key+"l), new sun.awt.X11.XKeysym$Keysym2JavaKeycode(java.awt.event.KeyEvent.VK_"+value+", java.awt.event.KeyEvent.KEY_LOCATION_STANDARD));"); } } b = cl.toBytecode(); } catch (Exception e) { e.printStackTrace(); System.out.println("LinuxJavaPatchAgent.Could not instrument " + name + ", exception : " + e.getMessage()); } finally { if (cl != null) { cl.detach(); } } return b; }
@Override public synchronized void onLoad(ClassPool pool, String classname) throws NotFoundException { // do not instrument yourself if (classname.equals(getClass().getName())) { return; } // do not instrument anything javassist if (classname.startsWith("javassist.")) { return; } if (only != null && !only.contains(classname)) { return; } if (skip != null && skip.contains(classname)) { return; } if (debug) System.err.println("instrumenting " + classname); CtClass cc = pool.get(classname); if (cc.isFrozen()) return; // instrument all methods and constructors if (debug) System.err.println("Handling class " + cc.getName()); handle(cc, cc.getClassInitializer()); for (CtMethod method : cc.getDeclaredMethods()) handle(cc, method); for (CtConstructor constructor : cc.getDeclaredConstructors()) handle(cc, constructor); }
CtConstructor init = klass.getClassInitializer(); if (init != null) { klass.removeConstructor(init);
public byte[] transform(byte[] classfileBuffer, RuntimeException exception) { int number; synchronized (exceptions) { number = exceptions.size(); exceptions.add(exception); } final String src = JavaAgent.class.getName() + ".dontCall(" + number + ");"; try { CtClass clazz = pool.makeClass(new ByteArrayInputStream(classfileBuffer), false); CtConstructor method = clazz.getClassInitializer(); if (method != null) { method.insertBefore(src); } else { method = CtNewConstructor.make(new CtClass[0], new CtClass[0], src, clazz); method.getMethodInfo().setName("<clinit>"); method.setModifiers(Modifier.STATIC); clazz.addConstructor(method); } return clazz.toBytecode(); } catch (IOException e) { e.printStackTrace(); return null; } catch (CannotCompileException e) { e.printStackTrace(); return null; } } }
if (ctClass.getClassInitializer() != null) { sigChecksum.append("Static Code->"); for (CodeIterator i = ctClass.getClassInitializer().getMethodInfo().getCodeAttribute().iterator(); i.hasNext();) { int index = i.next(); int op = i.byteAt(index);
/** * Returns the constructor or method containing the expression. */ public CtBehavior where() { MethodInfo mi = thisMethod; CtBehavior[] cb = thisClass.getDeclaredBehaviors(); for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi) return cb[i]; CtConstructor init = thisClass.getClassInitializer(); if (init != null && init.getMethodInfo2() == mi) return init; /* getDeclaredBehaviors() returns a list of methods/constructors. * Although the list is cached in a CtClass object, it might be * recreated for some reason. Thus, the member name and the signature * must be also checked. */ for (int i = cb.length - 1; i >= 0; --i) { if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor() .equals(cb[i].getMethodInfo2().getDescriptor())) { return cb[i]; } } throw new RuntimeException("fatal: not found"); }
/** * Returns the constructor or method containing the expression. */ public CtBehavior where() { MethodInfo mi = thisMethod; CtBehavior[] cb = thisClass.getDeclaredBehaviors(); for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi) return cb[i]; CtConstructor init = thisClass.getClassInitializer(); if (init != null && init.getMethodInfo2() == mi) return init; /* getDeclaredBehaviors() returns a list of methods/constructors. * Although the list is cached in a CtClass object, it might be * recreated for some reason. Thus, the member name and the signature * must be also checked. */ for (int i = cb.length - 1; i >= 0; --i) { if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor() .equals(cb[i].getMethodInfo2().getDescriptor())) { return cb[i]; } } throw new RuntimeException("fatal: not found"); }
/** * Returns the constructor or method containing the expression. */ public CtBehavior where() { MethodInfo mi = thisMethod; CtBehavior[] cb = thisClass.getDeclaredBehaviors(); for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi) return cb[i]; CtConstructor init = thisClass.getClassInitializer(); if (init != null && init.getMethodInfo2() == mi) return init; /* getDeclaredBehaviors() returns a list of methods/constructors. * Although the list is cached in a CtClass object, it might be * recreated for some reason. Thus, the member name and the signature * must be also checked. */ for (int i = cb.length - 1; i >= 0; --i) { if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor() .equals(cb[i].getMethodInfo2().getDescriptor())) { return cb[i]; } } throw new RuntimeException("fatal: not found"); }
/** * Returns the constructor or method containing the expression. */ public CtBehavior where() { MethodInfo mi = thisMethod; CtBehavior[] cb = thisClass.getDeclaredBehaviors(); for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi) return cb[i]; CtConstructor init = thisClass.getClassInitializer(); if (init != null && init.getMethodInfo2() == mi) return init; /* getDeclaredBehaviors() returns a list of methods/constructors. * Although the list is cached in a CtClass object, it might be * recreated for some reason. Thus, the member name and the signature * must be also checked. */ for (int i = cb.length - 1; i >= 0; --i) { if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor() .equals(cb[i].getMethodInfo2().getDescriptor())) { return cb[i]; } } throw new RuntimeException("fatal: not found"); }
/** * Returns the constructor or method containing the expression. */ public CtBehavior where() { MethodInfo mi = thisMethod; CtBehavior[] cb = thisClass.getDeclaredBehaviors(); for (int i = cb.length - 1; i >= 0; --i) if (cb[i].getMethodInfo2() == mi) return cb[i]; CtConstructor init = thisClass.getClassInitializer(); if (init != null && init.getMethodInfo2() == mi) return init; /* getDeclaredBehaviors() returns a list of methods/constructors. * Although the list is cached in a CtClass object, it might be * recreated for some reason. Thus, the member name and the signature * must be also checked. */ for (int i = cb.length - 1; i >= 0; --i) { if (thisMethod.getName().equals(cb[i].getMethodInfo2().getName()) && thisMethod.getDescriptor() .equals(cb[i].getMethodInfo2().getDescriptor())) { return cb[i]; } } throw new RuntimeException("fatal: not found"); }
public void addToClassInitializer(final String fullClass, final String code) { final CtClass classRef = getClass(fullClass); try { CtConstructor method = classRef.getClassInitializer(); if (method != null) { method.insertAfter(code); } else { method = CtNewConstructor.make(new CtClass[0], new CtClass[0], code, classRef); method.getMethodInfo().setName("<clinit>"); method.setModifiers(Modifier.STATIC); classRef.addConstructor(method); } } catch (final Throwable e) { maybeThrow(new IllegalArgumentException("Cannot add " + code + " to class initializer of " + fullClass, e)); } }
public void addToClassInitializer(final String fullClass, final String code) { final CtClass classRef = getClass(fullClass); try { CtConstructor method = classRef.getClassInitializer(); if (method != null) { method.insertAfter(code); } else { method = CtNewConstructor.make(new CtClass[0], new CtClass[0], code, classRef); method.getMethodInfo().setName("<clinit>"); method.setModifiers(Modifier.STATIC); classRef.addConstructor(method); } } catch (final Throwable e) { maybeThrow(new IllegalArgumentException("Cannot add " + code + " to class initializer of " + fullClass, e)); } }
final CtConstructor initializer = this.c.getClassInitializer(); if(initializer != null && this.javaId.getType()==Type.CLASS) { jid = ((JavaClassId)this.javaId).getClassInit();