use of org.objectweb.asm.ClassVisitor in project cdap by caskdata.
the class SparkClassRewriter method rewriteClient.
/**
* Defines the org.apache.spark.deploy.yarn.Client class with rewriting of the createConfArchive method to
* workaround the SPARK-13441 bug.
*/
@Nullable
private byte[] rewriteClient(InputStream byteCodeStream) throws IOException {
// We only need to rewrite if listing either HADOOP_CONF_DIR or YARN_CONF_DIR return null.
boolean needRewrite = false;
for (String env : ImmutableList.of("HADOOP_CONF_DIR", "YARN_CONF_DIR")) {
String value = System.getenv(env);
if (value != null) {
File path = new File(value);
if (path.isDirectory() && path.listFiles() == null) {
needRewrite = true;
break;
}
}
}
// If rewrite is not needed
if (!needRewrite) {
return null;
}
ClassReader cr = new ClassReader(byteCodeStream);
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cr.accept(new ClassVisitor(Opcodes.ASM5, cw) {
@Override
public MethodVisitor visitMethod(final int access, final String name, final String desc, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
// Only rewrite the createConfArchive method
if (!"createConfArchive".equals(name)) {
return mv;
}
// Check if it's a recognizable return type.
// Spark 1.5+ return type is File
boolean isReturnFile = Type.getReturnType(desc).equals(Type.getType(File.class));
Type optionType = Type.getObjectType("scala/Option");
if (!isReturnFile) {
// Spark 1.4 return type is Option<File>
if (!Type.getReturnType(desc).equals(optionType)) {
// Unknown type. Not going to modify the code.
return mv;
}
}
// Generate this for Spark 1.5+
// return SparkRuntimeUtils.createConfArchive(this.sparkConf, SPARK_CONF_FILE,
// LOCALIZED_CONF_DIR, LOCALIZED_CONF_DIR_ZIP);
// Generate this for Spark 1.4
// return Option.apply(SparkRuntimeUtils.createConfArchive(this.sparkConf, SPARK_CONF_FILE,
// LOCALIZED_CONF_DIR, LOCALIZED_CONF_DIR_ZIP));
GeneratorAdapter mg = new GeneratorAdapter(mv, access, name, desc);
// load this.sparkConf to the stack
mg.loadThis();
mg.getField(Type.getObjectType("org/apache/spark/deploy/yarn/Client"), "sparkConf", SPARK_CONF_TYPE);
// push three constants to the stack
mg.visitLdcInsn(SPARK_CONF_FILE);
mg.visitLdcInsn(LOCALIZED_CONF_DIR);
mg.visitLdcInsn(LOCALIZED_CONF_DIR_ZIP);
// call SparkRuntimeUtils.createConfArchive, return a File and leave it in stack
Type stringType = Type.getType(String.class);
mg.invokeStatic(SPARK_RUNTIME_UTILS_TYPE, new Method("createConfArchive", Type.getType(File.class), new Type[] { SPARK_CONF_TYPE, stringType, stringType, stringType }));
if (isReturnFile) {
// Spark 1.5+ return type is File, hence just return the File from the stack
mg.returnValue();
mg.endMethod();
} else {
// Spark 1.4 return type is Option<File>
// return Option.apply(<file from stack>);
// where the file is actually just popped from the stack
mg.invokeStatic(optionType, new Method("apply", optionType, new Type[] { Type.getType(Object.class) }));
mg.checkCast(optionType);
mg.returnValue();
mg.endMethod();
}
return null;
}
}, ClassReader.EXPAND_FRAMES);
return cw.toByteArray();
}
use of org.objectweb.asm.ClassVisitor in project android_frameworks_base by crdroidandroid.
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.equals(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.equals(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 cv = cw;
// FIXME Generify
if ("android/content/res/Resources".equals(className)) {
cv = new FieldInjectorAdapter(cv);
}
if (mReplaceMethodCallsClasses.contains(className)) {
cv = new ReplaceMethodCallsAdapter(cv, className);
}
cv = new RefactorClassAdapter(cv, mRefactorClasses);
if (!newName.equals(className)) {
cv = new RenameClassAdapter(cv, className, newName);
}
String binaryNewName = newName.replace('/', '.');
if (mInjectedMethodsMap.keySet().contains(binaryNewName)) {
cv = new InjectMethodsAdapter(cv, mInjectedMethodsMap.get(binaryNewName));
}
cv = new TransformClassAdapter(mLog, mStubMethods, mDeleteReturns.get(className), newName, cv, stubNativesOnly);
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);
}
}
Set<String> promoteFields = mPromotedFields.get(className);
if (promoteFields != null && !promoteFields.isEmpty()) {
cv = new PromoteFieldClassAdapter(cv, promoteFields);
}
cr.accept(cv, 0);
return cw.toByteArray();
}
use of org.objectweb.asm.ClassVisitor in project Lucee by lucee.
the class JarUtil method _getExternalImports.
private static void _getExternalImports(Set<String> imports, InputStream src, String[] ignores) throws IOException {
final ClassReader reader = new ClassReader(src);
final Remapper remapper = new Collector(imports, ignores);
final ClassVisitor inner = new EmptyVisitor();
final RemappingClassAdapter visitor = new RemappingClassAdapter(inner, remapper);
reader.accept(visitor, 0);
}
use of org.objectweb.asm.ClassVisitor in project Lucee by lucee.
the class ClassRenamer method rename.
public static byte[] rename(byte[] src, String newName) {
ClassReader cr = new ClassReader(src);
ClassWriter cw = ASMUtil.getClassWriter();
ClassVisitor ca = new ClassRenamer(cw, newName);
cr.accept(ca, 0);
return cw.toByteArray();
}
use of org.objectweb.asm.ClassVisitor in project Lucee by lucee.
the class MethodCleaner method modifie.
public static byte[] modifie(byte[] src, String methodName, Class[] args, Class rtn, String msg) {
ClassReader cr = new ClassReader(src);
ClassWriter cw = ASMUtil.getClassWriter();
ClassVisitor ca = new MethodCleaner(cw, methodName, args, rtn, msg);
cr.accept(ca, 0);
return cw.toByteArray();
}
Aggregations