// this GwtCreateHandler has been introduced to make possible the
  // instanciation of abstract classes
  // that gwt-test-utils doesn't patch right now
  public Object create(Class<?> classLiteral) throws Exception {
    if (classLiteral.isAnnotation()
        || classLiteral.isArray()
        || classLiteral.isEnum()
        || classLiteral.isInterface()
        || !Modifier.isAbstract(classLiteral.getModifiers())) {
      return null;
    }

    Class<?> newClass = cache.get(classLiteral);

    if (newClass != null) {
      return newClass.newInstance();
    }

    CtClass ctClass = GwtClassPool.getCtClass(classLiteral);
    CtClass subClass = GwtClassPool.get().makeClass(classLiteral.getCanonicalName() + "SubClass");

    subClass.setSuperclass(ctClass);

    for (CtMethod m : ctClass.getDeclaredMethods()) {
      if (javassist.Modifier.isAbstract(m.getModifiers())) {
        CtMethod copy = new CtMethod(m, subClass, null);
        subClass.addMethod(copy);
      }
    }

    GwtPatcherUtils.patch(subClass, null);

    newClass = subClass.toClass(GwtClassLoader.get(), null);
    cache.put(classLiteral, newClass);

    return newClass.newInstance();
  }
Пример #2
0
  /**
   * Retrieve the String value of an annotation which is not available at runtime.
   *
   * @param clazz The annotated class
   * @param annotation The annotation which is not visible at runtime
   * @param name The name of the String property of the annotation to retrieve
   * @return The String value of the annotation or null if the annotation or its property is not
   *     present
   */
  public static String getInvisibleAnnotationStringValue(
      Class<?> clazz, Class<? extends Annotation> annotation, String name) {
    CtClass ctClass = GwtClassPool.getCtClass(clazz);
    ctClass.defrost();

    AnnotationsAttribute attr =
        (AnnotationsAttribute)
            ctClass.getClassFile().getAttribute(AnnotationsAttribute.invisibleTag);
    if (attr == null) {
      return null;
    }
    javassist.bytecode.annotation.Annotation an = attr.getAnnotation(annotation.getName());

    ctClass.freeze();

    return an != null ? ((StringMemberValue) an.getMemberValue(name)).getValue() : null;
  }