javassist.bytecode.AttributeInfo Java Examples

The following examples show how to use javassist.bytecode.AttributeInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Javassists.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static @Nullable AttributeInfo copyAttribute(MethodInfo from, MethodInfo to, String attributeName) {
    final AttributeInfo attr = from.getAttribute(attributeName);
    if(attr != null) {
        addAttribute(to, attr);
    }
    return attr;
}
 
Example #2
Source File: JavaAssistClass.java    From smart-testing with Apache License 2.0 5 votes vote down vote up
private void addParameterAnnotationsFor(Collection<String> imports, MethodInfo methodInfo, String tag) {
    AttributeInfo attribute = methodInfo.getAttribute(tag);
    ParameterAnnotationsAttribute annotationAttribute = (ParameterAnnotationsAttribute) attribute;
    if (annotationAttribute != null) {
        Annotation[][] parameters = annotationAttribute.getAnnotations();
        for (Annotation[] annotations : parameters) {
            for (Annotation annotation : annotations) {
                imports.add(annotation.getTypeName());
            }
        }
    }
}
 
Example #3
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
private byte[] mergeVisibleAndInvisibleAttributes(AttributeInfo attrVisible, AttributeInfo attrInvisible) {
    final byte[] visible = (attrVisible == null ? new byte[0] : attrVisible.get());
    final byte[] invisible = (attrInvisible == null ? new byte[0] : attrInvisible.get());
    final byte[] retVal = new byte[visible.length + invisible.length];
    System.arraycopy(visible, 0, retVal, 0, visible.length);
    System.arraycopy(invisible, 0, retVal, visible.length, invisible.length);
    return retVal;
}
 
Example #4
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public byte[] getMethodAnnotationsRaw(Signature methodSignature) 
throws MethodNotFoundException {
    final MethodInfo m = findMethodDeclaration(methodSignature);
    if (m == null) {
        throw new MethodNotFoundException(methodSignature.toString());
    }
    final AttributeInfo attrVisible = m.getAttribute(AnnotationsAttribute.visibleTag);
    final AttributeInfo attrInvisible = m.getAttribute(AnnotationsAttribute.invisibleTag);
    return mergeVisibleAndInvisibleAttributes(attrVisible, attrInvisible);
}
 
Example #5
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 5 votes vote down vote up
@Override
public byte[] getFieldAnnotationsRaw(Signature fieldSignature) 
throws FieldNotFoundException {
    final FieldInfo fld = findField(fieldSignature);
    if (fld == null) {
        throw new FieldNotFoundException(fieldSignature.toString());
    }
    final AttributeInfo attrVisible = fld.getAttribute(AnnotationsAttribute.visibleTag);
    final AttributeInfo attrInvisible = fld.getAttribute(AnnotationsAttribute.invisibleTag);
    return mergeVisibleAndInvisibleAttributes(attrVisible, attrInvisible);
}
 
Example #6
Source File: ByteCodeAnalyzer.java    From deadcode4j with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves all annotations of a package/class and its members (if requested).
 *
 * @param clazz        the <code>CtClass</code> to examine
 * @param elementTypes indicates which annotations to retrieve
 * @since 1.4
 */
@Nonnull
@SuppressWarnings("unchecked")
protected static Iterable<Annotation> getAnnotations(@Nonnull CtClass clazz, ElementType... elementTypes) {
    List<ElementType> types = asList(elementTypes);
    List<AttributeInfo> attributes = newArrayList();

    if (clazz.getName().endsWith("package-info") && types.contains(ElementType.PACKAGE) ||
            !clazz.getName().endsWith("package-info") && types.contains(ElementType.TYPE)) {
        attributes.addAll(clazz.getClassFile2().getAttributes());
    }
    if (types.contains(METHOD)) {
        for (CtMethod method : clazz.getDeclaredMethods()) {
            attributes.addAll(method.getMethodInfo2().getAttributes());
        }
    }
    if (types.contains(FIELD)) {
        for (CtField field : clazz.getDeclaredFields()) {
            attributes.addAll(field.getFieldInfo2().getAttributes());
        }
    }

    List<Annotation> annotations = newArrayList();
    for (AttributeInfo attribute : attributes) {
        if (AnnotationsAttribute.class.isInstance(attribute)) {
            Collections.addAll(annotations, AnnotationsAttribute.class.cast(attribute).getAnnotations());
        }
    }

    return annotations;
}
 
Example #7
Source File: Javassists.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static AttributeInfo inPool(AttributeInfo attr, ConstPool pool) {
    return pool.equals(attr.getConstPool()) ? attr
                                            : attr.copy(pool, null);
}
 
Example #8
Source File: Javassists.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static AttributeInfo addAttribute(MethodInfo to, AttributeInfo attr) {
    attr = inPool(attr, to.getConstPool());
    to.addAttribute(attr);
    return attr;
}
 
Example #9
Source File: Tracer.java    From jlibs with Apache License 2.0 4 votes vote down vote up
@Override
public void onLoad(ClassPool pool, String className) throws NotFoundException, CannotCompileException{
    if(!className.startsWith("jlibs.nio."))
        return;
    CtClass clazz = pool.get(className);
    if(clazz.isEnum() || clazz.isInterface() || clazz.isAnnotation())
        return;
    try{
        Set<CtClass> classes = getClasses(clazz, new LinkedHashSet<>());
        CtMethod[] methods = clazz.getDeclaredMethods();
        for(CtMethod method: methods){
            if(Modifier.isAbstract(method.getModifiers()))
                continue;
            Trace trace = getTraceAnnotation(method, classes);
            if(trace!=null && trace.condition()){
                CtMethod copy = CtNewMethod.copy(method, clazz, null);

                // copy annotations from original method
                AttributeInfo attr = method.getMethodInfo().getAttribute(AnnotationsAttribute.invisibleTag);
                if(attr!=null)
                    copy.getMethodInfo().addAttribute(attr);
                attr = method.getMethodInfo().getAttribute(AnnotationsAttribute.visibleTag);
                if(attr!=null)
                    copy.getMethodInfo().addAttribute(attr);

                method.setName(method.getName()+"_orig");
                StringBuilder body = new StringBuilder();
                body.append("{\n");
                body.append("jlibs.nio.Debugger.enter(this+\"."+copy.getName()+"(\"+"+trace.args()+"+\")\");\n");
                body.append("try{\n");
                if(method.getReturnType().getName().equals("void")){
                    body.append("$proceed($$);\n");
                    body.append("jlibs.nio.Debugger.exit();\n");
                }else{
                    body.append(method.getReturnType().getName()+" returnValue = $proceed($$);\n");
                    body.append("jlibs.nio.Debugger.exit(\"return \"+returnValue);\n");
                    body.append("return returnValue;\n");
                }
                body.append("}catch(Throwable thr){\n");
                body.append("jlibs.nio.Debugger.exit(\"throw \"+thr);\n");
                body.append("throw thr;\n");
                body.append("}\n");
                body.append("}");
                copy.setBody(body.toString(), "this", method.getName());
                clazz.addMethod(copy);
            }
        }
    }catch(Throwable e){
        e.printStackTrace();
    }
}
 
Example #10
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] getClassAnnotationsRaw() {
    final AttributeInfo attrVisible = this.cf.getAttribute(AnnotationsAttribute.visibleTag);
    final AttributeInfo attrInvisible = this.cf.getAttribute(AnnotationsAttribute.invisibleTag);
    return mergeVisibleAndInvisibleAttributes(attrVisible, attrInvisible);
}