/** * Convert a generic {@link Type} to {@link JType}. * * @param rawType * the raw type * @param type * the generic type * @param jCodeModel * the code model * * @return converted {@link JType}. */ private JType typeToJType(final Class<?> rawType, final Type type, final JCodeModel jCodeModel) { final JType jType = jCodeModel._ref(rawType); if (jType instanceof JPrimitiveType) { return jType; } JClass result = (JClass) jType; if (type instanceof ParameterizedType) { for (final Type typeArgument : ((ParameterizedType) type).getActualTypeArguments()) { if (typeArgument instanceof WildcardType) { result = result.narrow(jCodeModel.wildcard()); } else if (typeArgument instanceof Class) { result = result.narrow(jCodeModel._ref((Class<?>) typeArgument)); } } } return result; }
private void createReportingValueClass(JCodeModel codeModel, String name, JPrimitiveType type) throws JClassAlreadyExistsException { JClass reportClazz = codeModel.ref("org.jpmml.evaluator.Report"); JDefinedClass clazz = codeModel._class(JMod.PUBLIC, "org.jpmml.evaluator.Reporting" + name, ClassType.CLASS); clazz._extends(codeModel.ref("org.jpmml.evaluator." + name)); clazz._implements(codeModel.ref("org.jpmml.evaluator.HasReport")); JFieldVar reportField = clazz.field(JMod.PRIVATE, reportClazz, "report", JExpr._null()); createCopyMethod(clazz); createOperationMethods(clazz, name, type); createReportMethod(clazz); createExpressionMethods(clazz); createAccessorMethods(clazz, reportField); createFormatMethod(clazz, type); }
private void createReportingVectorClass(JCodeModel codeModel, String name, JPrimitiveType type) throws JClassAlreadyExistsException { JDefinedClass clazz = codeModel._class(JMod.ABSTRACT | JMod.PUBLIC, "org.jpmml.evaluator.Reporting" + name, ClassType.CLASS); clazz._extends(codeModel.ref("org.jpmml.evaluator." + name)); JFieldVar expressionField = clazz.field(JMod.PRIVATE, String.class, "expression", JExpr.lit("")); createNewReportMethod(clazz); createOperationMethods(clazz, name, type); createValueMethods(clazz, type); createReportMethod(clazz); createAccessorMethods(clazz, expressionField); }
static private void createReportingConstructor(JDefinedClass clazz, ExecutableElement executableElement, String operation, String initialOperation, JPrimitiveType type){ JCodeModel codeModel = clazz.owner(); JMethod constructor = clazz.constructor(JMod.PUBLIC); List<? extends VariableElement> parameterElements = executableElement.getParameters(); for(VariableElement parameterElement : parameterElements){ constructor.param(toType(codeModel, parameterElement.asType()), String.valueOf(parameterElement.getSimpleName())); } JBlock body = constructor.body(); body.add(createSuperInvocation(clazz, constructor)); if((clazz.name()).endsWith("Value")){ JClass reportClazz = codeModel.ref("org.jpmml.evaluator.Report"); JVar reportParameter = constructor.param(reportClazz, "report"); body.add(JExpr.invoke("setReport").arg(reportParameter)); } // End if if(initialOperation != null){ throw new RuntimeException(); } body.add(JExpr.invoke("report").arg(createReportInvocation(clazz, operation, constructor.params(), type))); }
static private void createAggregationMethod(JDefinedClass clazz, JClass valueClazz, String name, JExpression valueExpression, String operation, JPrimitiveType type){ JMethod method = clazz.method(JMod.PUBLIC, valueClazz, name); method.annotate(Override.class); JBlock body = method.body(); body._return(JExpr._new(valueClazz).arg(valueExpression).arg(JExpr.invoke("newReport")).arg(createReportInvocation(clazz, operation, Collections.<JVar>emptyList(), type))); }
static private JInvocation createReportInvocation(JDefinedClass clazz, String operation, List<JVar> parameters, JPrimitiveType type){ JCodeModel codeModel = clazz.owner(); JClass stringBuilderClazz = codeModel.ref(StringBuilder.class); return createReportInvocation(clazz, JExpr._new(stringBuilderClazz).arg(JExpr.lit(256)), operation, parameters, type); }
protected JPrimitiveType _void() { return this.codeModel.VOID; }
protected JPrimitiveType _boolean() { return this.codeModel.BOOLEAN; }
public JCMPrimitiveType(JCMTypeFactory factory, JPrimitiveType type) { super(factory, type); }
public static JClass box(JType t) { if (t instanceof JClass) return (JClass) t; else return ((JPrimitiveType) t).boxify(); }
private void createOperationMethods(JDefinedClass clazz, String name, JPrimitiveType type){ Elements elements = super.processingEnv.getElementUtils(); Types types = super.processingEnv.getTypeUtils(); TypeElement operationElement = elements.getTypeElement("org.jpmml.evaluator.Operation"); DeclaredType operationType = types.getDeclaredType(operationElement); ExecutableElement valueMethod = getMethod(operationElement, "value"); ExecutableElement initialValueMethod = getMethod(operationElement, "initialValue"); TypeElement clazzElement = elements.getTypeElement("org.jpmml.evaluator." + name); for(int level = 0; clazzElement != null; level++){ TypeMirror superClazz = clazzElement.getSuperclass(); List<? extends Element> enclosedElements = clazzElement.getEnclosedElements(); for(Element enclosedElement : enclosedElements){ if(enclosedElement instanceof ExecutableElement){ ExecutableElement executableElement = (ExecutableElement)enclosedElement; ElementKind kind = executableElement.getKind(); AnnotationMirror annotationMirror = getAnnotation(executableElement, operationType); if(annotationMirror != null){ Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues(); AnnotationValue valueAttribute = elementValues.get(valueMethod); AnnotationValue initialValueAttribute = elementValues.get(initialValueMethod); if(valueAttribute == null){ throw new RuntimeException(); } switch(kind){ case CONSTRUCTOR: createReportingConstructor(clazz, executableElement, (String)valueAttribute.getValue(), (initialValueAttribute != null ? (String)initialValueAttribute.getValue() : null), type); createConstructor(clazz, executableElement, true); break; case METHOD: createReportingMethod(clazz, executableElement, (String)valueAttribute.getValue(), (initialValueAttribute != null ? (String)initialValueAttribute.getValue() : null), type); break; default: break; } } else { if(level != 0){ continue; } switch(kind){ case CONSTRUCTOR: createConstructor(clazz, executableElement, false); createConstructor(clazz, executableElement, true); break; default: break; } } } } if(superClazz instanceof DeclaredType){ DeclaredType declaredType = (DeclaredType)superClazz; clazzElement = (TypeElement)declaredType.asElement(); continue; } break; } }
/** * Create a primitive type reference (for code generation) using the given * primitive type name. * * @param name * the name of a primitive Java type * @param owner * the current code model for type generation * @return a type reference created by the given owner */ public static JPrimitiveType primitiveType(String name, JCodeModel owner) { try { return (JPrimitiveType) owner.parseType(name); } catch (ClassNotFoundException e) { throw new GenerationException( "Given name does not refer to a primitive type, this type can't be found: " + name, e); } }
static private void createReportingMethod(JDefinedClass clazz, ExecutableElement executableElement, String operation, String initialOperation, JPrimitiveType type){ JCodeModel codeModel = clazz.owner(); JMethod method = clazz.method(JMod.PUBLIC, clazz, String.valueOf(executableElement.getSimpleName())); method.annotate(Override.class); List<? extends VariableElement> parameterElements = executableElement.getParameters(); for(VariableElement parameterElement : parameterElements){ method.param(toType(codeModel, parameterElement.asType()), String.valueOf(parameterElement.getSimpleName())); } JBlock body = method.body(); JVar resultVariable = body.decl(clazz, "result", JExpr.cast(clazz, createSuperInvocation(clazz, method))); if(initialOperation != null){ JConditional ifStatement = body._if(JExpr.invoke("hasExpression")); JBlock trueBlock = ifStatement._then(); trueBlock.add(JExpr.invoke("report").arg(createReportInvocation(clazz, operation, method.params(), type))); JBlock falseBlock = ifStatement._else(); falseBlock.add(JExpr.invoke("report").arg(createReportInvocation(clazz, initialOperation, method.params(), type))); } else { body.add(JExpr.invoke("report").arg(createReportInvocation(clazz, operation, method.params(), type))); } body._return(resultVariable); }
static private void createFormatMethod(JDefinedClass clazz, JPrimitiveType type){ JCodeModel codeModel = clazz.owner(); JClass numberClazz = codeModel.ref(Number.class); JClass stringBuilderClazz = codeModel.ref(StringBuilder.class); JType numberListClazz; try { numberListClazz = codeModel.parseType("java.util.List<? extends java.lang.Number>"); } catch(ClassNotFoundException cnfe){ throw new RuntimeException(cnfe); } JMethod method = clazz.method(JMod.STATIC | JMod.PRIVATE, String.class, "format"); JVar valuesParameter = method.param(numberListClazz, "values"); JBlock body = method.body(); JVar sbVariable = body.decl(stringBuilderClazz, "sb", JExpr._new(stringBuilderClazz).arg(valuesParameter.invoke("size").mul(JExpr.lit(32)))); JForEach forStatement = body.forEach(numberClazz, "value", valuesParameter); JBlock forBody = forStatement.body(); forBody.add(createReportInvocation(clazz, sbVariable, "${0}", Collections.singletonList(forStatement.var()), type)); body._return(sbVariable.invoke("toString")); }