use of org.objectweb.asm.ClassVisitor in project dex2jar by pxb1988.
the class Dex2jarMultiThreadCmd method run0.
private void run0(String fileName, final ExecutorService executorService) throws IOException {
// long baseTS = System.currentTimeMillis();
String baseName = getBaseName(new File(fileName).toPath());
Path currentDir = new File(".").toPath();
Path file = currentDir.resolve(baseName + "-dex2jar.jar");
final Path errorFile = currentDir.resolve(baseName + "-error.zip");
System.err.println("dex2jar " + fileName + " -> " + file);
final BaksmaliBaseDexExceptionHandler exceptionHandler = new BaksmaliBaseDexExceptionHandler();
BaseDexFileReader reader = MultiDexFileReader.open(Files.readAllBytes(new File(fileName).toPath()));
DexFileNode fileNode = new DexFileNode();
try {
reader.accept(fileNode, DexFileReader.SKIP_DEBUG | DexFileReader.IGNORE_READ_EXCEPTION);
} catch (Exception ex) {
exceptionHandler.handleFileException(ex);
throw ex;
}
final FileSystem fs = createZip(file);
final Path dist = fs.getPath("/");
ClassVisitorFactory cvf = new ClassVisitorFactory() {
@Override
public ClassVisitor create(final String name) {
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw);
return new ClassVisitor(Opcodes.ASM4, rca) {
@Override
public void visitEnd() {
super.visitEnd();
String className = rca.getClassName();
byte[] data;
try {
// FIXME handle 'java.lang.RuntimeException: Method code too large!'
data = cw.toByteArray();
} catch (Exception ex) {
System.err.println(String.format("ASM fail to generate .class file: %s", className));
exceptionHandler.handleFileException(ex);
return;
}
try {
Path dist1 = dist.resolve(className + ".class");
BaseCmd.createParentDirectories(dist1);
Files.write(dist1, data);
} catch (IOException e) {
exceptionHandler.handleFileException(e);
}
}
};
}
};
new ExDex2Asm(exceptionHandler) {
@Override
public void convertDex(final DexFileNode fileNode, final ClassVisitorFactory cvf) {
if (fileNode.clzs != null) {
final Map<String, Clz> classes = collectClzInfo(fileNode);
final List<Future<?>> results = new ArrayList<>(fileNode.clzs.size());
for (final DexClassNode classNode : fileNode.clzs) {
results.add(executorService.submit(new Runnable() {
@Override
public void run() {
convertClass(fileNode, classNode, cvf, classes);
}
}));
}
executorService.submit(new Runnable() {
@Override
public void run() {
for (Future<?> result : results) {
try {
result.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
BaksmaliBaseDexExceptionHandler exceptionHandler1 = (BaksmaliBaseDexExceptionHandler) exceptionHandler;
if (exceptionHandler1.hasException()) {
exceptionHandler1.dump(errorFile, new String[0]);
}
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
}.convertDex(fileNode, cvf);
}
use of org.objectweb.asm.ClassVisitor in project groovy-core by groovy.
the class MetaClassImpl method getClassNode.
/**
* Obtains a reference to the original AST for the MetaClass if it is available at runtime
*
* @return The original AST or null if it cannot be returned
*/
public ClassNode getClassNode() {
if (classNode == null && GroovyObject.class.isAssignableFrom(theClass)) {
// let's try load it from the classpath
String groovyFile = theClass.getName();
int idx = groovyFile.indexOf('$');
if (idx > 0) {
groovyFile = groovyFile.substring(0, idx);
}
groovyFile = groovyFile.replace('.', '/') + ".groovy";
// System.out.println("Attempting to load: " + groovyFile);
URL url = theClass.getClassLoader().getResource(groovyFile);
if (url == null) {
url = Thread.currentThread().getContextClassLoader().getResource(groovyFile);
}
if (url != null) {
try {
/**
* todo there is no CompileUnit in scope so class name
* checking won't work but that mostly affects the bytecode
* generation rather than viewing the AST
*/
CompilationUnit.ClassgenCallback search = new CompilationUnit.ClassgenCallback() {
public void call(ClassVisitor writer, ClassNode node) {
if (node.getName().equals(theClass.getName())) {
MetaClassImpl.this.classNode = node;
}
}
};
final ClassLoader parent = theClass.getClassLoader();
CompilationUnit unit = new CompilationUnit();
unit.setClassgenCallback(search);
unit.addSource(url);
unit.compile(Phases.CLASS_GENERATION);
} catch (Exception e) {
throw new GroovyRuntimeException("Exception thrown parsing: " + groovyFile + ". Reason: " + e, e);
}
}
}
return classNode;
}
use of org.objectweb.asm.ClassVisitor in project gwt-test-utils by gwt-test-utils.
the class OverlayTypesRewriter method rewrite.
/**
* Performs rewriting transformations on a class.
*
* @param className the name of the class
* @param classBytes the bytes of the class
*/
public byte[] rewrite(String className, byte[] classBytes) {
Event classBytesRewriteEvent = SpeedTracerLogger.start(DevModeEventType.CLASS_BYTES_REWRITE, "Class Name", className);
String desc = toDescriptor(className);
assert !jsoIntfDescs.contains(desc);
// The ASM model is to chain a bunch of visitors together.
ClassWriter writer = new ClassWriter(0);
ClassVisitor v = writer;
// v = new CheckClassAdapter(v);
// v = new TraceClassVisitor(v, new PrintWriter(System.out));
v = new UseMirroredClasses(v, className);
v = new RewriteSingleJsoImplDispatches(v, typeOracle, jsoData);
v = new RewriteRefsToJsoClasses(v, jsoIntfDescs, mapper);
if (jsoImplDescs.contains(desc)) {
v = WriteJsoImpl.create(v, desc, jsoIntfDescs, mapper, jsoData);
}
if (Double.parseDouble(System.getProperty("java.class.version")) < Opcodes.V1_6) {
v = new ForceClassVersion15(v);
}
new ClassReader(classBytes).accept(v, 0);
classBytesRewriteEvent.end();
return writer.toByteArray();
}
use of org.objectweb.asm.ClassVisitor in project AndroidUtilCode by Blankj.
the class ApiTest method inject2ApiUtils.
private static void inject2ApiUtils(Map<String, ApiInfo> apiImpls) throws IOException {
ClassReader cr = new ClassReader(ApiUtils.class.getName());
ClassWriter cw = new ClassWriter(cr, 0);
ClassVisitor cv = new ApiUtilsClassVisitor(cw, apiImpls, ApiUtils.class.getCanonicalName());
cr.accept(cv, ClassReader.SKIP_FRAMES);
FileUtils.writeByteArrayToFile(new File("ApiUtils2333.class"), cw.toByteArray());
}
use of org.objectweb.asm.ClassVisitor in project AndroidUtilCode by Blankj.
the class BusTest method inject2BusUtils.
private static void inject2BusUtils(Map<String, List<BusInfo>> busMap) throws IOException {
ClassReader cr = new ClassReader(BusUtils.class.getName());
ClassWriter cw = new ClassWriter(cr, 0);
ClassVisitor cv = new BusUtilsClassVisitor(cw, busMap, BusUtils.class.getName());
cr.accept(cv, ClassReader.SKIP_FRAMES);
FileUtils.writeByteArrayToFile(new File("BusUtils2333.class"), cw.toByteArray());
}
Aggregations