com.sun.codemodel.JAnnotationValue Java Examples

The following examples show how to use com.sun.codemodel.JAnnotationValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluginImpl.java    From immutable-xjc with MIT License 6 votes vote down vote up
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();
}
 
Example #2
Source File: ConnectPlugin.java    From kafka-connect-transform-xml with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #3
Source File: ConnectPlugin.java    From kafka-connect-transform-xml with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #4
Source File: RamlInterpreterTest.java    From springmvc-raml-plugin with Apache License 2.0 5 votes vote down vote up
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));
}
 
Example #5
Source File: ClassDiscoverer.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example #6
Source File: CreateJAXBElementNameCallback.java    From jaxb-visitor with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #7
Source File: PluginImpl.java    From immutable-xjc with MIT License 5 votes vote down vote up
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;
}
 
Example #8
Source File: RamlInterpreterTest.java    From springmvc-raml-plugin with Apache License 2.0 4 votes vote down vote up
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()));
}