com.sun.codemodel.JFormatter Java Examples
The following examples show how to use
com.sun.codemodel.JFormatter.
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 |
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: JTypedInvocation.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
@Override public void generate(JFormatter f) { if (this.lhs != null) { f = f.g(this.lhs).p('.'); } else if(this.type != null && !this.constructor) { f = f.g(this.type).p('.'); } if (this.type != null && this.constructor) { f = f.p("new").g(this.type).p('('); } else { if (!this.typeArguments.isEmpty()) { f = f.p("<"); boolean first = true; for (final JType typeArg : this.typeArguments) { if (!first) { f = f.p(", "); } else { first = false; } f = f.g(typeArg); } f = f.p(">"); } f = f.p(this.method).p('('); } f.g(this.args); f.p(')'); }
Example #3
Source File: PluginImpl.java From immutable-xjc with MIT License | 5 votes |
private boolean isRequired(JFieldVar field) { return Stream.of(XmlElement.class, XmlAttribute.class) .map(annotationType -> getAnnotation(field.annotations(), annotationType.getCanonicalName()) .map(JAnnotationUse::getAnnotationMembers) .map(annotationValues -> annotationValues.get("required")) .filter(annotationValue -> { StringWriter sw = new StringWriter(); JFormatter f = new JFormatter(sw); annotationValue.generate(f); return sw.toString().equals("true"); }) ).anyMatch(Optional::isPresent); }
Example #4
Source File: PluginImpl.java From immutable-xjc with MIT License | 5 votes |
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 #5
Source File: PluginImpl.java From xero-java-client with Apache License 2.0 | 5 votes |
/** * Update getters to use Java List. For example: * ArrayOfInvoices getInvoices() -> List<Invoice> getInvoices() */ private void updateArrayOfGetters(ClassOutline co, JCodeModel model) { JDefinedClass implClass = co.implClass; List<JMethod> removedMethods = new ArrayList<>(); Iterator<JMethod> iter = implClass.methods().iterator(); while (iter.hasNext()) { JMethod method = iter.next(); if (method.type().name().startsWith("ArrayOf")) { removedMethods.add(method); iter.remove(); } } for (JMethod removed : removedMethods) { // Parse the old code to get the variable name StringWriter oldWriter = new StringWriter(); removed.body().state(new JFormatter(oldWriter)); String oldBody = oldWriter.toString(); String varName = oldBody.substring(oldBody.indexOf("return ") + "return ".length(), oldBody.indexOf(";")); // Build the new method JClass newReturnType = (JClass) ((JDefinedClass) removed.type()).fields().values().iterator().next().type(); JMethod newMethod = implClass.method(removed.mods().getValue(), newReturnType, removed.name()); JFieldVar field = implClass.fields().get(varName); JClass typeParameter = newReturnType.getTypeParameters().get(0); String fieldName = model._getClass(field.type().fullName()).fields().keySet().iterator().next(); newMethod.body()._return( JOp.cond(field.eq(JExpr._null()), JExpr._new(model.ref("java.util.ArrayList").narrow(typeParameter)), field.invoke("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1)))); } }
Example #6
Source File: CreateJAXBElementNameCallback.java From jaxb-visitor with Apache License 2.0 | 5 votes |
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: ClassDiscoverer.java From jaxb-visitor with Apache License 2.0 | 5 votes |
/** * 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 #8
Source File: MetaPlugin.java From jaxb2-rich-contract-plugin with MIT License | 5 votes |
private JExpression dotClass(final JType cl) { if (cl instanceof JClass) { return ((JClass)cl).dotclass(); } else { return new JExpressionImpl() { public void generate(final JFormatter f) { f.g(cl).p(".class"); } }; } }
Example #9
Source File: DebugStringBuilder.java From Bats with Apache License 2.0 | 5 votes |
public DebugStringBuilder( Object obj ) { strWriter = new StringWriter( ); writer = new PrintWriter( strWriter ); writer.print( "[" ); writer.print( obj.getClass().getSimpleName() ); writer.print( ": " ); fmt = new JFormatter( writer ); }
Example #10
Source File: KubernetesCoreTypeAnnotator.java From kubernetes-client with Apache License 2.0 | 5 votes |
private JExpressionImpl literalExpression(String literal) { return new JExpressionImpl() { @Override public void generate(JFormatter f) { f.p(literal); } }; }
Example #11
Source File: ConnectPlugin.java From kafka-connect-transform-xml with Apache License 2.0 | 5 votes |
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 #12
Source File: ConnectPlugin.java From kafka-connect-transform-xml with Apache License 2.0 | 5 votes |
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 #13
Source File: IsNullExpression.java From aem-component-generator with Apache License 2.0 | 5 votes |
@Override public void generate(JFormatter f) { if (this.isNullType) { f.g(this.variable).p(" == null"); } else { f.g(this.variable).p(" != null"); } }
Example #14
Source File: JTypedInvocation.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
@Override public void state(final JFormatter f) { f.g(this).p(";").nl(); }
Example #15
Source File: NestedThisRef.java From jaxb2-rich-contract-plugin with MIT License | 4 votes |
@Override public void generate(final JFormatter f) { f.g(jClass); f.p(".this"); }
Example #16
Source File: DirectExpression.java From dremio-oss with Apache License 2.0 | 4 votes |
public void generate( JFormatter f ) { f.p('(').p(source).p(')'); }
Example #17
Source File: TernaryOperator.java From aem-component-generator with Apache License 2.0 | 4 votes |
@Override public void generate(JFormatter f) { f.g(condition).p(" ? ").g(ifTrue).p(" : ").g(ifFalse); }
Example #18
Source File: DirectExpression.java From Bats with Apache License 2.0 | 4 votes |
@Override public void generate(JFormatter f) { f.p('(').p(source).p(')'); }
Example #19
Source File: CodeModelHelper.java From springmvc-raml-plugin with Apache License 2.0 | 3 votes |
/** * Returns the string equivalent of the code model element * * @param type * The element to stringify * @return the element as a string (generated code) */ public static String getElementAsString(JDeclaration type) { StringBuilder builder = new StringBuilder(); JFormatter jFormatter = new JFormatter(new StringBuilderWriter(builder)); type.declare(jFormatter); return builder.toString(); }
Example #20
Source File: DebugStringBuilder.java From Bats with Apache License 2.0 | votes |
public JFormatter formatter() { return fmt; }