com.sun.tools.xjc.outline.Aspect Java Examples

The following examples show how to use com.sun.tools.xjc.outline.Aspect. 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: XJCCMInfoFactory.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected NClass getClazz(final Class<?> _clas) {
	return new NClass() {

		@Override
		public boolean isBoxedType() {
			return false;
		}

		@Override
		public String fullName() {
			return _clas.getName();
		}

		@Override
		public JClass toType(Outline o, Aspect aspect) {
			return o.getCodeModel().ref(_clas);
		}

		@Override
		public boolean isAbstract() {
			return false;
		}
	};
}
 
Example #2
Source File: XJCCMInfoFactory.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected NClass getClazz(final String className) {
	return new NClass() {

		@Override
		public boolean isBoxedType() {
			return false;
		}

		@Override
		public String fullName() {
			return className;
		}

		@Override
		public JClass toType(Outline o, Aspect aspect) {
			return o.getCodeModel().ref(className);
		}

		@Override
		public boolean isAbstract() {
			return false;
		}
	};
}
 
Example #3
Source File: XJCCMInfoFactory.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
protected NType createListType(final NType elementType) {

	return new NClass() {

		public boolean isBoxedType() {
			return false;
		}

		public String fullName() {
			return List.class.getName();
		}

		public JClass toType(Outline o, Aspect aspect) {
			return o.getCodeModel().ref(List.class)
					.narrow(elementType.toType(o, aspect).boxify());
		}

		public boolean isAbstract() {
			return false;
		}
	};
}
 
Example #4
Source File: FieldUtils.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Set<JType> getPossibleTypes(FieldOutline fieldOutline,
		Aspect aspect) {
	Validate.notNull(fieldOutline);
	final ClassOutline classOutline = fieldOutline.parent();
	final Outline outline = classOutline.parent();
	final CPropertyInfo propertyInfo = fieldOutline.getPropertyInfo();

	final Set<JType> types = new HashSet<JType>();

	if (propertyInfo.getAdapter() != null) {
		types.add(propertyInfo.getAdapter().customType.toType(fieldOutline
				.parent().parent(), aspect));
	} else if (propertyInfo.baseType != null) {
		types.add(propertyInfo.baseType);
	} else {
		Collection<? extends CTypeInfo> typeInfos = propertyInfo.ref();
		for (CTypeInfo typeInfo : typeInfos) {
			types.addAll(getPossibleTypes(outline, aspect, typeInfo));
		}
	}
	return types;
}
 
Example #5
Source File: FieldUtils.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static Set<JType> getPossibleTypes(Outline outline, Aspect aspect,
		CTypeInfo typeInfo) {

	final Set<JType> types = new HashSet<JType>();

	types.add(typeInfo.getType().toType(outline, aspect));
	if (typeInfo instanceof CElementInfo) {

		final CElementInfo elementInfo = (CElementInfo) typeInfo;
		for (CElementInfo substitutionMember : elementInfo
				.getSubstitutionMembers()) {
			types.addAll(getPossibleTypes(outline, aspect,
					substitutionMember));
		}
	}
	return types;
}
 
Example #6
Source File: EnumValuePlugin.java    From jaxb2-basics with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void processEnumOutline(EnumOutline enumOutline) {
	CEnumLeafInfo enumLeafInfo = enumOutline.target;
	JClass enumType = enumLeafInfo.base.toType(enumOutline.parent(),
			Aspect.EXPOSED).boxify();

	final JDefinedClass theClass = enumOutline.clazz;

	ClassUtils._implements(theClass, theClass.owner().ref(EnumValue.class)
			.narrow(enumType));

	final JMethod enumValue$enumValue = theClass.method(JMod.PUBLIC,
			enumType, "enumValue");
	enumValue$enumValue.annotate(Override.class);
	enumValue$enumValue.body()._return(JExpr._this().invoke("value"));
}
 
Example #7
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private JType getTagRefType(final PropertyOutline.TagRef tagRef, final Outline outline, final Aspect aspect) {
	final TypeInfo<NType, NClass> typeInfo = tagRef.getTypeInfo();
	final JType type;
	if (typeInfo instanceof CClassInfo) {
		type = ((CClassInfo) typeInfo).toType(outline, aspect);
	} else if (typeInfo instanceof CElementInfo) {
		final List<CNonElement> refs = ((CElementInfo) typeInfo).getProperty().ref();
		// This feels dirty but am not sure what we do if we get multiple refs
		if (refs.size() == 1) {
			try {
				type = ((CClassInfo) refs.get(0)).toType(outline, aspect);
			} catch (Exception e) {
				throw new RuntimeException(String.format("Unexpected type %s for tagRef %s",
						refs.get(0).getClass().getCanonicalName(),
						tagRef.getTagName()));
			}
		} else {
			throw new RuntimeException(String.format("Expecting one ref type for tagRef %s, found %s",
					tagRef.getTagName(),
					refs.size()));
		}
	} else {
		throw new RuntimeException(String.format("Unexpected type %s for tagRef %s",
				typeInfo.getClass().getCanonicalName(),
				tagRef.getTagName()));
	}
	return type;
}
 
Example #8
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 5 votes vote down vote up
private void generateCollectionChoiceProperty(final PropertyOutline propertyOutline) {
	for (final PropertyOutline.TagRef tagRef : propertyOutline.getChoiceProperties()) {
		final TypeInfo<NType,NClass> typeInfo = tagRef.getTypeInfo();
		final QName elementName = tagRef.getTagName();
		final JType elementType = typeInfo.getType().toType(this.pluginContext.outline, Aspect.EXPOSED);
		generateAddMethods(
				propertyOutline,
				elementName,
				elementType,
				propertyOutline.getSchemaAnnotationText(tagRef).orElse(null));
	}
}
 
Example #9
Source File: ClassModelBuilder.java    From mxjc with MIT License 5 votes vote down vote up
/**
 * Build class name to element name mapping
 * 
 * @param outline, JAXB schema/code model
 * @return class name to element name map
 */
private static Map<String, QName> buildClass2ElementMapping(Outline outline) {
	Map<String, QName> mapping = new HashMap<String, QName>();
	for(CElementInfo ei : outline.getModel().getAllElements()) {
        JType exposedType = ei.getContentInMemoryType().toType(outline,Aspect.EXPOSED);
        mapping.put(exposedType.fullName(), ei.getElementName());
	}
	return mapping;
}
 
Example #10
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 #11
Source File: BuilderGenerator.java    From jaxb2-rich-contract-plugin with MIT License 4 votes vote down vote up
private void generateSingularChoiceProperty(final JBlock initBody, final JVar productParam, final PropertyOutline propertyOutline) {
	// First create the builder field, init and withXXX methods for the supertype of the choices
    JFieldVar superTypeBuilderField = generateSingularChoiceSuperTypeProperty(initBody, productParam, propertyOutline)
			.orElseThrow(() -> new RuntimeException(String.format(
			        "Expecting to have a builderField for property %s", propertyOutline.getFieldName())));

	// Now create the withXXX methods for each choice type
	for (final PropertyOutline.TagRef typeInfo : propertyOutline.getChoiceProperties()) {
		final QName elementName = typeInfo.getTagName();
		final JType elementType = getTagRefType(typeInfo, this.pluginContext.outline, Aspect.EXPOSED);
		final String fieldName = this.pluginContext.toVariableName(elementName.getLocalPart());
		final String propertyName = this.pluginContext.toPropertyName(elementName.getLocalPart());
		final BuilderOutline childBuilderOutline = getBuilderDeclaration(elementType);
		if (childBuilderOutline == null) {
		    // TODO not sure when we will come in here so throw ex to highlight in testing
			throw new RuntimeException(String.format(
					"Don't think we should ever come in here, fieldName: %s", propertyOutline.getFieldName()));
		} else {
			final JClass builderFieldElementType = childBuilderOutline.getBuilderClass().narrow(this.builderClass.type);
			final JClass builderWithMethodReturnType = childBuilderOutline.getBuilderClass().narrow(this.builderClass.type.wildcard());
			final JMethod withValueMethod = this.builderClass.raw.method(JMod.PUBLIC, this.builderClass.type, PluginContext.WITH_METHOD_PREFIX + propertyName);
			final JVar param = withValueMethod.param(JMod.FINAL, elementType, fieldName);
			generateWithMethodJavadoc(
					withValueMethod, param, propertyOutline.getSchemaAnnotationText(typeInfo).orElse(null));
			final JMethod withBuilderMethod = this.builderClass.raw.method(JMod.PUBLIC, builderWithMethodReturnType, PluginContext.WITH_METHOD_PREFIX + propertyName);
			generateBuilderMethodJavadoc(
			        withBuilderMethod,
					"with",
					fieldName,
					propertyOutline.getSchemaAnnotationText(typeInfo).orElse(null));
			if (this.implement) {
				// Generate the withXXX method that takes a value and returns the parent builder
				withValueMethod.body().assign(
						JExpr._this().ref(superTypeBuilderField),
						nullSafe(param, JExpr._new(builderFieldElementType).arg(JExpr._this()).arg(param).arg(JExpr.FALSE)));
				withValueMethod.body()._return(JExpr._this());

				// Generate the withXXX method that takes no args and returns a new child builder for that type
				JVar childBuilder = withBuilderMethod.body().decl(
				        JMod.FINAL,
						builderFieldElementType,
						fieldName + this.settings.getBuilderFieldSuffix(),
						JExpr._new(builderFieldElementType)
								.arg(JExpr._this())
								.arg(JExpr._null())
								.arg(JExpr.FALSE));

				withBuilderMethod.body().assign(
						JExpr._this().ref(superTypeBuilderField),
						childBuilder);
				withBuilderMethod.body()._return(childBuilder);
			}
		}
	}
}