@SuppressWarnings("unchecked") public static <T> Class<T> getModifiedClass(Class<T> clazz, Annotation[] add) throws javassist.NotFoundException, CannotCompileException, InvocationTargetException, IllegalAccessException { ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get(clazz.getCanonicalName()); for (CtMethod method : cc.getDeclaredMethods()) { ClassFile ccFile = cc.getClassFile(); ConstPool constpool = ccFile.getConstPool(); AnnotationsAttribute attr; attr = filterExistingAnnotations(add, method); addNewAnnotations(add, method, constpool, pool, attr); } cc.setName(cc.getName() + "$ClassModifier$" + COUNTER.increment()); cc.setSuperclass(pool.get(clazz.getCanonicalName())); return cc.toClass(); }
private static MemberValue[] toMemberValue(Object[] value, Class<?> valueType, ConstPool constpool, ClassPool classPool) throws NotFoundException { final MemberValue[] memberValues = new MemberValue[value.length]; for (int i = 0; i < value.length; i++) { memberValues[i] = toMemberValue(value[i], valueType, constpool, classPool); } return memberValues; }
@Override protected Object enrichByType(Class<?> clazz, Method method, ArquillianResteasyResource annotation, Consumes consumes, Produces produces) { Object value; Client client = JerseyClientBuilder.newClient(); WebTarget webTarget = client.target(getBaseURL() + annotation.value()); final Map<String, String> headers = getHeaders(clazz, method); if (!headers.isEmpty()) { webTarget.register(new HeaderFilter(headers)); } JerseyWebTarget jerseyWebTarget = (JerseyWebTarget) webTarget; if (WebTarget.class.isAssignableFrom(clazz)) { value = jerseyWebTarget; } else { final Class<?> parameterType; try { final Annotation[] methodDeclaredAnnotations = method.getDeclaredAnnotations(); // This is test method so if it only contains @Test annotation then we don't need to hassel with substitutions parameterType = methodDeclaredAnnotations.length <= 1 ? clazz : ClassModifier.getModifiedClass(clazz, methodDeclaredAnnotations); } catch (Exception e) { throw new RuntimeException("Cannot substitute annotations for method " + method.getName(), e); } value = WebResourceFactory.newResource(parameterType, jerseyWebTarget); } return value; }
private static MemberValue toMemberValue(Object value, Class<?> componentType, ConstPool constpool, ClassPool classPool) throws NotFoundException { final CtClass type = getType(value, componentType, classPool); final MemberValue memberValue = javassist.bytecode.annotation.Annotation.createMemberValue(constpool, type); if (memberValue instanceof BooleanMemberValue) { } else if (type.isArray()) { ((ArrayMemberValue) memberValue).setValue( toMemberValue((Object[]) value, componentType, constpool, classPool)); } else if (type.isInterface()) { final javassist.bytecode.annotation.Annotation annotation = try { annotation.addMemberValue(method.getName(), toMemberValue(method.invoke(value), null, constpool, classPool)); } catch (Exception e) { throw new RuntimeException(e);
private static void addNewAnnotations(Annotation[] add, CtMethod method, ConstPool constpool, ClassPool classPool, AnnotationsAttribute attr) throws IllegalAccessException, NotFoundException, InvocationTargetException { if (null == attr) { attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag); method.getMethodInfo().addAttribute(attr); } for (Annotation toAdd : add) { attr.addAnnotation(toJavassist(toAdd, constpool, classPool)); } }
@Override protected Object enrichByType(Class<?> clazz, Method method, ArquillianResteasyResource annotation, Consumes consumes, Produces produces) { Object value; Client client = JerseyClientBuilder.newClient(); WebTarget webTarget = client.target(getBaseURL() + annotation.value()); final Map<String, String> headers = getHeaders(clazz, method); if (!headers.isEmpty()) { webTarget.register(new HeaderFilter(headers)); } JerseyWebTarget jerseyWebTarget = (JerseyWebTarget) webTarget; if (WebTarget.class.isAssignableFrom(clazz)) { value = jerseyWebTarget; } else { final Class<?> parameterType; try { final Annotation[] methodDeclaredAnnotations = method.getDeclaredAnnotations(); // This is test method so if it only contains @Test annotation then we don't need to hassel with substitutions parameterType = methodDeclaredAnnotations.length <= 1 ? clazz : ClassModifier.getModifiedClass(clazz, methodDeclaredAnnotations); } catch (Exception e) { throw new RuntimeException("Cannot substitute annotations for method " + method.getName(), e); } value = WebResourceFactory.newResource(parameterType, jerseyWebTarget); } return value; }
: ClassModifier.getModifiedClass(clazz, methodDeclaredAnnotations); } catch (Exception e) { throw new RuntimeException("Cannot substitute annotations for method " + method.getName(), e);
private static javassist.bytecode.annotation.Annotation toJavassist(Annotation annotation, ConstPool constpool, ClassPool classPool) throws NotFoundException, InvocationTargetException, IllegalAccessException { final javassist.bytecode.annotation.Annotation newAnnotation = new javassist.bytecode.annotation.Annotation( annotation.annotationType().getCanonicalName(), constpool); for (Method method : annotation.annotationType().getDeclaredMethods()) { final Object value = method.invoke(annotation); Class<?> componentType = null; if (method.getReturnType().isArray()) { componentType = method.getReturnType().getComponentType(); } newAnnotation.addMemberValue(method.getName(), toMemberValue(value, componentType, constpool, classPool)); } return newAnnotation; }
final Annotation[] methodDeclaredAnnotations = method.getDeclaredAnnotations(); parameterType = methodDeclaredAnnotations.length <= 1 ? clazz : ClassModifier.getModifiedClass(clazz, methodDeclaredAnnotations); } catch (Exception e) { throw new RuntimeException("Cannot substitute annotations for method " + method.getName(), e);
final Annotation[] methodDeclaredAnnotations = method.getDeclaredAnnotations(); parameterType = methodDeclaredAnnotations.length <= 1 ? clazz : ClassModifier.getModifiedClass(clazz, methodDeclaredAnnotations); } catch (Exception e) { throw new RuntimeException("Cannot substitute annotations for method " + method.getName(), e);
: ClassModifier.getModifiedClass(clazz, methodDeclaredAnnotations); } catch (Exception e) { throw new RuntimeException("Cannot substitute annotations for method " + method.getName(), e);