use of org.objectweb.asm.ClassVisitor in project byte-buddy by raphw.
the class TypeConstantAdjustmentTest method testInstrumentationLegacyClassFileObjectType.
@Test
public void testInstrumentationLegacyClassFileObjectType() throws Exception {
ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap(mock(TypeDescription.class), this.classVisitor, mock(Implementation.Context.class), mock(TypePool.class), new FieldList.Empty<FieldDescription.InDefinedShape>(), new MethodList.Empty<MethodDescription>(), IGNORED, IGNORED);
classVisitor.visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
MethodVisitor methodVisitor = classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
assertThat(methodVisitor, not(this.methodVisitor));
methodVisitor.visitLdcInsn(Type.getType(Object.class));
verify(this.classVisitor).visit(ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[] { BAZ });
verifyNoMoreInteractions(this.classVisitor);
verify(this.methodVisitor).visitLdcInsn(Type.getType(Object.class).getClassName());
verify(this.methodVisitor).visitMethodInsn(Opcodes.INVOKESTATIC, Type.getType(Class.class).getInternalName(), "forName", Type.getType(Class.class.getDeclaredMethod("forName", String.class)).getDescriptor(), false);
verifyNoMoreInteractions(this.methodVisitor);
}
use of org.objectweb.asm.ClassVisitor in project android_frameworks_base by ParanoidAndroid.
the class AsmGenerator method transform.
/**
* Transforms a class.
* <p/>
* There are 3 kind of transformations:
*
* 1- For "mock" dependencies classes, we want to remove all code from methods and replace
* by a stub. Native methods must be implemented with this stub too. Abstract methods are
* left intact. Modified classes must be overridable (non-private, non-final).
* Native methods must be made non-final, non-private.
*
* 2- For "keep" classes, we want to rewrite all native methods as indicated above.
* If a class has native methods, it must also be made non-private, non-final.
*
* Note that unfortunately static methods cannot be changed to non-static (since static and
* non-static are invoked differently.)
*/
byte[] transform(ClassReader cr, boolean stubNativesOnly) {
boolean hasNativeMethods = hasNativeMethods(cr);
// Get the class name, as an internal name (e.g. com/android/SomeClass$InnerClass)
String className = cr.getClassName();
String newName = transformName(className);
// transformName returns its input argument if there's no need to rename the class
if (newName != className) {
mRenameCount++;
// This class is being renamed, so remove it from the list of classes not renamed.
mClassesNotRenamed.remove(className);
}
mLog.debug("Transform %s%s%s%s", className, newName == className ? "" : " (renamed to " + newName + ")", hasNativeMethods ? " -- has natives" : "", stubNativesOnly ? " -- stub natives only" : "");
// Rewrite the new class from scratch, without reusing the constant pool from the
// original class reader.
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor rv = cw;
if (newName != className) {
rv = new RenameClassAdapter(cw, className, newName);
}
ClassVisitor cv = new TransformClassAdapter(mLog, mStubMethods, mDeleteReturns.get(className), newName, rv, stubNativesOnly, stubNativesOnly || hasNativeMethods);
Set<String> delegateMethods = mDelegateMethods.get(className);
if (delegateMethods != null && !delegateMethods.isEmpty()) {
// known to have no native methods, just skip this step.
if (hasNativeMethods || !(delegateMethods.size() == 1 && delegateMethods.contains(DelegateClassAdapter.ALL_NATIVES))) {
cv = new DelegateClassAdapter(mLog, cv, className, delegateMethods);
}
}
cr.accept(cv, 0);
return cw.toByteArray();
}
use of org.objectweb.asm.ClassVisitor in project quasar by puniverse.
the class Retransform method dumpClass.
public static void dumpClass(String className, byte[] data) {
System.err.println("DUMP OF CLASS: " + className);
ClassReader cr = new ClassReader(data);
ClassVisitor cv = new TraceClassVisitor(null, new Textifier(), new PrintWriter(System.err));
cr.accept(cv, ClassReader.SKIP_FRAMES);
System.out.println("=================");
}
use of org.objectweb.asm.ClassVisitor in project quasar by puniverse.
the class SuspendablesScanner method createGraph.
private void createGraph(InputStream classStream) throws IOException {
final ClassReader cr = new ClassReader(classStream);
ClassVisitor cv = null;
cv = new SuspendableClassifier(true, ASMAPI, cv);
cv = new ClassNodeVisitor(true, ASMAPI, cv);
if (auto)
cv = new CallGraphVisitor(true, ASMAPI, cv);
cr.accept(cv, ClassReader.SKIP_DEBUG | (auto ? 0 : ClassReader.SKIP_CODE));
}
use of org.objectweb.asm.ClassVisitor in project robovm by robovm.
the class ObjCProtocolProxyPlugin method generateProxyMethods.
private void generateProxyMethods(Config config, List<String> interfazes, ClassWriter cw) throws IOException {
Clazzes clazzes = config.getClazzes();
final Set<String> addedMethods = new HashSet<>();
for (String interfaze : interfazes) {
Clazz clazz = clazzes.load(interfaze);
if (clazz == null) {
continue;
}
// Copy all abstract method (we skip default methods) to the proxy
// and make them native instead of abstract.
ClassReader classReader = new ClassReader(clazz.getBytes());
classReader.accept(new ClassVisitor(ASM4, cw) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
String key = name + desc;
if ((access & ACC_ABSTRACT) > 0 && !addedMethods.contains(key)) {
access &= ~ACC_ABSTRACT;
access |= ACC_NATIVE;
addedMethods.add(key);
return super.visitMethod(access, name, desc, signature, exceptions);
}
return null;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
// Ignored
}
@Override
public void visitEnd() {
// Ignored
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
// Ignored
return null;
}
@Override
public void visitAttribute(Attribute attr) {
// Ignored
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
// Ignored
return null;
}
@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
// Ignored
}
@Override
public void visitOuterClass(String owner, String name, String desc) {
// Ignored
}
@Override
public void visitSource(String source, String debug) {
// Ignored
}
}, 0);
}
}
Aggregations