Search in sources :

Example 6 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project UniverseCore by EB-wilson.

the class CtBehavior method setAttribute.

/**
 * Adds an attribute. The attribute is saved in the class file.
 *
 * <p>Note that an attribute is a data block specified by
 * the class file format.  It is not an annotation.
 * See {@link javassist.bytecode.AttributeInfo}.
 *
 * @param name      attribute name
 * @param data      attribute value
 */
@Override
public void setAttribute(String name, byte[] data) {
    declaringClass.checkModify();
    methodInfo.addAttribute(new AttributeInfo(methodInfo.getConstPool(), name, data));
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo)

Example 7 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project slf4j by qos-ch.

the class JavassistHelper method getSignature.

/**
 * Return javassist source snippet which lists all the parameters and their
 * values. If available the source names are extracted from the debug
 * information and used, otherwise just a number is shown.
 *
 * @param method
 * @return
 * @throws NotFoundException
 */
public static String getSignature(CtBehavior method) throws NotFoundException {
    CtClass[] parameterTypes = method.getParameterTypes();
    CodeAttribute codeAttribute = method.getMethodInfo().getCodeAttribute();
    LocalVariableAttribute locals = null;
    if (codeAttribute != null) {
        AttributeInfo attribute;
        attribute = codeAttribute.getAttribute("LocalVariableTable");
        locals = (LocalVariableAttribute) attribute;
    }
    String methodName = method.getName();
    StringBuilder sb = new StringBuilder(methodName).append("(\" ");
    for (int i = 0; i < parameterTypes.length; i++) {
        if (i > 0) {
            // add a comma and a space between printed values
            sb.append(" + \", \" ");
        }
        CtClass parameterType = parameterTypes[i];
        boolean isArray = parameterType.isArray();
        CtClass arrayType = parameterType.getComponentType();
        if (isArray) {
            while (arrayType.isArray()) {
                arrayType = arrayType.getComponentType();
            }
        }
        sb.append(" + \"");
        try {
            sb.append(parameterNameFor(method, locals, i));
        } catch (Exception e) {
            sb.append(i + 1);
        }
        sb.append("\" + \"=");
        if (parameterType.isPrimitive()) {
            // let the compiler handle primitive -> string
            sb.append("\"+ $").append(i + 1);
        } else {
            String s = "org.slf4j.instrumentation.ToStringHelper.render";
            sb.append("\"+ ").append(s).append("($").append(i + 1).append(')');
        }
    }
    sb.append("+\")");
    String signature = sb.toString();
    return signature;
}
Also used : CtClass(javassist.CtClass) AttributeInfo(javassist.bytecode.AttributeInfo) CodeAttribute(javassist.bytecode.CodeAttribute) LocalVariableAttribute(javassist.bytecode.LocalVariableAttribute) NotFoundException(javassist.NotFoundException)

Example 8 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project HiddenApiRefinePlugin by RikkaApps.

the class RefineCollector method collect.

public static Map.Entry<String, String> collect(InputStream stream) throws IOException {
    final DataInputStream in = new DataInputStream(new BufferedInputStream(stream));
    final ClassFile file = new ClassFile(in);
    for (AttributeInfo info : file.getAttributes()) {
        if (info instanceof AnnotationsAttribute) {
            final Annotation annotation = ((AnnotationsAttribute) info).getAnnotation(RefineProcessor.DESCRIPTOR_REFINE_DESCRIPTOR);
            if (annotation == null)
                continue;
            final MemberValue from = annotation.getMemberValue(RefineProcessor.DESCRIPTOR_REFINE_FROM);
            final MemberValue to = annotation.getMemberValue(RefineProcessor.DESCRIPTOR_REFINE_TO);
            return new AbstractMap.SimpleEntry<>(((ClassMemberValue) from).getValue().replace('.', '/'), ((ClassMemberValue) to).getValue().replace('.', '/'));
        }
    }
    return null;
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo) ClassFile(javassist.bytecode.ClassFile) BufferedInputStream(java.io.BufferedInputStream) MemberValue(javassist.bytecode.annotation.MemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) DataInputStream(java.io.DataInputStream) Annotation(javassist.bytecode.annotation.Annotation) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue)

Example 9 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project powermock by powermock.

the class AbstractMainMockTransformer method removeFinalModifierFromClass.

protected void removeFinalModifierFromClass(final CtClass clazz) {
    if (strategy != INST_REDEFINE) {
        if (Modifier.isFinal(clazz.getModifiers())) {
            clazz.setModifiers(clazz.getModifiers() ^ Modifier.FINAL);
        }
        ClassFile classFile = clazz.getClassFile2();
        AttributeInfo attribute = classFile.getAttribute(InnerClassesAttribute.tag);
        if (attribute != null && attribute instanceof InnerClassesAttribute) {
            InnerClassesAttribute ica = (InnerClassesAttribute) attribute;
            String name = classFile.getName();
            int n = ica.tableLength();
            for (int i = 0; i < n; ++i) {
                if (name.equals(ica.innerClass(i))) {
                    int accessFlags = ica.accessFlags(i);
                    if (Modifier.isFinal(accessFlags)) {
                        ica.setAccessFlags(i, accessFlags ^ Modifier.FINAL);
                    }
                }
            }
        }
    }
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo) ClassFile(javassist.bytecode.ClassFile) InnerClassesAttribute(javassist.bytecode.InnerClassesAttribute)

Example 10 with AttributeInfo

use of javassist.bytecode.AttributeInfo in project fakereplace by fakereplace.

the class FakeMethodCallManipulator method handleLambdas.

private void handleLambdas(ClassFile file, Map<Integer, Data> knownFakeMethodCallLocations, ConstPool pool) {
    // check the bootstrapmethod's attribute
    // this makes lambda support work
    AttributeInfo bootstrapMethods = file.getAttribute(BootstrapMethodsAttribute.tag);
    if (bootstrapMethods instanceof BootstrapMethodsAttribute) {
        BootstrapMethodsAttribute boot = (BootstrapMethodsAttribute) bootstrapMethods;
        boolean replaceBootstrap = false;
        BootstrapMethodsAttribute.BootstrapMethod[] replacement = boot.getMethods();
        for (BootstrapMethodsAttribute.BootstrapMethod method : replacement) {
            // initial support for lambda replacement
            // first we look for all invocations on LambdaMetafactory
            int kind = pool.getMethodHandleKind(method.methodRef);
            if (kind == ConstPool.REF_invokeStatic) {
                int nameAndType = pool.getMethodHandleIndex(method.methodRef);
                String className = pool.getMethodrefClassName(nameAndType);
                String methodName = pool.getMethodrefName(nameAndType);
                if (className.equals(LambdaMetafactory.class.getName())) {
                    if (methodName.equals("metafactory")) {
                        // we have a lambda instance
                        // does it reference a new method
                        int methodHandleArg = method.arguments[1];
                        kind = pool.getMethodHandleKind(methodHandleArg);
                        if (kind == ConstPool.REF_invokeStatic || kind == ConstPool.REF_invokeVirtual || kind == ConstPool.REF_invokeSpecial) {
                            int methodRefArg = pool.getMethodHandleIndex(methodHandleArg);
                            if (knownFakeMethodCallLocations.containsKey(methodRefArg)) {
                                // the lambda references a new method
                                replaceBootstrap = true;
                                Data target = knownFakeMethodCallLocations.get(methodRefArg);
                                String type = pool.getMethodrefType(methodRefArg);
                                String name = pool.getMethodrefName(methodRefArg);
                                if (kind != ConstPool.REF_invokeStatic) {
                                    type = "(" + DescriptorUtils.extToInt(file.getName()) + type.substring(1);
                                }
                                int newMethodRef = pool.addMethodrefInfo(pool.addClassInfo(target.getProxyName()), name, type);
                                int newMethodHandle = pool.addMethodHandleInfo(ConstPool.REF_invokeStatic, newMethodRef);
                                method.arguments[1] = newMethodHandle;
                            }
                        }
                    }
                }
            }
        }
        if (replaceBootstrap) {
            file.addAttribute(new BootstrapMethodsAttribute(file.getConstPool(), replacement));
        }
    }
}
Also used : AttributeInfo(javassist.bytecode.AttributeInfo) BootstrapMethodsAttribute(javassist.bytecode.BootstrapMethodsAttribute) LambdaMetafactory(java.lang.invoke.LambdaMetafactory) MethodData(org.fakereplace.data.MethodData) BaseClassData(org.fakereplace.data.BaseClassData)

Aggregations

AttributeInfo (javassist.bytecode.AttributeInfo)14 ClassFile (javassist.bytecode.ClassFile)6 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)4 MethodInfo (javassist.bytecode.MethodInfo)4 IOException (java.io.IOException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 CtClass (javassist.CtClass)2 NotFoundException (javassist.NotFoundException)2 CodeAttribute (javassist.bytecode.CodeAttribute)2 InnerClassesAttribute (javassist.bytecode.InnerClassesAttribute)2 LocalVariableAttribute (javassist.bytecode.LocalVariableAttribute)2 SignatureAttribute (javassist.bytecode.SignatureAttribute)2 Annotation (javassist.bytecode.annotation.Annotation)2 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 ObjectStreamClass (java.io.ObjectStreamClass)1 LambdaMetafactory (java.lang.invoke.LambdaMetafactory)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1