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));
}
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;
}
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;
}
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);
}
}
}
}
}
}
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));
}
}
}
Aggregations