本文整理汇总了Java中com.sun.codemodel.JFormatter类的典型用法代码示例。如果您正苦于以下问题:Java JFormatter类的具体用法?Java JFormatter怎么用?Java JFormatter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JFormatter类属于com.sun.codemodel包,在下文中一共展示了JFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateWriter
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
protected JBlock generateWriter(AcScheme scheme, JavaWriter writer) {
JBlock bl=new JBlock();
for (AbstractType tp:scheme.getSchemes().keySet()){
String name = getName(tp, writer);
String ge="value.get"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"()";
JBlock th=bl._if(JExpr.direct(ge+"!=null"))._then();
th.add(new JStatement() {
@Override
public void state(JFormatter f) {
f.p("gson.toJson("+ge+","+ge+".getClass(), out);");
f.nl();
}
});
}
return bl;
}
示例2: generateWriter
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
protected JBlock generateWriter(AcScheme scheme, JavaWriter writer) {
JBlock bl=new JBlock();
for (AbstractType tp:scheme.getSchemes().keySet()){
String name = getName(tp, writer);
String ge="value.get"+Character.toUpperCase(name.charAt(0))+name.substring(1)+"()";
JBlock th=bl._if(JExpr.direct(ge+"!=null"))._then();
th.add(new JStatement() {
@Override
public void state(JFormatter f) {
f.p("gen.writeObject("+ge+");");
f.nl();
}
});
}
return bl;
}
示例3: customize
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
@Override
public void customize(ClassCustomizerParameters parameters) {
parameters.clazz._implements(Cloneable.class);
parameters.clazz.method(JMod.PUBLIC, parameters.clazz, "clone").body().add(new JStatement() {
@Override
public void state(JFormatter f) {
f.p("try {");
f.nl();
f.p("return ("+parameters.clazz.name()+")super.clone();");
f.nl();
f.p("} catch (CloneNotSupportedException e){ throw new IllegalStateException(e);}");
f.nl();
}
});
}
示例4: doReader
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
private String doReader(AcScheme scheme, JavaWriter writer, String template) {
JBlock bl=generateAc(scheme,writer);
StringWriter w = new StringWriter();
JFormatter f = new JFormatter(w);
for (int i=0;i<4;i++){
f.i();
}
bl.generate(f);
template=template.replace((CharSequence)"{acCode}", w.toString());
return template;
}
示例5: assertSimpleAnnotation
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
protected void assertSimpleAnnotation(JMethod method, String annotation, String argument) {
JAnnotationUse annotationUse = method.annotations().stream()
.filter(a -> a.getAnnotationClass().name().equals(annotation)).findFirst().get();
JAnnotationValue value = annotationUse.getAnnotationMembers().get("value");
StringWriter writer = new StringWriter();
JFormatter formatter = new JFormatter(writer);
value.generate(formatter);
assertThat(writer.toString()).isEqualTo(argument);
}
示例6: assertNoArgAnnotation
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
protected void assertNoArgAnnotation(JMethod method, Class<?> annotation) {
JAnnotationUse annotationUse = method.annotations().stream()
.filter(a -> a.getAnnotationClass().name().equals(annotation.getSimpleName())).findFirst().get();
StringWriter writer = new StringWriter();
JFormatter formatter = new JFormatter(writer);
annotationUse.generate(formatter);
assertThat(writer.toString()).isEqualTo("@" + annotation.getName());
}
示例7: DebugStringBuilder
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
public DebugStringBuilder( Object obj ) {
strWriter = new StringWriter( );
writer = new PrintWriter( strWriter );
writer.print( "[" );
writer.print( obj.getClass().getSimpleName() );
writer.print( ": " );
fmt = new JFormatter( writer );
}
示例8: getElementAsString
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
/**
* 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();
}
示例9: generate
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
@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(')');
}
示例10: dotClass
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
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");
}
};
}
}
示例11: handleXmlElement
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
/**
* 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);
}
}
示例12: annotationValueToString
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
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);
}
示例13: param
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
private JAnnotationUse param(final JType type) {
if (type instanceof JClass) {
return annotationUse.param(this.name, (JClass) type);
} else {
return annotationUse.param(this.name, new JExpressionImpl() {
public void generate(JFormatter f) {
f.g(type).p(".class");
}
});
}
}
示例14: param
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
private JAnnotationArrayMember param(final JType type) {
if (type instanceof JClass) {
return annotationArrayMember.param((JClass) type);
} else {
return annotationArrayMember.param(new JExpressionImpl() {
public void generate(JFormatter f) {
f.g(type).p(".class");
}
});
}
}
示例15: updateArrayOfGetters
import com.sun.codemodel.JFormatter; //导入依赖的package包/类
/**
* 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))));
}
}