Java源码示例:javassist.bytecode.AttributeInfo
示例1
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;
}
示例2
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());
}
}
}
}
示例3
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;
}
示例4
@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);
}
示例5
@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);
}
示例6
/**
* 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;
}
示例7
public static AttributeInfo inPool(AttributeInfo attr, ConstPool pool) {
return pool.equals(attr.getConstPool()) ? attr
: attr.copy(pool, null);
}
示例8
public static AttributeInfo addAttribute(MethodInfo to, AttributeInfo attr) {
attr = inPool(attr, to.getConstPool());
to.addAttribute(attr);
return attr;
}
示例9
@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();
}
}
示例10
@Override
public byte[] getClassAnnotationsRaw() {
final AttributeInfo attrVisible = this.cf.getAttribute(AnnotationsAttribute.visibleTag);
final AttributeInfo attrInvisible = this.cf.getAttribute(AnnotationsAttribute.invisibleTag);
return mergeVisibleAndInvisibleAttributes(attrVisible, attrInvisible);
}