com.sun.codemodel.JPrimitiveType Java Examples

The following examples show how to use com.sun.codemodel.JPrimitiveType. 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: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private void createReportingValueClass(JCodeModel codeModel, Class<? extends Value<?>> valueClazz, JPrimitiveType type) throws JClassAlreadyExistsException {
	JClass reportClazz = codeModel.ref(Report.class);

	JDefinedClass reportingValueClazz = codeModel._class(JMod.PUBLIC, asReportingClass(valueClazz), ClassType.CLASS);
	reportingValueClazz._extends(codeModel.ref(valueClazz));
	reportingValueClazz._implements(codeModel.ref(HasReport.class));

	JFieldVar reportField = reportingValueClazz.field(JMod.PRIVATE, reportClazz, "report", JExpr._null());

	createCopyMethod(reportingValueClazz);
	createOperationMethods(reportingValueClazz, valueClazz, type);
	createReportMethod(reportingValueClazz);
	createExpressionMethods(reportingValueClazz);
	createAccessorMethods(reportingValueClazz, reportField);
	createFormatMethod(reportingValueClazz, type);
}
 
Example #2
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private void createReportingVectorClass(JCodeModel codeModel, Class<? extends Vector<?>> vectorClazz, JPrimitiveType type) throws JClassAlreadyExistsException {
	JDefinedClass reportingVectorClazz = codeModel._class(JMod.ABSTRACT | JMod.PUBLIC, asReportingClass(vectorClazz), ClassType.CLASS);
	reportingVectorClazz._extends(codeModel.ref(vectorClazz));

	JFieldVar expressionField = reportingVectorClazz.field(JMod.PRIVATE, String.class, "expression", JExpr.lit(""));

	createNewReportMethod(reportingVectorClazz);
	createOperationMethods(reportingVectorClazz, vectorClazz, type);
	createValueMethods(reportingVectorClazz, type);
	createReportMethod(reportingVectorClazz);
	createAccessorMethods(reportingVectorClazz, expressionField);
}
 
Example #3
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
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(Report.class);

		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)));
}
 
Example #4
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
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.emptyList(), type)));
}
 
Example #5
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
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);
}
 
Example #6
Source File: JCMPrimitiveType.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public JCMPrimitiveType(JCMTypeFactory factory, JPrimitiveType type) {
	super(factory, type);
}
 
Example #7
Source File: CodeModelUtils.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static JClass box(JType t) {
	if (t instanceof JClass)
		return (JClass) t;
	else
		return ((JPrimitiveType) t).boxify();
}
 
Example #8
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private void createReportingMethod(JDefinedClass clazz, ExecutableElement executableElement, String operation, String initialOperation, JPrimitiveType type){
	JCodeModel codeModel = clazz.owner();

	String name = String.valueOf(executableElement.getSimpleName());

	JMethod method = clazz.method(JMod.PUBLIC, clazz, name);
	method.annotate(Override.class);

	List<JVar> params = new ArrayList<>();

	List<? extends VariableElement> parameterElements = executableElement.getParameters();
	for(VariableElement parameterElement : parameterElements){
		TypeMirror paramType = parameterElement.asType();
		Name paramName = parameterElement.getSimpleName();

		JVar param;

		if((TypeKind.ARRAY).equals(paramType.getKind())){
			ArrayType arrayType = (ArrayType)paramType;

			param = method.varParam(toType(codeModel, arrayType.getComponentType()), String.valueOf(paramName));
		} else

		{
			param = method.param(toType(codeModel, paramType), String.valueOf(paramName));
		}

		params.add(param);
	}

	String valueMethod;

	if((codeModel.DOUBLE).equals(type)){
		valueMethod = "doubleValue";
	} else

	if((codeModel.FLOAT).equals(type)){
		valueMethod = "floatValue";
	} else

	{
		throw new IllegalArgumentException();
	}

	boolean checkChange;

	switch(name){
		case "add":
		case "subtract":
		case "multiply":
		case "divide":
			checkChange = (clazz.name()).endsWith("Value");
			break;
		default:
			checkChange = false;
			break;
	}

	JBlock body = method.body();

	JVar oldValueVar = null;
	if(checkChange){
		oldValueVar = body.decl(type, "oldValue", JExpr.invoke(valueMethod));
	}

	JVar resultVariable = body.decl(clazz, "result", JExpr.cast(clazz, createSuperInvocation(clazz, method)));

	JVar newValueVar = null;
	if(checkChange){
		newValueVar = body.decl(type, "newValue", JExpr.invoke(valueMethod));
	}

	JBlock block = body;
	if(checkChange){
		block = body._if((oldValueVar).ne(newValueVar))._then();
	}

	if(initialOperation != null){
		JConditional ifStatement = block._if(JExpr.invoke("hasExpression"));

		JBlock trueBlock = ifStatement._then();

		trueBlock.add(JExpr.invoke("report").arg(createReportInvocation(clazz, operation, params, type)));

		JBlock falseBlock = ifStatement._else();

		falseBlock.add(JExpr.invoke("report").arg(createReportInvocation(clazz, initialOperation, params, type)));
	} else

	{
		block.add(JExpr.invoke("report").arg(createReportInvocation(clazz, operation, params, type)));
	}

	body._return(resultVariable);
}
 
Example #9
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private void createFormatMethod(JDefinedClass clazz, JPrimitiveType type){
	JCodeModel codeModel = clazz.owner();

	JClass numberClazz = codeModel.ref(Number.class);
	JClass stringBuilderClazz = codeModel.ref(StringBuilder.class);

	JMethod method = clazz.method(JMod.STATIC | JMod.PRIVATE, String.class, "format");

	JVar valuesParameter = method.varParam(numberClazz, "values");

	JBlock body = method.body();

	JVar sbVariable = body.decl(stringBuilderClazz, "sb", JExpr._new(stringBuilderClazz).arg(valuesParameter.ref("length").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"));
}