Java Code Examples for com.sun.codemodel.JDefinedClass#owner()

The following examples show how to use com.sun.codemodel.JDefinedClass#owner() . 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: EqualsPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected JMethod generateObject$equals(final ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();
	final JMethod objectEquals = theClass.method(JMod.PUBLIC,
			codeModel.BOOLEAN, "equals");
	objectEquals.annotate(Override.class);
	{
		final JVar object = objectEquals.param(Object.class, "object");
		final JBlock body = objectEquals.body();
		final JVar equalsStrategy = body.decl(JMod.FINAL,
				codeModel.ref(EqualsStrategy2.class), "strategy",
				createEqualsStrategy(codeModel));
		body._return(JExpr.invoke("equals").arg(JExpr._null())
				.arg(JExpr._null()).arg(object).arg(equalsStrategy));
	}
	return objectEquals;
}
 
Example 2
Source File: ToStringPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected JMethod generateObject$toString(final ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();
	final JMethod object$toString = theClass.method(JMod.PUBLIC,
			codeModel.ref(String.class), "toString");
	object$toString.annotate(Override.class);
	{
		final JBlock body = object$toString.body();

		final JVar toStringStrategy =

		body.decl(JMod.FINAL, codeModel.ref(ToStringStrategy2.class),
				"strategy", createToStringStrategy(codeModel));

		final JVar buffer = body.decl(JMod.FINAL,
				codeModel.ref(StringBuilder.class), "buffer",
				JExpr._new(codeModel.ref(StringBuilder.class)));
		body.invoke("append").arg(JExpr._null()).arg(buffer)
				.arg(toStringStrategy);
		body._return(buffer.invoke("toString"));
	}
	return object$toString;
}
 
Example 3
Source File: CopyablePlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected JMethod generateCopyTo$copyTo(final ClassOutline classOutline,
		final JDefinedClass theClass) {

	final JCodeModel codeModel = theClass.owner();
	final JMethod copyTo$copyTo = theClass.method(JMod.PUBLIC,
			codeModel.ref(Object.class), "copyTo");
	copyTo$copyTo.annotate(Override.class);
	{
		final JVar target = copyTo$copyTo.param(Object.class, "target");

		final JBlock body = copyTo$copyTo.body();
		final JVar copyStrategy = body.decl(JMod.FINAL,
				codeModel.ref(CopyStrategy2.class), "strategy",
				createCopyStrategy(codeModel));

		body._return(JExpr.invoke("copyTo").arg(JExpr._null()).arg(target)
				.arg(copyStrategy));
	}
	return copyTo$copyTo;
}
 
Example 4
Source File: SimpleToStringPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected JMethod generateObject$toString(final ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();
	final JMethod object$toString = theClass.method(JMod.PUBLIC,
			codeModel.ref(String.class), "toString");
	object$toString.annotate(Override.class);
	{
		final JBlock body = object$toString.body();

		final JVar toStringStrategy =

		body.decl(JMod.FINAL, codeModel.ref(ToStringStrategy2.class),
				"strategy", createToStringStrategy(codeModel));

		final JVar buffer = body.decl(JMod.FINAL,
				codeModel.ref(StringBuilder.class), "buffer",
				JExpr._new(codeModel.ref(StringBuilder.class)));
		body.invoke("append").arg(JExpr._null()).arg(buffer)
				.arg(toStringStrategy);
		body._return(buffer.invoke("toString"));
	}
	return object$toString;
}
 
Example 5
Source File: MergeablePlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected JMethod generateMergeFrom$mergeFrom0(
		final ClassOutline classOutline, final JDefinedClass theClass) {

	JCodeModel codeModel = theClass.owner();
	final JMethod mergeFrom$mergeFrom = theClass.method(JMod.PUBLIC,
			codeModel.VOID, "mergeFrom");
	mergeFrom$mergeFrom.annotate(Override.class);
	{
		final JVar left = mergeFrom$mergeFrom.param(Object.class, "left");
		final JVar right = mergeFrom$mergeFrom.param(Object.class, "right");
		final JBlock body = mergeFrom$mergeFrom.body();

		final JVar mergeStrategy = body.decl(JMod.FINAL,
				codeModel.ref(MergeStrategy2.class), "strategy",
				createMergeStrategy(codeModel));

		body.invoke("mergeFrom").arg(JExpr._null()).arg(JExpr._null())
				.arg(left).arg(right).arg(mergeStrategy);
	}
	return mergeFrom$mergeFrom;
}
 
Example 6
Source File: ToStringPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected JMethod generateToString$append(final ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();
	final JMethod toString$append = theClass.method(JMod.PUBLIC,
			codeModel.ref(StringBuilder.class), "append");
	toString$append.annotate(Override.class);
	{

		final JVar locator = toString$append.param(ObjectLocator.class,
				"locator");
		final JVar buffer = toString$append.param(StringBuilder.class,
				"buffer");
		final JVar toStringStrategy = toString$append.param(
				ToStringStrategy2.class, "strategy");

		final JBlock body = toString$append.body();

		body.invoke(toStringStrategy, "appendStart").arg(locator)
				.arg(JExpr._this()).arg(buffer);
		body.invoke("appendFields").arg(locator).arg(buffer)
				.arg(toStringStrategy);
		body.invoke(toStringStrategy, "appendEnd").arg(locator)
				.arg(JExpr._this()).arg(buffer);
		body._return(buffer);
	}
	return toString$append;
}
 
Example 7
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private void createReportMethod(JDefinedClass clazz){
	JCodeModel codeModel = clazz.owner();

	JMethod method = clazz.method(JMod.PRIVATE, void.class, "report");

	JVar expressionParameter = method.param(String.class, "expression");

	JBlock body = method.body();

	if((clazz.name()).endsWith("Value")){
		JClass reportClazz = codeModel.ref(Report.class);
		JClass reportEntryClazz = codeModel.ref(Report.Entry.class);

		JVar reportVariable = body.decl(reportClazz, "report", JExpr.invoke("getReport"));
		JVar entryVariable = body.decl(reportEntryClazz, "entry", JExpr._new(reportEntryClazz).arg(expressionParameter).arg(JExpr.invoke("getValue")));

		body.add(reportVariable.invoke("add").arg(entryVariable));
	} else

	if((clazz.name()).endsWith("Vector")){
		body.add(JExpr.invoke("setExpression").arg(expressionParameter));
	} else

	{
		throw new RuntimeException();
	}
}
 
Example 8
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 9
Source File: SimpleToStringPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected JMethod generateToString$append(final ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();
	final JMethod toString$append = theClass.method(JMod.PUBLIC,
			codeModel.ref(StringBuilder.class), "append");
	toString$append.annotate(Override.class);
	{

		final JVar locator = toString$append.param(ObjectLocator.class,
				"locator");
		final JVar buffer = toString$append.param(StringBuilder.class,
				"buffer");
		final JVar toStringStrategy = toString$append.param(
				ToStringStrategy2.class, "strategy");

		final JBlock body = toString$append.body();

		body.invoke(toStringStrategy, "appendStart").arg(locator)
				.arg(JExpr._this()).arg(buffer);
		body.invoke("appendFields").arg(locator).arg(buffer)
				.arg(toStringStrategy);
		body.invoke(toStringStrategy, "appendEnd").arg(locator)
				.arg(JExpr._this()).arg(buffer);
		body._return(buffer);
	}
	return toString$append;
}
 
Example 10
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private void createNewReportMethod(JDefinedClass clazz){
	JCodeModel codeModel = clazz.owner();

	JClass reportClazz = codeModel.ref(Report.class);

	JMethod method = clazz.method(JMod.ABSTRACT | JMod.PROTECTED, reportClazz, "newReport");
}
 
Example 11
Source File: MetaPlugin.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private JMethod generateVisitMethod(final ClassOutline classOutline) {
	final JDefinedClass definedClass = classOutline.implClass;
	final JMethod visitMethod = definedClass.method(JMod.PUBLIC, definedClass, this.visitMethodName);
	final JCodeModel codeModel = definedClass.owner();
	final JClass visitorType = codeModel.ref(PropertyVisitor.class);
	final JVar visitorParam = visitMethod.param(JMod.FINAL, visitorType, "_visitor_");
	if (classOutline.getSuperClass() != null) {
		visitMethod.body().add(JExpr._super().invoke(this.visitMethodName).arg(visitorParam));
	} else {
		visitMethod.body().add(visitorParam.invoke("visit").arg(JExpr._this()));
	}
	return visitMethod;
}
 
Example 12
Source File: PMMLPlugin.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private void declareField(JDefinedClass _interface, JDefinedClass beanClazz, JFieldVar fieldVar){
	JCodeModel codeModel = _interface.owner();

	JExpression init = codeModel.ref("org.jpmml.model.ReflectionUtil").staticInvoke("getField").arg(beanClazz.dotclass()).arg(fieldVar.name());

	_interface.field(0, Field.class, (beanClazz.name() + "_" + fieldVar.name()).toUpperCase(), init);
}
 
Example 13
Source File: DummyXjcPlugin.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public boolean run(Outline arg0, Options arg1, ErrorHandler arg2) {

    for (ClassOutline classOutline : arg0.getClasses()) {
        JDefinedClass implClass = classOutline.implClass;
        JCodeModel codeModel = implClass.owner();
        JMethod dummyMethod =
            implClass.method(JMod.PUBLIC, codeModel.ref(String.class), "dummy");
        dummyMethod.body()._return(JExpr.lit("dummy"));
    }
    return true;
}
 
Example 14
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private void createConstructor(JDefinedClass clazz, ExecutableElement executableElement, boolean hasExpression){
	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(hasExpression){
		JVar expressionParameter = constructor.param(String.class, "expression");

		body._if(expressionParameter.ne(JExpr._null()))._then().add(JExpr.invoke("report").arg(expressionParameter));
	}
}
 
Example 15
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"));
}
 
Example 16
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 17
Source File: SimpleToStringPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected JMethod generateToString$appendFields(ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();

	final JMethod toString$appendFields = theClass.method(JMod.PUBLIC,
			codeModel.ref(StringBuilder.class), "appendFields");
	toString$appendFields.annotate(Override.class);
	{
		final JVar locator = toString$appendFields.param(
				ObjectLocator.class, "locator");
		final JVar buffer = toString$appendFields.param(
				StringBuilder.class, "buffer");
		final JVar toStringStrategy = toString$appendFields.param(
				ToStringStrategy2.class, "strategy");
		final JBlock body = toString$appendFields.body();

		final Boolean superClassImplementsToString = StrategyClassUtils
				.superClassImplements(classOutline, ignoring,
						ToString2.class);

		if (superClassImplementsToString == null) {
			// No superclass
		} else if (superClassImplementsToString.booleanValue()) {
			body.invoke(JExpr._super(), "appendFields").arg(locator)
					.arg(buffer).arg(toStringStrategy);
		} else {
			// Superclass does not implement ToString
		}

		final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
				classOutline.getDeclaredFields(), getIgnoring());

		if (declaredFields.length > 0) {

			for (final FieldOutline fieldOutline : declaredFields) {
				final JBlock block = body.block();
				final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
						.createFieldAccessor(fieldOutline, JExpr._this());
				final JVar theValue = block.decl(
						fieldAccessor.getType(),
						"the"
								+ fieldOutline.getPropertyInfo().getName(
										true));
				final JExpression valueIsSet = (fieldAccessor.isAlwaysSet() || fieldAccessor
						.hasSetValue() == null) ? JExpr.TRUE
						: fieldAccessor.hasSetValue();

				fieldAccessor.toRawValue(block, theValue);

				block.invoke(toStringStrategy, "appendField")
						.arg(locator)
						.arg(JExpr._this())
						.arg(JExpr.lit(fieldOutline.getPropertyInfo()
								.getName(false))).arg(buffer).arg(theValue)
						.arg(valueIsSet);
			}
		}
		body._return(buffer);
	}
	return toString$appendFields;
}
 
Example 18
Source File: ToStringPlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
protected JMethod generateToString$appendFields(ClassOutline classOutline,
		final JDefinedClass theClass) {
	final JCodeModel codeModel = theClass.owner();

	final JMethod toString$appendFields = theClass.method(JMod.PUBLIC,
			codeModel.ref(StringBuilder.class), "appendFields");
	toString$appendFields.annotate(Override.class);
	{
		final JVar locator = toString$appendFields.param(
				ObjectLocator.class, "locator");
		final JVar buffer = toString$appendFields.param(
				StringBuilder.class, "buffer");
		final JVar toStringStrategy = toString$appendFields.param(
				ToStringStrategy2.class, "strategy");
		final JBlock body = toString$appendFields.body();

		final Boolean superClassImplementsToString = StrategyClassUtils
				.superClassImplements(classOutline, ignoring,
						ToString2.class);

		if (superClassImplementsToString == null) {
			// No superclass
		} else if (superClassImplementsToString.booleanValue()) {
			body.invoke(JExpr._super(), "appendFields").arg(locator)
					.arg(buffer).arg(toStringStrategy);
		} else {
			// Superclass does not implement ToString
		}

		final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
				classOutline.getDeclaredFields(), getIgnoring());

		if (declaredFields.length > 0) {

			for (final FieldOutline fieldOutline : declaredFields) {
				final JBlock block = body.block();
				final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
						.createFieldAccessor(fieldOutline, JExpr._this());
				final JVar theValue = block.decl(
						fieldAccessor.getType(),
						"the"
								+ fieldOutline.getPropertyInfo().getName(
										true));

				final JExpression valueIsSet = (fieldAccessor.isAlwaysSet() || fieldAccessor
						.hasSetValue() == null) ? JExpr.TRUE
						: fieldAccessor.hasSetValue();

				fieldAccessor.toRawValue(block, theValue);

				block.invoke(toStringStrategy, "appendField")
						.arg(locator)
						.arg(JExpr._this())
						.arg(JExpr.lit(fieldOutline.getPropertyInfo()
								.getName(false))).arg(buffer).arg(theValue)
						.arg(valueIsSet);
			}
		}
		body._return(buffer);
	}
	return toString$appendFields;
}
 
Example 19
Source File: SimpleHashCodePlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
	protected void generate(ClassOutline classOutline, JDefinedClass theClass) {

		final JCodeModel codeModel = theClass.owner();
		final JMethod object$hashCode = theClass.method(JMod.PUBLIC,
				codeModel.INT, "hashCode");
		object$hashCode.annotate(Override.class);
		{
			final JBlock body = object$hashCode.body();

			final JExpression currentHashCodeExpression = JExpr.lit(1);

			final JVar currentHashCode = body.decl(codeModel.INT,
					"currentHashCode", currentHashCodeExpression);

			final Boolean superClassImplementsHashCode = StrategyClassUtils
					.superClassNotIgnored(classOutline, getIgnoring());

			if (superClassImplementsHashCode != null) {
				body.assign(
						currentHashCode,
						currentHashCode.mul(JExpr.lit(getMultiplier())).plus(
								JExpr._super().invoke("hashCode")));
			}

			final FieldOutline[] declaredFields = FieldOutlineUtils.filter(
					classOutline.getDeclaredFields(), getIgnoring());

			if (declaredFields.length > 0) {

				for (final FieldOutline fieldOutline : declaredFields) {
					final FieldAccessorEx fieldAccessor = getFieldAccessorFactory()
							.createFieldAccessor(fieldOutline, JExpr._this());
					if (fieldAccessor.isConstant()) {
						continue;
					}
					final JBlock block = body.block();
					block.assign(currentHashCode,
							currentHashCode.mul(JExpr.lit(getMultiplier())));

					String propertyName = fieldOutline.getPropertyInfo()
							.getName(true);
					final JVar value = block.decl(fieldAccessor.getType(),
							"the" + propertyName);

					fieldAccessor.toRawValue(block, value);
					final JType exposedType = fieldAccessor.getType();

					final Collection<JType> possibleTypes = FieldUtils
							.getPossibleTypes(fieldOutline, Aspect.EXPOSED);
					final boolean isAlwaysSet = fieldAccessor.isAlwaysSet();
//					final JExpression hasSetValue = exposedType.isPrimitive() ? JExpr.TRUE
//							: value.ne(JExpr._null());
					
					final JExpression hasSetValue = (fieldAccessor.isAlwaysSet() || fieldAccessor
							.hasSetValue() == null) ? JExpr.TRUE
							: fieldAccessor.hasSetValue();					
					getCodeGenerator().generate(
							block,
							exposedType,
							possibleTypes,
							isAlwaysSet,
							new HashCodeArguments(codeModel, currentHashCode,
									getMultiplier(), value, hasSetValue));
				}
			}
			body._return(currentHashCode);
		}
	}
 
Example 20
Source File: OperationProcessor.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
static
private void createGetMethod(JDefinedClass clazz, JClass valueClazz, JExpression valueExpression){
	JCodeModel codeModel = clazz.owner();

	JMethod method = clazz.method(JMod.PUBLIC, valueClazz, "get");
	method.annotate(Override.class);

	method.param(codeModel.INT, "index");

	JBlock body = method.body();

	body._return(JExpr._new(valueClazz).arg(valueExpression).arg(JExpr.invoke("newReport")));
}