Search in sources :

Example 1 with StringMemberValue

use of javassist.bytecode.annotation.StringMemberValue in project play-cookbook by spinscale.

the class XmlEnhancer method enhanceThisClass.

@Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
    CtClass ctClass = makeClass(applicationClass);
    if (!ctClass.subtypeOf(classPool.get("play.db.jpa.JPABase"))) {
        return;
    }
    if (!hasAnnotation(ctClass, "javax.persistence.Entity")) {
        return;
    }
    ConstPool constpool = ctClass.getClassFile().getConstPool();
    AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
    if (!hasAnnotation(ctClass, "javax.xml.bind.annotation.XmlAccessorType")) {
        Annotation annot = new Annotation("javax.xml.bind.annotation.XmlAccessorType", constpool);
        EnumMemberValue enumValue = new EnumMemberValue(constpool);
        enumValue.setType("javax.xml.bind.annotation.XmlAccessType");
        enumValue.setValue("FIELD");
        annot.addMemberValue("value", enumValue);
        attr.addAnnotation(annot);
        ctClass.getClassFile().addAttribute(attr);
    }
    if (!hasAnnotation(ctClass, "javax.xml.bind.annotation.XmlRootElement")) {
        Annotation annot = new Annotation("javax.xml.bind.annotation.XmlRootElement", constpool);
        String entityName = ctClass.getName();
        String entity = entityName.substring(entityName.lastIndexOf('.') + 1).toLowerCase();
        annot.addMemberValue("name", new StringMemberValue(entity, constpool));
        attr.addAnnotation(annot);
        ctClass.getClassFile().addAttribute(attr);
    }
    applicationClass.enhancedByteCode = ctClass.toBytecode();
    ctClass.defrost();
}
Also used : EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) CtClass(javassist.CtClass) ConstPool(javassist.bytecode.ConstPool) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) Annotation(javassist.bytecode.annotation.Annotation)

Example 2 with StringMemberValue

use of javassist.bytecode.annotation.StringMemberValue in project gwt-test-utils by gwt-test-utils.

the class ConfigurationLoader method visitPatchClasses.

private void visitPatchClasses() {
    final Map<String, Set<CtClass>> patchClassMap = new HashMap<String, Set<CtClass>>();
    ClassVisitor patchClassVisitor = new ClassVisitor() {

        public void visit(CtClass ctClass) {
            try {
                if (ctClass.hasAnnotation(PatchClass.class)) {
                    Annotation annotation = JavassistUtils.getAnnotation(ctClass, PatchClass.class);
                    String classToPatchName = PatchClass.class.getName();
                    ClassMemberValue value = (ClassMemberValue) annotation.getMemberValue("value");
                    if (value != null) {
                        classToPatchName = value.getValue();
                    }
                    if (classToPatchName.equals(PatchClass.class.getName())) {
                        StringMemberValue target = (StringMemberValue) annotation.getMemberValue("target");
                        classToPatchName = (target != null) ? target.getValue() : "";
                    }
                    if (!"".equals(classToPatchName)) {
                        addPatchClass(classToPatchName, ctClass);
                    }
                }
            } catch (ClassNotFoundException e) {
                // should never happen
                throw new GwtTestPatchException(e);
            }
        }

        private void addPatchClass(String targetName, CtClass patchClass) {
            Set<CtClass> patchClasses = patchClassMap.get(targetName);
            if (patchClasses == null) {
                patchClasses = new HashSet<CtClass>();
                patchClassMap.put(targetName, patchClasses);
            }
            patchClasses.add(patchClass);
            LOGGER.debug("Add patch for class '" + targetName + "' : '" + patchClass.getName() + "'");
        }
    };
    ClassesScanner.getInstance().scanPackages(patchClassVisitor, scanPackages);
    // create all patchers
    patcherFactory = new PatcherFactory(patchClassMap);
}
Also used : StringMemberValue(javassist.bytecode.annotation.StringMemberValue) ClassVisitor(com.googlecode.gwt.test.internal.ClassesScanner.ClassVisitor) Annotation(javassist.bytecode.annotation.Annotation) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) CtClass(javassist.CtClass) PatchClass(com.googlecode.gwt.test.patchers.PatchClass)

Example 3 with StringMemberValue

use of javassist.bytecode.annotation.StringMemberValue in project gwt-test-utils by gwt-test-utils.

the class RemoteServiceCreateHandler method getRemoveServiceRelativePath.

private String getRemoveServiceRelativePath(Class<?> clazz) {
    CtClass ctClass = GwtClassPool.getCtClass((clazz));
    Object[] annotations = ctClass.getAvailableAnnotations();
    for (Object o : annotations) {
        if (Proxy.isProxyClass(o.getClass())) {
            AnnotationImpl annotation = (AnnotationImpl) Proxy.getInvocationHandler(o);
            if (annotation.getTypeName().equals(RemoteServiceRelativePath.class.getName())) {
                return ((StringMemberValue) annotation.getAnnotation().getMemberValue("value")).getValue();
            }
        }
    }
    throw new GwtTestRpcException("Cannot find the '@" + RemoteServiceRelativePath.class.getSimpleName() + "' annotation on RemoteService interface '" + clazz.getName() + "'");
}
Also used : CtClass(javassist.CtClass) GwtTestRpcException(com.googlecode.gwt.test.exceptions.GwtTestRpcException) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) AnnotationImpl(javassist.bytecode.annotation.AnnotationImpl) RemoteServiceRelativePath(com.google.gwt.user.client.rpc.RemoteServiceRelativePath)

Aggregations

CtClass (javassist.CtClass)3 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)3 Annotation (javassist.bytecode.annotation.Annotation)2 RemoteServiceRelativePath (com.google.gwt.user.client.rpc.RemoteServiceRelativePath)1 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)1 GwtTestRpcException (com.googlecode.gwt.test.exceptions.GwtTestRpcException)1 ClassVisitor (com.googlecode.gwt.test.internal.ClassesScanner.ClassVisitor)1 PatchClass (com.googlecode.gwt.test.patchers.PatchClass)1 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)1 ConstPool (javassist.bytecode.ConstPool)1 AnnotationImpl (javassist.bytecode.annotation.AnnotationImpl)1 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)1 EnumMemberValue (javassist.bytecode.annotation.EnumMemberValue)1