private JInvocation illegalArgumentInvocation(JDefinedClass jclass, String propertyName, JType propertyType, JVar valueVar) { return _new(jclass.owner()._ref(IllegalArgumentException.class)) .arg(lit("property \"" + propertyName + "\" is of type \"" + propertyType.fullName() + "\", but got ") .plus(valueVar.invoke("getClass").invoke("toString"))); }
private void addToString(JDefinedClass _enum, JFieldVar valueField) { JMethod toString = _enum.method(JMod.PUBLIC, String.class, "toString"); JBlock body = toString.body(); JExpression toReturn = JExpr._this().ref(valueField); if(!isString(valueField.type())){ toReturn = toReturn.plus(JExpr.lit("")); } body._return(toReturn); toString.annotate(Override.class); }
private void addFactoryMethod(JDefinedClass _enum, JType backingType) { JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType); JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue"); JVar valueParam = fromValue.param(backingType, "value"); JBlock body = fromValue.body(); JVar constant = body.decl(_enum, "constant"); constant.init(quickLookupMap.invoke("get").arg(valueParam)); JConditional _if = body._if(constant.eq(JExpr._null())); JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class)); JExpression expr = valueParam; // if string no need to add "" if(!isString(backingType)){ expr = expr.plus(JExpr.lit("")); } illegalArgumentException.arg(expr); _if._then()._throw(illegalArgumentException); _if._else()._return(constant); ruleFactory.getAnnotator().enumCreatorMethod(_enum, fromValue); }
protected void addToString() { definedClass.method(JMod.PUBLIC, String.class, "toString") .body()._return(nameField.plus(JExpr.lit(": ")).plus(oidStrField)); }
@Override protected void generateMethod(final ClassRepresentation clazz, final List<JFieldVar> excludeFields, final boolean hasSuperClassFields) { final JDefinedClass implClass = clazz.getClassOutline().implClass; final JMethod method = implClass.method(JMod.PUBLIC, String.class, OPERATION); method.annotate(Override.class); JExpression invocation = null; if (hasSuperClassFields) { invocation = JExpr._super().invoke(OPERATION); } final List<JFieldVar> clonedList = new ArrayList<JFieldVar>(); final Collection<JFieldVar> fields = implClass.fields().values(); if (fields != null && !fields.isEmpty()) { clonedList.addAll(implClass.fields().values()); final JFieldVar last = Iterables.getLast(clonedList); for (final JFieldVar field : clonedList) { if (excludeFields == null || !excludeFields.contains(field)) { final String lastChar = field.name().equals(last.name()) ? ") " : "), "; if (invocation != null) { invocation = JOp.plus(invocation, JExpr.lit("(" + field.name() + " = ")).plus(JExpr.ref(field.name())).plus(JExpr.lit(lastChar)); } else { invocation = JExpr.lit("(" + field.name() + " = ").plus(JExpr.ref(field.name())).plus(JExpr.lit(lastChar)); } } } } final JExpression returnValue = invocation == null ? JExpr.lit("") : invocation; method.body()._return(returnValue); }
private JInvocation illegalArgumentInvocation(JDefinedClass jclass, String propertyName, JType propertyType, JVar valueVar) { return _new(jclass.owner()._ref(IllegalArgumentException.class)) .arg(lit("property \"" + propertyName + "\" is of type \"" + propertyType.fullName() + "\", but got ") .plus(valueVar.invoke("getClass").invoke("toString"))); }
private void addToString(JDefinedClass _enum, JFieldVar valueField) { JMethod toString = _enum.method(JMod.PUBLIC, String.class, "toString"); JBlock body = toString.body(); JExpression toReturn = JExpr._this().ref(valueField); if(!isString(valueField.type())){ toReturn = toReturn.plus(JExpr.lit("")); } body._return(toReturn); toString.annotate(Override.class); }
public void generateToString() { JMethod toString1 = cls().method(JMod.PUBLIC, cm.ref("java.lang.String"), "toString"); toString1.body()._return(ref("this").invoke("toString").arg(lit(""))); JMethod toString2 = cls().method(JMod.PUBLIC, cm.ref("java.lang.String"), "toString"); toString2.param(String.class, "indent"); toString2.body().decl(cm.ref("java.lang.String"), "rc", lit("")); for (int i = 0; i < amqpFields.size(); i++) { Attribute attr = amqpFields.get(i); if ( attr.attribute.type().isArray() ) { JBlock block = toString2.body()._if(_this().ref(attr.attribute).ne(_null()))._then().block(); block._if(ref("rc").invoke("length").ne(lit(0)))._then().assignPlus(ref("rc"), lit(",\n")); block.assignPlus(ref("rc"), ref("indent").plus(lit(" " + attr.attribute.name() + ":").plus(cm.ref("java.util.Arrays").staticInvoke("toString").arg(_this().ref(attr.attribute))))); } else { JExpression value = _this().ref(attr.attribute); if ( generator.getMapping().get(attr.type) == null ) { value = value.invoke("toString").arg(ref("indent").plus(lit(" "))); } JBlock block = toString2.body()._if(_this().ref(attr.attribute).ne(_null()))._then().block(); block._if(ref("rc").invoke("length").ne(lit(0)))._then().assignPlus(ref("rc"), lit(",\n")); block.assignPlus(ref("rc"), ref("indent").plus(lit(" " + attr.attribute.name() + ":").plus(value))); } } toString2.body()._if(ref("rc").invoke("length").ne(lit(0)))._then(). assign(ref("rc"), lit(", {\n").plus(ref("rc").plus(lit("}")))); toString2.body()._return(lit("[" + toJavaClassName(type.getName())).plus(ref("rc")).plus(lit("]"))); }
private void addFactoryMethod(JDefinedClass _enum, JType backingType) { JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType); JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue"); JVar valueParam = fromValue.param(backingType, "value"); JBlock body = fromValue.body(); JVar constant = body.decl(_enum, "constant"); constant.init(quickLookupMap.invoke("get").arg(valueParam)); JConditional _if = body._if(constant.eq(JExpr._null())); JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class)); JExpression expr = valueParam; // if string no need to add "" if(!isString(backingType)){ expr = expr.plus(JExpr.lit("")); } illegalArgumentException.arg(expr); _if._then()._throw(illegalArgumentException); _if._else()._return(constant); ruleFactory.getAnnotator().enumCreatorMethod(_enum, fromValue); }
logExitInvoke.arg(tag); JExpression exitMessage = JExpr.lit("Exiting " + methodName + ", duration in ms: ").plus(durationDeclaration); logExitInvoke.arg(exitMessage); finallyBlock.add(logExitInvoke);
valueOf.body()._throw(_new(cm.ref("java.lang.IllegalArgumentException")).arg(lit("invalid "+toJavaClassName(type.getName())+" value: ").plus(ref("value")))); } else if ( isBase(Short.class) || isBase(Integer.class) || isBase(Long.class)) { JMethod valueOf = cls().method(JMod.PUBLIC | JMod.STATIC, cls(), "valueOf"); valueOf.body()._throw(_new(cm.ref("java.lang.IllegalArgumentException")).arg(lit("invalid "+toJavaClassName(type.getName())+" value: ").plus(ref("value"))));
readSwitchBlock._default().body()._throw(_new(cm.ref(Exception.class)).arg(lit("Unknown format code for " + type.getName() + " : 0x").plus(cm.ref("java.lang.String").staticInvoke("format").arg("%x").arg(ref("formatCode"))))); staticReadSwitchBlock._default().body()._throw(_new(cm.ref(Exception.class)).arg(lit("Unknown format code for " + type.getName() + " : 0x").plus(cm.ref("java.lang.String").staticInvoke("format").arg("%x").arg(ref("formatCode"))))); writeBodySwitchBlock._default().body()._throw(_new(cm.ref(Exception.class)).arg(lit("Unknown format code for " + type.getName() + " : 0x").plus(cm.ref("java.lang.String").staticInvoke("format").arg("%x").arg(ref("formatCode")))));