use of javassist.bytecode.ClassFile in project audit4j-core by audit4j.
the class AnnotationDB method scanFields.
protected void scanFields(ClassFile cf) {
List<ClassFile> fields = cf.getFields();
if (fields == null)
return;
for (Object obj : fields) {
FieldInfo field = (FieldInfo) obj;
AnnotationsAttribute visible = (AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.visibleTag);
AnnotationsAttribute invisible = (AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.invisibleTag);
if (visible != null)
populate(visible.getAnnotations(), cf.getName());
if (invisible != null)
populate(invisible.getAnnotations(), cf.getName());
}
}
use of javassist.bytecode.ClassFile in project audit4j-core by audit4j.
the class AnnotationDB method scanMethods.
/**
* Scanns both the method and its parameters for annotations.
*
* @param cf
*/
protected void scanMethods(ClassFile cf) {
List<ClassFile> methods = cf.getMethods();
if (methods == null)
return;
for (Object obj : methods) {
MethodInfo method = (MethodInfo) obj;
if (scanMethodAnnotations) {
AnnotationsAttribute visible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.visibleTag);
AnnotationsAttribute invisible = (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.invisibleTag);
if (visible != null)
populate(visible.getAnnotations(), cf.getName());
if (invisible != null)
populate(invisible.getAnnotations(), cf.getName());
}
if (scanParameterAnnotations) {
ParameterAnnotationsAttribute paramsVisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.visibleTag);
ParameterAnnotationsAttribute paramsInvisible = (ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.invisibleTag);
if (paramsVisible != null && paramsVisible.getAnnotations() != null) {
for (Annotation[] anns : paramsVisible.getAnnotations()) {
populate(anns, cf.getName());
}
}
if (paramsInvisible != null && paramsInvisible.getAnnotations() != null) {
for (Annotation[] anns : paramsInvisible.getAnnotations()) {
populate(anns, cf.getName());
}
}
}
}
}
use of javassist.bytecode.ClassFile in project coprhd-controller by CoprHD.
the class DbSchemaChanger method removeAnnotation.
/**
* remove an annotation from a method
*
* @param methodName the method to which the annotation to be removed
* @param annotationName the annotation name, it should be a full name
*/
public DbSchemaChanger removeAnnotation(String methodName, String annotationName) throws Exception {
// looking for the method to apply the annotation on
CtMethod methodDescriptor = cc.getDeclaredMethod(methodName);
// create the annotation
ClassFile ccFile = cc.getClassFile();
ccFile.setVersionToJava5();
ConstPool constpool = ccFile.getConstPool();
MethodInfo minfo = methodDescriptor.getMethodInfo();
AnnotationsAttribute attr = (AnnotationsAttribute) minfo.getAttribute(AnnotationsAttribute.visibleTag);
Annotation[] annotations = attr.getAnnotations();
List<Annotation> list = new ArrayList();
for (Annotation annotation : annotations) {
if (!annotation.getTypeName().equals(annotationName)) {
list.add(annotation);
}
}
Annotation[] newAnnotations = list.toArray(new Annotation[0]);
attr.setAnnotations(newAnnotations);
minfo.addAttribute(attr);
return this;
}
use of javassist.bytecode.ClassFile in project dubbo by alibaba.
the class JValidator method getMethodParameterBean.
private static Object getMethodParameterBean(Class<?> clazz, Method method, Object[] args) {
if (!hasConstraintParameter(method)) {
return null;
}
try {
String parameterClassName = generateMethodParameterClassName(clazz, method);
Class<?> parameterClass;
try {
parameterClass = (Class<?>) Class.forName(parameterClassName, true, clazz.getClassLoader());
} catch (ClassNotFoundException e) {
ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader());
CtClass ctClass = pool.makeClass(parameterClassName);
ClassFile classFile = ctClass.getClassFile();
classFile.setVersionToJava5();
ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
// parameter fields
Class<?>[] parameterTypes = method.getParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterTypes.length; i++) {
Class<?> type = parameterTypes[i];
Annotation[] annotations = parameterAnnotations[i];
AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag);
for (Annotation annotation : annotations) {
if (annotation.annotationType().isAnnotationPresent(Constraint.class)) {
javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation(classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName()));
Method[] members = annotation.annotationType().getMethods();
for (Method member : members) {
if (Modifier.isPublic(member.getModifiers()) && member.getParameterTypes().length == 0 && member.getDeclaringClass() == annotation.annotationType()) {
Object value = member.invoke(annotation, new Object[0]);
if (null != value) {
MemberValue memberValue = createMemberValue(classFile.getConstPool(), pool.get(member.getReturnType().getName()), value);
ja.addMemberValue(member.getName(), memberValue);
}
}
}
attribute.addAnnotation(ja);
}
}
String fieldName = method.getName() + "Argument" + i;
CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName));
ctField.getFieldInfo().addAttribute(attribute);
ctClass.addField(ctField);
}
parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
}
Object parameterBean = parameterClass.newInstance();
for (int i = 0; i < args.length; i++) {
Field field = parameterClass.getField(method.getName() + "Argument" + i);
field.set(parameterBean, args[i]);
}
return parameterBean;
} catch (Throwable e) {
logger.warn(e.getMessage(), e);
return null;
}
}
use of javassist.bytecode.ClassFile in project openj9 by eclipse.
the class DummyClassGenerator method modifyInvokerOperand.
/*
* This method is to workaround a limitation of ASM which prevents
* generating class file where invokeinterface and invoke[static|special]
* bytecodes share same constant pool entry.
*
* To overcome this limitation, after the ASM has generated class data, the
* operand of invoke[static|special] bytecodes is modified to be same as the
* operand of invokeinterface bytecode. It uses javassist APIs to make this
* modification.
*/
protected byte[] modifyInvokerOperand() throws Exception {
int cpIndex = 0;
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ClassFile classFile = new ClassFile(new DataInputStream(bais));
String[] sharedInvokers = (String[]) characteristics.get(SHARED_INVOKERS);
/* Record operand of invokeinterface in invokeInterface() */
for (int i = 0; i < sharedInvokers.length; i++) {
String methodName = sharedInvokers[i];
if (methodName.equals(INVOKE_INTERFACE)) {
MethodInfo methodInfo = classFile.getMethod(methodName);
CodeIterator ci = methodInfo.getCodeAttribute().iterator();
while (ci.hasNext()) {
int bcIndex = ci.next();
int bc = ci.byteAt(bcIndex);
if (bc == Opcode.INVOKEINTERFACE) {
/* found invokeinterfce bytecode */
cpIndex = ci.s16bitAt(bcIndex + 1);
break;
}
}
}
}
/*
* Modify operand of invokestatic/invokespecial to cpIndex recorded
* above
*/
for (int i = 0; i < sharedInvokers.length; i++) {
String methodName = sharedInvokers[i];
if (methodName.equals(INVOKE_SPECIAL) || (methodName.equals(INVOKE_STATIC))) {
MethodInfo methodInfo = classFile.getMethod(methodName);
CodeIterator ci = methodInfo.getCodeAttribute().iterator();
while (ci.hasNext()) {
int bcIndex = ci.next();
int bc = ci.byteAt(bcIndex);
if ((bc == Opcode.INVOKESPECIAL) || (bc == Opcode.INVOKESTATIC)) {
/*
* found invokespecial or invokestatic bytecode; update
* the operand constant pool index
*/
ci.write16bit(cpIndex, bcIndex + 1);
}
}
}
}
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
DataOutputStream dataStream = new DataOutputStream(byteStream);
classFile.write(dataStream);
return byteStream.toByteArray();
}
Aggregations