@Override public void begin(final String name, final Attributes attrs) { String desc = attrs.getValue("desc"); boolean visible = Boolean.valueOf(attrs.getValue("visible")) .booleanValue(); int typeRef = Integer.parseInt(attrs.getValue("typeRef")); TypePath typePath = TypePath.fromString(attrs.getValue("typePath")); Object v = peek(); if (v instanceof ClassVisitor) { push(((ClassVisitor) v).visitTypeAnnotation(typeRef, typePath, desc, visible)); } else if (v instanceof FieldVisitor) { push(((FieldVisitor) v).visitTypeAnnotation(typeRef, typePath, desc, visible)); } else if (v instanceof MethodVisitor) { push(((MethodVisitor) v).visitTypeAnnotation(typeRef, typePath, desc, visible)); } }
@Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { StringBuilder sb = new StringBuilder(); appendAccess(access | ACCESS_FIELD, sb); AttributesImpl att = new AttributesImpl(); att.addAttribute("", "access", "access", "", sb.toString()); att.addAttribute("", "name", "name", "", name); att.addAttribute("", "desc", "desc", "", desc); if (signature != null) { att.addAttribute("", "signature", "signature", "", encode(signature)); } if (value != null) { att.addAttribute("", "value", "value", "", encode(value.toString())); } return new SAXFieldAdapter(sa, att); }
@Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { checkState(); checkAccess(access, Opcodes.ACC_PUBLIC + Opcodes.ACC_PRIVATE + Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL + Opcodes.ACC_VOLATILE + Opcodes.ACC_TRANSIENT + Opcodes.ACC_SYNTHETIC + Opcodes.ACC_ENUM + Opcodes.ACC_DEPRECATED + 0x40000); // ClassWriter.ACC_SYNTHETIC_ATTRIBUTE CheckMethodAdapter.checkUnqualifiedName(version, name, "field name"); CheckMethodAdapter.checkDesc(desc, false); if (signature != null) { checkFieldSignature(signature); } if (value != null) { CheckMethodAdapter.checkConstant(value); } FieldVisitor av = super .visitField(access, name, desc, signature, value); return new CheckFieldAdapter(av); }
private void assertFiledTypes(final Class<?> type, String... expected) throws IOException { final ClassReader reader = new ClassReader(type.getName()); final Set<String> actual = new HashSet<String>(); reader.accept(new ClassVisitor(Opcodes.ASM5) { @Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if ((access & Opcodes.ACC_SYNTHETIC) == 0) { if (signature == null) { signature = desc; } cyclicRefsTest.getTypesFromSignature(signature, actual); } return null; } }, 0); assertEquals(new HashSet<String>(Arrays.asList(expected)), actual); }
/** * For each field, a representative State object is created and added to the * class. * * @param access Modifers applied to the field (including STATICness). * @param name Name of the field. * @param descriptor Type description. * @param signature Generic Signature (for debugging/reflection). * @param value Default value (for static variables only). * @return FieldVisitor (for annotations of the field) - null here. */ @Override public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) { // NOTE: signature: Generic Signature (for debugging/reflection). // Get the field from the class. Field f = classNode.getField(name, descriptor); f.getModifiers().addAll(Modifier.getSet(access)); f.setDefaultValue(value); // TODO: Anonymous inner class extension of EmptyVisitor that imports // annotations. return null; }
public byte[] generate(){ String className = "eu.mikroskeem.test.shuriken.common.classtools.gen.GeneratedClass"; String classNameInternal = className.replaceAll("\\.", "/"); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); cw.visit( V1_8, ACC_PUBLIC + ACC_SUPER, classNameInternal, null, Type.getInternalName(Object.class), null); FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "test", Type.getDescriptor(String.class), null, "hey"); fv.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); cw.visitEnd(); return cw.toByteArray(); }
public byte[] generate(){ String className = "eu.mikroskeem.test.shuriken.common.classtools.gen.InvalidGeneratedClass"; String classNameInternal = className.replaceAll("\\.", "/"); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES); cw.visit( V1_8, ACC_PUBLIC + ACC_SUPER, classNameInternal, null, Type.getInternalName(Object.class), null); FieldVisitor fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "test", Type.getDescriptor(String.class), null, "hey"); fv.visitEnd(); MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitVarInsn(ALOAD, 0); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); cw.visitEnd(); return cw.toByteArray(); }
@Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { this.dirs.addIf("attributes") .add("attribute") .set(name) .attr("type", desc.replaceAll(";$", "")) .attr( "public", (access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC ) .attr( "static", (access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC ) .up().up(); return super.visitField( access, name, desc, signature, value ); }
@Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { if ((access & Opcodes.ACC_SYNTHETIC) != 0) { cp.newUTF8("Synthetic"); } if ((access & Opcodes.ACC_DEPRECATED) != 0) { cp.newUTF8("Deprecated"); } cp.newUTF8(name); cp.newUTF8(desc); if (signature != null) { cp.newUTF8("Signature"); cp.newUTF8(signature); } if (value != null) { cp.newConst(value); } return new FieldConstantsCollector(cv.visitField(access, name, desc, signature, value), cp); }
@Override public void visitLdcInsn(Object cst) { if (!(cst instanceof Type)) { super.visitLdcInsn(cst); return; } // transform Foo.class for 1.2 compatibility String ldcName = ((Type) cst).getInternalName(); String fieldName = "class$" + ldcName.replace('/', '$'); if (!classOptimizer.syntheticClassFields.contains(ldcName)) { classOptimizer.syntheticClassFields.add(ldcName); FieldVisitor fv = classOptimizer.syntheticFieldVisitor(ACC_STATIC | ACC_SYNTHETIC, fieldName, "Ljava/lang/Class;"); fv.visitEnd(); } String clsName = classOptimizer.clsName; mv.visitFieldInsn(GETSTATIC, clsName, fieldName, "Ljava/lang/Class;"); }
@Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { String s = remapper.mapFieldName(className, name, desc); if ("-".equals(s)) { return null; } if ((access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) { if ((access & Opcodes.ACC_FINAL) != 0 && (access & Opcodes.ACC_STATIC) != 0 && desc.length() == 1) { return null; } if ("org/objectweb/asm".equals(pkgName) && s.equals(name)) { System.out.println("INFO: " + clsName + "." + s + " could be renamed"); } super.visitField(access, name, desc, null, value); } else { if (!s.equals(name)) { throw new RuntimeException("The public or protected field " + className + '.' + name + " must not be renamed."); } super.visitField(access, name, desc, null, value); } return null; // remove debug info }
@Override public FieldVisitor visitField( final int access, final String name, final String desc, final String signature, final Object value) { if ((access & ACC_SYNTHETIC) == 0) { JvmFieldBuilder fieldBuilder = new JvmFieldBuilder( result, (access & ACC_STATIC) == 0 ? typeParameters : null, proxies, access, name, desc, signature, value); return fieldBuilder; } return null; }
@Override public FieldVisitor visitField(int access, String name, String desc, String sig, Object o) { if (this.classOnly) { return super.visitField(access, name, desc, sig, o); } if (name.startsWith("$") || name.startsWith("this$")) { return super.visitField(access, name, desc, sig, o); } if (includePrivate) { return new FieldAnalyzeVisitor(this, access, name, desc, sig) .setTypeMap(this.getTypeMap()) .parseSignature(); } // log.debug("Field Name:{}", name); if ((Opcodes.ACC_PRIVATE & access) == 0) { return new FieldAnalyzeVisitor(this, access, name, desc, sig) .setTypeMap(this.getTypeMap()) .parseSignature(); } return super.visitField(access, name, desc, sig, o); }
/** * Emits the bytecode for all fields of the class */ protected void generateFields() { for (SootField f : sc.getFields()) { 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(); } } }
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if ("$VALUES".equals(name) && classStack.peek().isEnum()) { return null; } String signatureDesc = signature != null ? signature : desc; SignatureReader r = new SignatureReader(signatureDesc); CustomTraceSignatureVisitor sv = new CustomTraceSignatureVisitor(Opcodes.ACC_PUBLIC); r.accept(sv); FieldInfo field = FieldInfo.fieldInfo() .name(name) .modifiers(access) .type(sv.getTypeOrSuperclass()) .value(value) .build(); classStack.peek().addToFields(field); return new FieldProcessor(field); }
public FieldVisitor visitField( final int access, final String name, final String desc, final String signature, final Object value) { checkState(); checkAccess(access, Opcodes.ACC_PUBLIC + Opcodes.ACC_PRIVATE + Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL + Opcodes.ACC_VOLATILE + Opcodes.ACC_TRANSIENT + Opcodes.ACC_SYNTHETIC + Opcodes.ACC_ENUM + Opcodes.ACC_DEPRECATED); CheckMethodAdapter.checkIdentifier(name, "field name"); CheckMethodAdapter.checkDesc(desc, false); if (signature != null) { // TODO } if (value != null) { CheckMethodAdapter.checkConstant(value); } FieldVisitor av = cv.visitField(access, name, desc, signature, value); return new CheckFieldAdapter(av); }
public FieldVisitor visitField( final int access, final String name, final String desc, final String signature, final Object value) { buf.setLength(0); buf.append("{\n"); buf.append("fv = cw.visitField("); appendAccess(access | ACCESS_FIELD); buf.append(", "); appendConstant(name); buf.append(", "); appendConstant(desc); buf.append(", "); appendConstant(signature); buf.append(", "); appendConstant(value); buf.append(");\n"); text.add(buf.toString()); ASMifierFieldVisitor aav = new ASMifierFieldVisitor(); text.add(aav.getText()); text.add("}\n"); return aav; }
static void visitMethods(String className, ClassWriter cw, String methodName, @Nullable List<Method> methods) { if (methods != null) { final FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC, KEYS, format("[%s", dKey), null, null); fv.visitEnd(); for (int i = 0; i < methods.size(); i++) { final Method method = methods.get(i); final TypeToken<?> typeToken = TypeToken.of(method.getGenericReturnType()); final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, method.getName(), Type.getMethodDescriptor(method), format("()%s", getDescriptor(typeToken)), null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETSTATIC, className, KEYS, format("[%s", dKey)); visitIntInsn(mv, i); mv.visitInsn(AALOAD); mv.visitMethodInsn(INVOKEVIRTUAL, className, methodName, format("(%s)Ljava/util/Optional;", dKey), false); mv.visitMethodInsn(INVOKEVIRTUAL, "java/util/Optional", "get", "()Ljava/lang/Object;", false); mv.visitTypeInsn(CHECKCAST, Type.getInternalName(method.getReturnType())); mv.visitInsn(ARETURN); mv.visitMaxs(3, 1); mv.visitEnd(); } } }
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { // purge based on FieldNode references for (FieldNode fieldToPurge : fieldsToPurge) { if (fieldToPurge.signature != null && signature != null) { if (fieldToPurge.signature.equals(signature)) { if (fieldToPurge.name.equals(name) && fieldToPurge.desc.equals(desc)) { // return null in order to remove this field Log.info("Purged Field: " + name); return null; } } } else { // signature was null, fall back to name and description only if (fieldToPurge.name.equals(name) && fieldToPurge.desc.equals(desc)) { // return null in order to remove this field Log.info("Purged Field: " + name); return null; } } } // make the next visitor visit this field, in order to keep it return super.visitField(access, name, desc, signature, value); }
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if (isSynthetic(access)) return null; VariableJson f = new VariableJson(); f.setName(name); f.setType(new TypeJson(Type.getType(desc).getClassName())); f.setAccess(access); if (signature != null) { SignatureReader sr = new SignatureReader(signature); TypeSignatureNode sn = new TypeSignatureNode(); sr.acceptType(sn); TypeJson type = new TypeJson(); sn.accept(type); f.setType(type); } addField(f); return new FieldJsonVisitor(f); }
@Override public void visitEnd() { if (transformationParameters.isJUnit4RuleInjectionRequired) { FieldVisitor fv = super.visitField(Opcodes.ACC_PUBLIC, "scottReportingRule", Type.getDescriptor(ScottReportingRule.class), null, null); fv.visitAnnotation(Type.getDescriptor(Rule.class), true).visitEnd(); } if (transformationParameters.isJUnit5ExtensionInjectionRequired) { AnnotationVisitor av0 = super.visitAnnotation("Lorg/junit/jupiter/api/extension/ExtendWith;", true); AnnotationVisitor av1 = av0.visitArray("value"); av1.visit(null, Type.getType("Lhu/advancedweb/scott/runtime/ScottJUnit5Extension;")); av1.visitEnd(); av0.visitEnd(); } super.visitEnd(); }
@Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { StringBuffer sb = new StringBuffer(); appendAccess(access | ACCESS_FIELD, sb); AttributesImpl att = new AttributesImpl(); att.addAttribute("", "access", "access", "", sb.toString()); att.addAttribute("", "name", "name", "", name); att.addAttribute("", "desc", "desc", "", desc); if (signature != null) { att.addAttribute("", "signature", "signature", "", encode(signature)); } if (value != null) { att.addAttribute("", "value", "value", "", encode(value.toString())); } return new SAXFieldAdapter(sa, att); }
public void visit(ClassVisitor visitor) { FieldVisitor fieldVisitor = visitor.visitField(toAccessModifier(access), name, type.getType(), type.getGenericSignature(), null); if (fieldVisitor == null) { return; } for (AnnotationDefinition annotation : annotations) { annotation.visitFieldAnnotation(fieldVisitor); } fieldVisitor.visitEnd(); }
@Override public void visitEnd() { super.visitEnd(); apiMemberAdapter.visit( classMember.getVersion(), classMember.getAccess(), classMember.getName(), classMember.getSignature(), classMember.getSuperName(), classMember.getInterfaces()); visitAnnotationMembers(classMember.getAnnotations()); for (MethodMember method : methods) { MethodVisitor mv = apiMemberAdapter.visitMethod( method.getAccess(), method.getName(), method.getTypeDesc(), method.getSignature(), method.getExceptions().toArray(new String[0])); visitAnnotationMembers(mv, method.getAnnotations()); visitAnnotationMembers(mv, method.getParameterAnnotations()); mv.visitEnd(); } for (FieldMember field : fields) { FieldVisitor fieldVisitor = apiMemberAdapter.visitField( field.getAccess(), field.getName(), field.getTypeDesc(), field.getSignature(), null); visitAnnotationMembers(fieldVisitor, field.getAnnotations()); fieldVisitor.visitEnd(); } for (InnerClassMember innerClass : innerClasses) { apiMemberAdapter.visitInnerClass( innerClass.getName(), innerClass.getOuterName(), innerClass.getInnerName(), innerClass.getAccess()); } apiMemberAdapter.visitEnd(); }
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { ImplementationDependencyRelocator.ClassLiteralRemapping remapping = null; if (CLASS_DESC.equals(desc)) { remapping = remapper.maybeRemap(name); if (remapping != null) { remappedClassLiterals.put(remapping.getLiteral(), remapping.getLiteralReplacement().replace("/", ".")); } } return super.visitField(access, remapping != null ? remapping.getFieldNameReplacement() : name, desc, signature, value); }
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if (isConstant(access) && !isPrivate(access)) { dependentToAll = true; //non-private const } return null; }
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { if (((access & PUBLIC_STATIC_FINAL) == PUBLIC_STATIC_FINAL) && Type.getDescriptor(String.class).equals(desc)) { missingStaticStringConstantGetters.put("get" + name, (String) value); } if (((access & Opcodes.ACC_PRIVATE) > 0) && !isStatic(access) && (Type.getDescriptor(boolean.class).equals(desc))) { booleanFields.add(name); } return super.visitField(access, name, desc, signature, value); }
@Override public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) { if (computeSVUID) { if ("serialVersionUID".equals(name)) { // since the class already has SVUID, we won't be computing it. computeSVUID = false; hasSVUID = true; } /* * Remember field for SVUID computation For field modifiers, only * the ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED, ACC_STATIC, * ACC_FINAL, ACC_VOLATILE, and ACC_TRANSIENT flags are used when * computing serialVersionUID values. */ if ((access & Opcodes.ACC_PRIVATE) == 0 || (access & (Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT)) == 0) { int mods = access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL | Opcodes.ACC_VOLATILE | Opcodes.ACC_TRANSIENT); svuidFields.add(new Item(name, mods, desc)); } } return super.visitField(access, name, desc, signature, value); }
protected void addSVUID(long svuid) { FieldVisitor fv = super.visitField(Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "serialVersionUID", "J", null, svuid); if (fv != null) { fv.visitEnd(); } }