use of org.objectweb.asm.FieldVisitor in project access-modifier by kohsuke.
the class Checker method loadRestrictions.
/**
* Loads an additional restriction from the specified "META-INF/annotations/org.kohsuke.accmod.Restricted" file.
*
* @param isInTheInspectedModule
* This value shows up in {@link RestrictedElement#isInTheInspectedModule()}.
* @param stream
*/
public void loadRestrictions(InputStream stream, final boolean isInTheInspectedModule) throws IOException {
if (stream == null)
return;
BufferedReader r = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String className;
while ((className = r.readLine()) != null) {
InputStream is = dependencies.getResourceAsStream(className.replace('.', '/') + ".class");
if (is == null) {
errorListener.onWarning(null, null, "Failed to find class file for " + className);
continue;
}
try {
new ClassReader(is).accept(new ClassVisitor(Opcodes.ASM5) {
private String className;
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
this.className = name;
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return onAnnotationFor(className, desc);
}
@Override
public FieldVisitor visitField(int access, final String name, String desc, String signature, Object value) {
return new FieldVisitor(Opcodes.ASM5) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return onAnnotationFor(className + '.' + name, desc);
}
};
}
@Override
public MethodVisitor visitMethod(int access, final String methodName, final String methodDesc, String signature, String[] exceptions) {
return new MethodVisitor(Opcodes.ASM5) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
return onAnnotationFor(className + '.' + methodName + methodDesc, desc);
}
};
}
/**
* Parse {@link Restricted} annotation on some annotated element.
*/
private AnnotationVisitor onAnnotationFor(final String keyName, String desc) {
if (RESTRICTED_DESCRIPTOR.equals(desc)) {
RestrictedElement target = new RestrictedElement() {
public boolean isInTheInspectedModule() {
return isInTheInspectedModule;
}
public String toString() {
return keyName;
}
};
return new Parser(target) {
@Override
public void visitEnd() {
try {
restrictions.put(keyName, build(factory));
} catch (ClassNotFoundException e) {
failure(e);
} catch (InstantiationException e) {
failure(e);
} catch (IllegalAccessException e) {
failure(e);
}
}
/**
* Fails to load a {@link AccessRestriction} instance.
*/
private void failure(Exception e) {
errorListener.onError(e, null, "Failed to load restrictions");
}
};
}
return null;
}
}, ClassReader.SKIP_CODE);
} finally {
is.close();
}
}
}
use of org.objectweb.asm.FieldVisitor in project soot by Sable.
the class AbstractASMBackend method generateFields.
/**
* Emits the bytecode for all fields of the class
*/
protected void generateFields() {
for (SootField f : sc.getFields()) {
if (f.isPhantom())
continue;
String name = f.getName();
String desc = toTypeDesc(f.getType());
String sig = null;
if (f.hasTag("SignatureTag")) {
SignatureTag genericSignature = (SignatureTag) f.getTag("SignatureTag");
sig = genericSignature.getSignature();
}
Object value = getDefaultValue(f);
int access = getModifiers(f.getModifiers(), f);
FieldVisitor fv = cv.visitField(access, name, desc, sig, value);
if (fv != null) {
generateAnnotations(fv, f);
generateAttributes(fv, f);
fv.visitEnd();
}
}
}
use of org.objectweb.asm.FieldVisitor in project beetl2.0 by javamonkey.
the class ASMGen method dump.
public static byte[] dump() throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "com/beetl/performance/lab/asm/UserAsmAccessor1", null, "java/lang/Object", new String[] { "com/beetl/performance/lab/asm/Access" });
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "get", "(Ljava/lang/Object;I)Ljava/lang/Object;", null, new String[] { "com/beetl/performance/lab/asm/ASMCastException" });
mv.visitCode();
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Exception");
mv.visitInsn(ACONST_NULL);
mv.visitVarInsn(ASTORE, 3);
mv.visitLabel(l0);
mv.visitVarInsn(ALOAD, 1);
mv.visitTypeInsn(CHECKCAST, "com/beetl/performance/lab/User");
mv.visitVarInsn(ASTORE, 3);
mv.visitLabel(l1);
Label l3 = new Label();
mv.visitJumpInsn(GOTO, l3);
mv.visitLabel(l2);
mv.visitFrame(Opcodes.F_FULL, 4, new Object[] { "com/beetl/performance/lab/asm/UserAsmAccessor", "java/lang/Object", Opcodes.INTEGER, "com/beetl/performance/lab/User" }, 1, new Object[] { "java/lang/Exception" });
mv.visitVarInsn(ASTORE, 4);
mv.visitTypeInsn(NEW, "com/beetl/performance/lab/asm/ASMCastException");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "com/beetl/performance/lab/asm/ASMCastException", "<init>", "()V");
mv.visitInsn(ATHROW);
mv.visitLabel(l3);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(ILOAD, 2);
Label l4 = new Label();
Label l5 = new Label();
Label l6 = new Label();
mv.visitTableSwitchInsn(1, 2, l6, new Label[] { l4, l5 });
mv.visitLabel(l4);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "com/beetl/performance/lab/User", "getName", "()Ljava/lang/String;");
mv.visitInsn(ARETURN);
mv.visitLabel(l5);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitVarInsn(ALOAD, 3);
mv.visitMethodInsn(INVOKEVIRTUAL, "com/beetl/performance/lab/User", "getId", "()I");
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
mv.visitInsn(ARETURN);
mv.visitLabel(l6);
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
mv.visitTypeInsn(NEW, "java/lang/RuntimeException");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "()V");
mv.visitInsn(ATHROW);
mv.visitMaxs(2, 5);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
use of org.objectweb.asm.FieldVisitor in project LanternServer by LanternPowered.
the class AbstractListDataTypeGenerator method generateClasses.
@Override
<M extends DataManipulator<M, I>, I extends ImmutableDataManipulator<I, M>> void generateClasses(ClassWriter cwM, ClassWriter cwI, String mutableClassName, String immutableClassName, Class<M> manipulatorType, Class<I> immutableManipulatorType, @Nullable Class<? extends M> mutableExpansion, @Nullable Class<? extends I> immutableExpansion, @Nullable List<Method> mutableMethods, @Nullable List<Method> immutableMethods) {
FieldVisitor fv;
MethodVisitor mv;
final String dManipulatorType = Type.getDescriptor(manipulatorType);
final String dImmutableManipulatorType = Type.getDescriptor(immutableManipulatorType);
final String dMutableExpansion = mutableExpansion == null ? null : Type.getDescriptor(mutableExpansion);
final String dImmutableExpansion = immutableExpansion == null ? null : Type.getDescriptor(immutableExpansion);
final String nManipulatorType = Type.getInternalName(manipulatorType);
final String nImmutableManipulatorType = Type.getInternalName(immutableManipulatorType);
final String nMutableExpansion = mutableExpansion == null ? null : Type.getInternalName(mutableExpansion);
final String nImmutableExpansion = immutableExpansion == null ? null : Type.getInternalName(immutableExpansion);
final Class<?> elementType = TypeToken.of(manipulatorType).resolveType(ListData.class.getTypeParameters()[0]).getRawType();
final String dElementType = Type.getDescriptor(elementType);
// Mutable class
{
final String[] interfaces = new String[mutableExpansion != null ? 2 : 1];
final StringBuilder signBuilder = new StringBuilder();
interfaces[0] = nManipulatorType;
signBuilder.append(dManipulatorType);
if (mutableExpansion != null) {
interfaces[1] = nMutableExpansion;
signBuilder.append(dMutableExpansion);
}
cwM.visit(V1_8, ACC_PUBLIC + ACC_SUPER, mutableClassName, format("L%s<%s%s%s>;", nAbstractListData, dElementType, dManipulatorType, dImmutableManipulatorType) + signBuilder.toString(), nAbstractListData, interfaces);
{
fv = cwM.visitField(ACC_PUBLIC + ACC_STATIC, KEY, dKey, format("L%s<L%s<%s>;>;", nKey, nListValue, dElementType), null);
fv.visitEnd();
}
{
fv = cwM.visitField(ACC_PUBLIC + ACC_STATIC, LIST_SUPPLIER, "Ljava/util/function/Supplier;", format("Ljava/util/function/Supplier<Ljava/util/List<%s>;>;", dElementType), null);
fv.visitEnd();
}
{
mv = cwM.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(Type.getType(manipulatorType));
mv.visitLdcInsn(Type.getType(immutableManipulatorType));
mv.visitFieldInsn(GETSTATIC, mutableClassName, KEY, dKey);
mv.visitFieldInsn(GETSTATIC, mutableClassName, LIST_SUPPLIER, "Ljava/util/function/Supplier;");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/function/Supplier", "get", "()Ljava/lang/Object;", true);
mv.visitTypeInsn(CHECKCAST, "java/util/List");
mv.visitMethodInsn(INVOKESPECIAL, nAbstractListData, "<init>", format("(Ljava/lang/Class;Ljava/lang/Class;%sLjava/util/List;)V", dKey), false);
mv.visitInsn(RETURN);
mv.visitMaxs(5, 1);
mv.visitEnd();
}
{
mv = cwM.visitMethod(ACC_PUBLIC, "<init>", format("(%s)V", dImmutableManipulatorType), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, nAbstractListData, "<init>", format("(%s)V", dImmutableListData), false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
{
mv = cwM.visitMethod(ACC_PUBLIC, "<init>", format("(%s)V", dManipulatorType), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, nAbstractListData, "<init>", format("(%s)V", dListData), false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cwM.visitEnd();
}
// Immutable class
{
final String[] interfaces = new String[mutableExpansion != null ? 2 : 1];
final StringBuilder signBuilder = new StringBuilder();
interfaces[0] = nImmutableManipulatorType;
signBuilder.append(dImmutableManipulatorType);
if (mutableExpansion != null) {
interfaces[1] = nImmutableExpansion;
signBuilder.append(dImmutableExpansion);
}
cwI.visit(V1_8, ACC_PUBLIC + ACC_SUPER, immutableClassName, format("L%s<%s%s%s>;", nAbstractImmutableListData, dElementType, dImmutableManipulatorType, dManipulatorType) + signBuilder.toString(), nAbstractImmutableListData, interfaces);
{
fv = cwI.visitField(ACC_PUBLIC + ACC_STATIC, KEY, dKey, format("L%s<L%s<%s>;>;", nKey, nListValue, dElementType), null);
fv.visitEnd();
}
{
fv = cwI.visitField(ACC_PUBLIC + ACC_STATIC, LIST_SUPPLIER, "Ljava/util/function/Supplier;", format("Ljava/util/function/Supplier<Ljava/util/List<%s>;>;", dElementType), null);
fv.visitEnd();
}
{
mv = cwI.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(Type.getType(immutableManipulatorType));
mv.visitLdcInsn(Type.getType(manipulatorType));
mv.visitFieldInsn(GETSTATIC, immutableClassName, "key", dKey);
mv.visitFieldInsn(GETSTATIC, immutableClassName, LIST_SUPPLIER, "Ljava/util/function/Supplier;");
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/function/Supplier", "get", "()Ljava/lang/Object;", true);
mv.visitTypeInsn(CHECKCAST, "java/util/List");
mv.visitMethodInsn(INVOKESPECIAL, nAbstractImmutableListData, "<init>", format("(Ljava/lang/Class;Ljava/lang/Class;%sLjava/util/List;)V", dKey), false);
mv.visitInsn(RETURN);
mv.visitMaxs(5, 1);
mv.visitEnd();
}
{
mv = cwI.visitMethod(ACC_PUBLIC, "<init>", format("(%s)V", dManipulatorType), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, nAbstractImmutableListData, "<init>", format("(%s)V", dListData), false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cwI.visitEnd();
}
}
use of org.objectweb.asm.FieldVisitor in project LanternServer by LanternPowered.
the class AbstractVariantDataTypeGenerator method generateClasses.
@Override
<M extends DataManipulator<M, I>, I extends ImmutableDataManipulator<I, M>> void generateClasses(ClassWriter cwM, ClassWriter cwI, String mutableClassName, String immutableClassName, Class<M> manipulatorType, Class<I> immutableManipulatorType, @Nullable Class<? extends M> mutableExpansion, @Nullable Class<? extends I> immutableExpansion, @Nullable List<Method> mutableMethods, @Nullable List<Method> immutableMethods) {
FieldVisitor fv;
MethodVisitor mv;
final String dManipulatorType = Type.getDescriptor(manipulatorType);
final String dImmutableManipulatorType = Type.getDescriptor(immutableManipulatorType);
final String dMutableExpansion = mutableExpansion == null ? null : Type.getDescriptor(mutableExpansion);
final String dImmutableExpansion = immutableExpansion == null ? null : Type.getDescriptor(immutableExpansion);
final String nManipulatorType = Type.getInternalName(manipulatorType);
final String nImmutableManipulatorType = Type.getInternalName(immutableManipulatorType);
final String nMutableExpansion = mutableExpansion == null ? null : Type.getInternalName(mutableExpansion);
final String nImmutableExpansion = immutableExpansion == null ? null : Type.getInternalName(immutableExpansion);
final Class<?> elementType = TypeToken.of(manipulatorType).resolveType(VariantData.class.getTypeParameters()[0]).getRawType();
final String dElementType = Type.getDescriptor(elementType);
// Mutable class
{
final String[] interfaces = new String[mutableExpansion != null ? 2 : 1];
final StringBuilder signBuilder = new StringBuilder();
interfaces[0] = nManipulatorType;
signBuilder.append(dManipulatorType);
if (mutableExpansion != null) {
interfaces[1] = nMutableExpansion;
signBuilder.append(dMutableExpansion);
}
cwM.visit(V1_8, ACC_PUBLIC + ACC_SUPER, mutableClassName, format("L%s<%s%s%s>;", nAbstractVariantData, dElementType, dManipulatorType, dImmutableManipulatorType) + signBuilder.toString(), nAbstractVariantData, interfaces);
{
fv = cwM.visitField(ACC_PUBLIC + ACC_STATIC, KEY, dKey, format("L%s<L%s<%s>;>;", nKey, nValue, dElementType), null);
fv.visitEnd();
}
{
fv = cwM.visitField(ACC_PUBLIC + ACC_STATIC, VALUE, dElementType, null, null);
fv.visitEnd();
}
{
mv = cwM.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(Type.getType(manipulatorType));
mv.visitLdcInsn(Type.getType(immutableManipulatorType));
mv.visitFieldInsn(GETSTATIC, mutableClassName, KEY, dKey);
mv.visitFieldInsn(GETSTATIC, mutableClassName, VALUE, dElementType);
mv.visitMethodInsn(INVOKESPECIAL, nAbstractVariantData, "<init>", format("(Ljava/lang/Class;Ljava/lang/Class;%sLjava/lang/Object;)V", dKey), false);
mv.visitInsn(RETURN);
mv.visitMaxs(5, 1);
mv.visitEnd();
}
{
mv = cwM.visitMethod(ACC_PUBLIC, "<init>", format("(%s)V", dImmutableManipulatorType), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, nAbstractVariantData, "<init>", format("(%s)V", dImmutableVariantData), false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
{
mv = cwM.visitMethod(ACC_PUBLIC, "<init>", format("(%s)V", dManipulatorType), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, nAbstractVariantData, "<init>", format("(%s)V", dVariantData), false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cwM.visitEnd();
}
// Immutable class
{
final String[] interfaces = new String[mutableExpansion != null ? 2 : 1];
final StringBuilder signBuilder = new StringBuilder();
interfaces[0] = nImmutableManipulatorType;
signBuilder.append(dImmutableManipulatorType);
if (mutableExpansion != null) {
interfaces[1] = nImmutableExpansion;
signBuilder.append(dImmutableExpansion);
}
cwI.visit(V1_8, ACC_PUBLIC + ACC_SUPER, immutableClassName, format("L%s<%s%s%s>;", nAbstractImmutableVariantData, dElementType, dImmutableManipulatorType, dManipulatorType) + signBuilder.toString(), nAbstractImmutableVariantData, interfaces);
{
fv = cwI.visitField(ACC_PUBLIC + ACC_STATIC, KEY, dKey, format("L%s<L%s<%s>;>;", nKey, nValue, dElementType), null);
fv.visitEnd();
}
{
fv = cwI.visitField(ACC_PUBLIC + ACC_STATIC, VALUE, dElementType, null, null);
fv.visitEnd();
}
{
mv = cwI.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(Type.getType(immutableManipulatorType));
mv.visitLdcInsn(Type.getType(manipulatorType));
mv.visitFieldInsn(GETSTATIC, immutableClassName, KEY, dKey);
mv.visitFieldInsn(GETSTATIC, immutableClassName, VALUE, dElementType);
mv.visitMethodInsn(INVOKESPECIAL, nAbstractImmutableVariantData, "<init>", format("(Ljava/lang/Class;Ljava/lang/Class;%sLjava/lang/Object;)V", dKey), false);
mv.visitInsn(RETURN);
mv.visitMaxs(5, 1);
mv.visitEnd();
}
{
mv = cwI.visitMethod(ACC_PUBLIC, "<init>", format("(%s)V", dManipulatorType), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKESPECIAL, nAbstractImmutableVariantData, "<init>", format("(%s)V", dVariantData), false);
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cwI.visitEnd();
}
}
Aggregations