private JInvocation getUDFInstance(JCodeModel m) { if (isGenericUDF) { return JExpr._new(m.directClass(genericUdfClazz.getCanonicalName())); } else { return JExpr._new(m.directClass(GenericUDFBridge.class.getCanonicalName())) .arg(JExpr.lit(udfName)) .arg(JExpr.lit(false)) .arg(JExpr.lit(udfClazz.getCanonicalName().toString())); } }
return JExpr.lit(node.asText()); return JExpr.lit(Integer.parseInt(node.asText())); return JExpr._new(fieldType).arg(JExpr.lit(node.asText())); return JExpr.lit(Double.parseDouble(node.asText())); return JExpr._new(fieldType).arg(JExpr.lit(node.asText())); return JExpr.lit(Boolean.parseBoolean(node.asText())); newDateTime.arg(JExpr.lit(millisecs)); stringParseableTypeInstance.arg(JExpr.lit(node.asText())); return stringParseableTypeInstance; return JExpr.lit(Long.parseLong(node.asText())); return JExpr.lit(Float.parseFloat(node.asText())); return invokeCreate.arg(JExpr.lit(node.asText()));
public void addDescribeContents(JDefinedClass jclass) { JMethod method = jclass.method(JMod.PUBLIC, int.class, "describeContents"); method.body()._return(JExpr.lit(0)); }
public static void addSerializableSupport(JDefinedClass jclass) { jclass._implements(Serializable.class); try { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream); processDefinedClassForSerializableSupport(jclass, dataOutputStream); dataOutputStream.flush(); final MessageDigest digest = MessageDigest.getInstance("SHA"); final byte[] digestBytes = digest.digest(byteArrayOutputStream.toByteArray()); long serialVersionUID = 0L; for (int i = Math.min(digestBytes.length, 8) - 1; i >= 0; i--) { serialVersionUID = serialVersionUID << 8 | digestBytes[i] & 0xff; } JFieldVar serialUIDField = jclass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, long.class, "serialVersionUID"); serialUIDField.init(JExpr.lit(serialVersionUID)); } catch (IOException exception) { throw new GenerationException("IOException while generating serialversionUID field while adding serializable support to class: " + jclass.fullName(), exception); } catch (NoSuchAlgorithmException exception) { throw new GenerationException("SHA algorithm not found when trying to generate serialversionUID field while adding serializable support to class: " + jclass.fullName(), exception); } } }
private void addSetPropertyCase(JDefinedClass jclass, JSwitch setterSwitch, String propertyName, JType propertyType, JVar valueVar, JsonNode node) { JBlock setterBody = setterSwitch._case(lit(propertyName)).body(); addSetProperty(jclass, setterBody, propertyName, propertyType, valueVar, node); setterBody._return(TRUE); }
private void addGetPropertyCase(JDefinedClass jclass, JSwitch propertySwitch, String propertyName, JType propertyType, JsonNode node) { JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {}); propertySwitch._case(lit(propertyName)).body() ._return(invoke(propertyGetter)); }
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); }
JVar result = body.decl(jclass.owner().INT, "result", JExpr.lit(1)); if (fieldVar.type().isPrimitive()) { if ("long".equals(fieldVar.type().name())) { fieldHash = JExpr.cast(jclass.owner().INT, fieldRef.xor(fieldRef.shrz(JExpr.lit(32)))); } else if ("boolean".equals(fieldVar.type().name())) { fieldHash = JOp.cond(fieldRef, JExpr.lit(1), JExpr.lit(0)); } else if ("int".equals(fieldVar.type().name())) { fieldHash = fieldRef; JExpression longField = doubleClass.staticInvoke("doubleToLongBits").arg(fieldRef); fieldHash = JExpr.cast(jclass.owner().INT, longField.xor(longField.shrz(JExpr.lit(32)))); } else if ("float".equals(fieldVar.type().name())) { fieldHash = jclass.owner().ref(Float.class).staticInvoke("floatToIntBits").arg(fieldRef); fieldHash = JOp.cond(fieldRef.eq(JExpr._null()), JExpr.lit(0), fieldRef.invoke("hashCode")); body.assign(result, result.mul(JExpr.lit(31)).plus(fieldHash)); body.assign(result, result.mul(JExpr.lit(31)).plus(JExpr._super().invoke("hashCode")));
sub.assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i))); JBlock conditionalBlock = new JBlock(false, false); JConditional jc = conditionalBlock._if(inputVariables[i].getIsSet().ne(JExpr.lit(0))); jc._then().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder()); jc._else().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), JExpr._null()); sub.add(conditionalBlock); } else { sub.assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i))); sub.assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder()); udfEvalCatch.body() ._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())) .arg(JExpr.lit(String.format("GenericUDF.evaluate method failed"))).arg(exVar));
for(int i=0; i<argTypes.length; i++) { sub.assign( oiArray.component(JExpr.lit(i)), oih.staticInvoke("getDrillObjectInspector") .arg(mode.staticInvoke("valueOf").arg(JExpr.lit(argTypes[i].getMode().getNumber()))) .arg(mt.staticInvoke("valueOf").arg(JExpr.lit(argTypes[i].getMinorType().getNumber()))) .arg((((PrimitiveObjectInspector) returnOI).getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.STRING) ? JExpr.lit(true) : JExpr.lit(false))); workspaceJVars[2].component(JExpr.lit(i)), JExpr._new(m.directClass(DrillDeferredObject.class.getCanonicalName()))); udfInitCatch.body() ._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())) .arg(JExpr.lit(String.format("Failed to initialize GenericUDF"))).arg(exVar));
String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node); JType propertyType = jclass.fields().get(fieldName).type(); JExpression condition = lit(propertyName).invoke("equals").arg(nameParam); propertyConditional = propertyConditional == null ? propertyConditional = body._if(condition) : propertyConditional._elseif(condition);
.invoke("append").arg(JExpr.lit('@')) .invoke("append").arg( jclass.owner().ref(Integer.class).staticInvoke("toHexString").arg( jclass.owner().ref(System.class).staticInvoke("identityHashCode").arg(JExpr._this()))) .invoke("append").arg(JExpr.lit('['))); superString.invoke("indexOf").arg(JExpr.lit('['))); JVar contentEnd = superToStringBlock.decl(jclass.owner().INT, "contentEnd", superString.invoke("lastIndexOf").arg(JExpr.lit(']'))); contentStart.gte(JExpr.lit(0)).cand(contentEnd.gt(contentStart))); .arg(contentStart.plus(JExpr.lit(1))) .arg(contentEnd)); ._then().add(sb.invoke("append").arg(JExpr.lit(','))); body.add(sb.invoke("append").arg(JExpr.lit('='))); .arg(JOp.cond( JExpr.refthis(fieldVar.name()).eq(JExpr._null()), JExpr.lit("<null>"), jclass.owner().ref(Arrays.class).staticInvoke("toString") .arg(JExpr.refthis(fieldVar.name())) .invoke("replace").arg(JExpr.lit('[')).arg(JExpr.lit('{')) .invoke("replace").arg(JExpr.lit(']')).arg(JExpr.lit('}')) .invoke("replace").arg(JExpr.lit(", ")).arg(JExpr.lit(","))))); } else { body.add(sb.invoke("append")
JType propertyType = jclass.fields().get(fieldName).type(); JExpression condition = lit(propertyName).invoke("equals").arg(nameParam); if (propertyConditional == null) { propertyConditional = body._if(condition);
case BOOLEAN:{ JConditional jc = block._if(returnValue.eq(JExpr._null())); jc._then().assign(returnValueHolder.ref("isSet"), JExpr.lit(0)); jc._else().assign(returnValueHolder.ref("isSet"), JExpr.lit(1)); JVar castedOI = jc._else().decl( m.directClass(BooleanObjectInspector.class.getCanonicalName()), "castOI", JExpr._null()); booleanJC._then().assign(returnValueHolder.ref("value"), JExpr.lit(1)); booleanJC._else().assign(returnValueHolder.ref("value"), JExpr.lit(0)); jc._then().assign(returnValueHolder.ref("isSet"), JExpr.lit(0)); jc._else().assign(returnValueHolder.ref("isSet"), JExpr.lit(1)); JVar castedOI = jc._else().decl( m.directClass(ByteObjectInspector.class.getCanonicalName()), "castOI", JExpr._null()); jc._then().assign(returnValueHolder.ref("isSet"), JExpr.lit(0)); jc._else().assign(returnValueHolder.ref("isSet"), JExpr.lit(1)); JVar castedOI = jc._else().decl( m.directClass(ShortObjectInspector.class.getCanonicalName()), "castOI", JExpr._null()); jc._then().assign(returnValueHolder.ref("isSet"), JExpr.lit(0)); jc._else().assign(returnValueHolder.ref("isSet"), JExpr.lit(1)); JVar castedOI = jc._else().decl( m.directClass(IntObjectInspector.class.getCanonicalName()), "castOI", JExpr._null()); jc._then().assign(returnValueHolder.ref("isSet"), JExpr.lit(0)); jc._else().assign(returnValueHolder.ref("isSet"), JExpr.lit(1)); JVar castedOI = jc._else().decl( m.directClass(LongObjectInspector.class.getCanonicalName()), "castOI", JExpr._null());
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); }
JExpression result = JExpr.lit(true);
.invoke("getManagedBuffer") .invoke("reallocIfNeeded") .arg(JExpr.lit(1024)) ); return block; .invoke("getManagedBuffer") .invoke("reallocIfNeeded") .arg(JExpr.lit(1024)) ); return block; .invoke("getManagedBuffer") .invoke("reallocIfNeeded") .arg(JExpr.lit(1024)) ); return block; .invoke("getManagedBuffer") .invoke("reallocIfNeeded") .arg(JExpr.lit(1024)) ); return block;
void start(JExpression $valueSize) { // [RESULT] // T[] x = new T[values.size()]; $array = var(arrayT, JExpr.newArray(componentT, $valueSize)); $index = var(int.class,JExpr.lit(0)); }