use of org.objectweb.asm.ClassWriter in project MineCamera by PorPit.
the class CamTransformer method transform.
public byte[] transform(String name, byte[] basicClass) {
int i = 0;
while (i < transformers.size()) {
if (transformers.get(i).is(name)) {
ClassNode classNode = new ClassNode();
ClassReader classReader = new ClassReader(basicClass);
classReader.accept(classNode, 0);
transformers.get(i).transform(classNode);
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
classNode.accept(writer);
basicClass = writer.toByteArray();
System.out.println("[" + MineCamera.MODID + "] Patched " + transformers.get(i).className + " ...");
transformers.get(i).done();
i++;
// transformers.remove(i);
} else
i++;
}
return basicClass;
}
use of org.objectweb.asm.ClassWriter in project teiid by teiid.
the class RestASMBasedWebArchiveBuilder method getApplicationClass.
public byte[] getApplicationClass(ArrayList<String> models) {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "org/teiid/jboss/rest/TeiidRestApplication", null, "javax/ws/rs/core/Application", null);
{
fv = cw.visitField(ACC_PRIVATE, "singletons", "Ljava/util/Set;", "Ljava/util/Set<Ljava/lang/Object;>;", null);
fv.visitEnd();
}
{
fv = cw.visitField(ACC_PRIVATE, "empty", "Ljava/util/Set;", "Ljava/util/Set<Ljava/lang/Class<*>;>;", null);
fv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/core/Application", "<init>", "()V");
mv.visitVarInsn(ALOAD, 0);
mv.visitTypeInsn(NEW, "java/util/HashSet");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V");
mv.visitFieldInsn(PUTFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
mv.visitVarInsn(ALOAD, 0);
mv.visitTypeInsn(NEW, "java/util/HashSet");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/util/HashSet", "<init>", "()V");
mv.visitFieldInsn(PUTFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "empty", "Ljava/util/Set;");
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
mv.visitTypeInsn(NEW, "org/teiid/jboss/rest/CustomApiListingResource");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/CustomApiListingResource", "<init>", "()V");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
mv.visitInsn(POP);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
mv.visitTypeInsn(NEW, "io/swagger/jaxrs/listing/SwaggerSerializers");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "io/swagger/jaxrs/listing/SwaggerSerializers", "<init>", "()V");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
mv.visitInsn(POP);
mv.visitVarInsn(ALOAD, 0);
for (int i = 0; i < models.size(); i++) {
mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
mv.visitTypeInsn(NEW, "org/teiid/jboss/rest/" + models.get(i));
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/" + models.get(i), "<init>", "()V");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Set", "add", "(Ljava/lang/Object;)Z");
mv.visitInsn(POP);
if (i < models.size() - 1) {
mv.visitVarInsn(ALOAD, 0);
}
}
mv.visitInsn(RETURN);
mv.visitMaxs(3, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "getClasses", "()Ljava/util/Set;", "()Ljava/util/Set<Ljava/lang/Class<*>;>;", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "empty", "Ljava/util/Set;");
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "getSingletons", "()Ljava/util/Set;", "()Ljava/util/Set<Ljava/lang/Object;>;", null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "org/teiid/jboss/rest/TeiidRestApplication", "singletons", "Ljava/util/Set;");
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project teiid by teiid.
the class RestASMBasedWebArchiveBuilder method getViewClass.
protected byte[] getViewClass(String vdbName, String vdbVersion, String modelName, Schema schema, boolean passthroughAuth) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
MethodVisitor mv;
AnnotationVisitor av0;
boolean hasValidProcedures = false;
cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "org/teiid/jboss/rest/" + modelName, null, "org/teiid/jboss/rest/TeiidRSProvider", null);
{
av0 = cw.visitAnnotation("Ljavax/ws/rs/Path;", true);
av0.visit("value", "/" + modelName);
av0.visitEnd();
}
{
av0 = cw.visitAnnotation("Lio/swagger/annotations/Api;", true);
av0.visit("value", "/" + modelName);
av0.visitEnd();
}
cw.visitInnerClass("javax/ws/rs/core/Response$Status", "javax/ws/rs/core/Response", "Status", ACC_PUBLIC + ACC_FINAL + ACC_STATIC + ACC_ENUM);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "org/teiid/jboss/rest/TeiidRSProvider", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
Collection<Procedure> procedures = schema.getProcedures().values();
for (Procedure procedure : procedures) {
String uri = procedure.getProperty(REST_NAMESPACE + "URI", false);
String method = procedure.getProperty(REST_NAMESPACE + "METHOD", false);
String contentType = procedure.getProperty(REST_NAMESPACE + "PRODUCES", false);
String charSet = procedure.getProperty(REST_NAMESPACE + "CHARSET", false);
if (uri != null && method != null) {
if (contentType == null) {
contentType = findContentType(procedure);
}
if (contentType != null) {
contentType = contentType.toLowerCase();
if (contentType.equals("xml")) {
contentType = "application/xml";
} else if (contentType.equals("json")) {
contentType = "application/json";
} else if (contentType.equals("plain")) {
contentType = "text/plain";
}
buildRestService(vdbName, vdbVersion, modelName, procedure, method, uri, cw, contentType, charSet, passthroughAuth);
hasValidProcedures = true;
}
}
}
buildQueryProcedure(vdbName, vdbVersion, modelName, "xml", cw, passthroughAuth);
buildQueryProcedure(vdbName, vdbVersion, modelName, "json", cw, passthroughAuth);
cw.visitEnd();
if (!hasValidProcedures) {
return null;
}
return cw.toByteArray();
}
use of org.objectweb.asm.ClassWriter in project evosuite by EvoSuite.
the class TracingTestRunner method loadClass.
/**
* {@inheritDoc}
*/
@Override
public Class<?> loadClass(String fullyQualifiedTargetClass) throws ClassNotFoundException {
if (isSystemClass(fullyQualifiedTargetClass)) {
logger.info("Not instrumenting class '{}'.", fullyQualifiedTargetClass);
return super.loadClass(fullyQualifiedTargetClass);
}
logger.info("Instrumenting class '{}'.", fullyQualifiedTargetClass);
String className = fullyQualifiedTargetClass.replace('.', '/');
InputStream is = java.lang.ClassLoader.getSystemResourceAsStream(className + ".class");
if (is == null) {
throw new ClassNotFoundException("Class " + fullyQualifiedTargetClass + "could not be found!");
}
ClassReader reader = null;
try {
reader = new ClassReader(is);
} catch (IOException exc) {
throw new ClassNotFoundException();
}
ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_MAXS);
ClassVisitor cv = new StatementTracingVisitor(fullyQualifiedTargetClass, writer);
CheckClassAdapter checkClassAdapter = new CheckClassAdapter(cv);
reader.accept(checkClassAdapter, ClassReader.SKIP_FRAMES);
byte[] byteBuffer = writer.toByteArray();
Class<?> result = defineClass(fullyQualifiedTargetClass, byteBuffer, 0, byteBuffer.length);
return result;
}
use of org.objectweb.asm.ClassWriter in project kalang by kasonyang.
the class Ast2Class method annotation.
protected void annotation(Object obj, AnnotationNode... annotations) {
for (AnnotationNode an : annotations) {
AnnotationVisitor av;
String desc = getTypeDescriptor(Types.getClassType(an.getAnnotationType()));
// TODO set annotation visible
boolean isVisible = true;
if (obj instanceof ClassWriter) {
av = ((ClassWriter) obj).visitAnnotation(desc, isVisible);
} else if (obj instanceof MethodVisitor) {
av = ((MethodVisitor) obj).visitAnnotation(desc, isVisible);
} else {
throw Exceptions.unsupportedTypeException(obj);
}
for (String v : an.values.keySet()) {
// TODO handle enum value
Object javaConst = getJavaConst(an.values.get(v));
av.visit(v, javaConst);
}
}
}
Aggregations