Java源码示例:com.sun.codemodel.JAnnotationValue

示例1
private JExpression defaultValue(JFieldVar field) {
    JType javaType = field.type();
    if (setDefaultValuesInConstructor) {
        Optional<JAnnotationUse> xmlElementAnnotation = getAnnotation(field.annotations(), javax.xml.bind.annotation.XmlElement.class.getCanonicalName());
        if (xmlElementAnnotation.isPresent()) {
            JAnnotationValue annotationValue = xmlElementAnnotation.get().getAnnotationMembers().get("defaultValue");
            if (annotationValue != null) {
                StringWriter sw = new StringWriter();
                JFormatter f = new JFormatter(sw);
                annotationValue.generate(f);
                return JExpr.lit(sw.toString().replaceAll("\"", ""));
            }
        }
    }
    if (javaType.isPrimitive()) {
        if (field.type().owner().BOOLEAN.equals(javaType)) {
            return JExpr.lit(false);
        } else if (javaType.owner().SHORT.equals(javaType)) {
            return JExpr.cast(javaType.owner().SHORT, JExpr.lit(0));
        } else {
            return JExpr.lit(0);
        }
    }
    return JExpr._null();
}
 
示例2
String attributeValue(JFieldVar field, Class<?> annotationClass, String param) {
  for (JAnnotationUse annotationUse : field.annotations()) {
    log.trace("isRequired() - name = '{}' getAnnotationClass = '{}'", field.name(), annotationUse.getAnnotationClass().fullName());
    if (annotationUse.getAnnotationClass().fullName().equals(annotationClass.getName())) {
      StringWriter writer = new StringWriter();
      JFormatter formatter = new JFormatter(writer);
      ((JAnnotationValue) annotationUse.getAnnotationMembers().get(param)).generate(formatter);
      return StringUtils.strip(writer.toString(), "\"");
    }
  }
  return null;
}
 
示例3
boolean isRequired(JFieldVar fieldVar) {
  for (JAnnotationUse annotationUse : fieldVar.annotations()) {
    log.trace("isRequired() - name = '{}' getAnnotationClass = '{}'", fieldVar.name(), annotationUse.getAnnotationClass().fullName());
    if (annotationUse.getAnnotationClass().fullName().equals("javax.xml.bind.annotation.XmlElement")) {
      StringWriter writer = new StringWriter();
      JFormatter formatter = new JFormatter(writer);
      ((JAnnotationValue) annotationUse.getAnnotationMembers().get("required")).generate(formatter);
      return Boolean.parseBoolean(writer.toString());
    }
  }
  return false;
}
 
示例4
private void assertPatternValue(JAnnotationUse jAnnotationUse, String expectedPattern) throws Exception {
	JAnnotationValue jAnnotationValue = jAnnotationUse.getAnnotationMembers().get("pattern");
	Field value = jAnnotationValue.getClass().getDeclaredField("value");
	value.setAccessible(true);
	JStringLiteral object = (JStringLiteral) value.get(jAnnotationValue);
	assertThat(object.str, is(expectedPattern));
}
 
示例5
/**
 * Handles the extraction of the schema type from the XmlElement
 * annotation. This was surprisingly difficult. Apparently the
 * model doesn't provide access to the annotation we're referring to
 * so I need to print it and read the string back. Even the formatter
 * itself is final!
 * @param outline root of the generated code
 * @param directClasses set of classes to append to
 * @param type annotation we're analysing
 */
private static void handleXmlElement(Outline outline, Set<String> directClasses, JAnnotationValue type) {
    StringWriter sw = new StringWriter();
    JFormatter jf = new JFormatter(new PrintWriter(sw));
    type.generate(jf);
    String s = sw.toString();
    s = s.substring(0, s.length()-".class".length());
    if (!s.startsWith("java") && outline.getCodeModel()._getClass(s) == null && !foundWithinOutline(s, outline)) {
        directClasses.add(s);
    }
}
 
示例6
private String annotationValueToString(JAnnotationValue ns) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    JFormatter jf = new JFormatter(pw, "");
    ns.generate(jf);
    pw.flush();
    String s = sw.toString();
    return s.substring(1, s.length()-1);
}
 
示例7
private JMethod getGetterProperty(final JFieldVar field, final JDefinedClass clazz) {
    JMethod getter = clazz.getMethod("get" + StringUtils.capitalize(field.name()), NO_ARGS);
    if (getter == null) {
        getter = clazz.getMethod("is" + StringUtils.capitalize(field.name()), NO_ARGS);
    }

    if (getter == null) {
        List<JDefinedClass> superClasses = getSuperClasses(clazz);
        for (JDefinedClass definedClass : superClasses) {
            getter = getGetterProperty(field, definedClass);

            if (getter != null) {
                break;
            }
        }
    }
    if (getter == null) {
        //XJC does not work conform Introspector.decapitalize when multiple upper-case letter are in field name
        Optional<JAnnotationUse> xmlElementAnnotation = getAnnotation(field.annotations(), javax.xml.bind.annotation.XmlElement.class.getCanonicalName());
        if (xmlElementAnnotation.isPresent()) {
            JAnnotationValue annotationValue = xmlElementAnnotation.get().getAnnotationMembers().get("name");
            if (annotationValue != null) {
                StringWriter sw = new StringWriter();
                JFormatter f = new JFormatter(sw);
                annotationValue.generate(f);
                getter = clazz.getMethod("get" + sw.toString().replaceAll("\"", ""), NO_ARGS);
            }
        }
    }
    return getter;
}
 
示例8
private void checkIfAnnotationHasParameter(JDefinedClass classToCheck, Class<?> annotationClass, String field, String param) {
	JAnnotationUse annotation = getAnnotationForGetter(classToCheck, annotationClass, field);
	assertThat(annotation, is(notNullValue()));
	JAnnotationValue annotationParam = annotation.getAnnotationMembers().get(param);
	assertThat(annotationParam, is(notNullValue()));
}