use of com.sun.tools.xjc.outline.FieldOutline in project jaxb-ri by eclipse-ee4j.
the class BeanGenerator method generateFieldDecl.
/**
* Determines the FieldRenderer used for the given FieldUse,
* then generates the field declaration and accessor methods.
*
* The <code>fields</code> map will be updated with the newly
* created FieldRenderer.
*/
private FieldOutline generateFieldDecl(ClassOutlineImpl cc, CPropertyInfo prop) {
FieldRenderer fr = prop.realization;
if (// none is specified. use the default factory
fr == null) {
fr = model.options.getFieldRendererFactory().getDefault();
}
FieldOutline field = fr.generate(cc, prop);
fields.put(prop, field);
return field;
}
use of com.sun.tools.xjc.outline.FieldOutline in project jaxb-ri by eclipse-ee4j.
the class SignatureWriter method dump.
private void dump(ClassOutline ci) throws IOException {
JDefinedClass cls = ci.implClass;
StringBuilder buf = new StringBuilder();
buf.append("interface ");
buf.append(cls.name());
boolean first = true;
Iterator itr = cls._implements();
while (itr.hasNext()) {
if (first) {
buf.append(" extends ");
first = false;
} else {
buf.append(", ");
}
buf.append(printName((JClass) itr.next()));
}
buf.append(" {");
println(buf.toString());
indent++;
// dump the field
for (FieldOutline fo : ci.getDeclaredFields()) {
String type = printName(fo.getRawType());
println(type + ' ' + fo.getPropertyInfo().getName(true) + ';');
}
dumpChildren(cls);
indent--;
println("}");
}
use of com.sun.tools.xjc.outline.FieldOutline in project jaxb-enhanced-navigation by 4Soft-de.
the class EmptyListHandlerPlugin method createBeforeMarshall.
public void createBeforeMarshall(final JCodeModel jCodeModel, final ClassOutline outline) {
// boolean beforeMarshal(Marshaller)
final JDefinedClass targetClass = outline.implClass;
// targetClass.method(JMod.PUBLIC, baseType, getterName)
final JMethod method = targetClass.method(JMod.PUBLIC, jCodeModel.BOOLEAN, "beforeMarshal");
final JVar marshaller = method.param(Marshaller.class, "marshaller");
final JBlock body = method.body();
if (outline.getSuperClass() != null) {
body.invoke(JExpr._super(), "beforeMarshal").arg(marshaller);
}
for (final FieldOutline f : outline.getDeclaredFields()) {
createEmptyCheck(f, body);
}
body._return(JExpr.TRUE);
}
use of com.sun.tools.xjc.outline.FieldOutline in project jstuff by sebthom.
the class FieldInstantiatingPlugin method run.
@Override
public boolean run(final Outline outline, final Options options, final ErrorHandler errorHandler) throws SAXException {
// collect all types defined in the XSD
final List<JType> typeDefs = new ArrayList<>();
for (final ClassOutline classDef : outline.getClasses()) {
typeDefs.add(classDef.implClass);
}
// scan all XSD based classes for field references to other XSD based classes
for (final ClassOutline classDef : outline.getClasses()) {
for (final JFieldVar fieldDecl : classDef.implClass.fields().values()) {
/*
* @XmlElementRefs({
* @XmlElementRef(name = "bike", namespace = "my-config", type = JAXBElement.class, required = false),
* @XmlElementRef(name = "car", namespace = "my-config", type = JAXBElement.class, required = false)
* })
* private List<JAXBElement<?>> bikesAndCars;
*/
final Field memberValueFields = Fields.find(JAnnotationUse.class, "memberValues");
for (final JAnnotationUse a : fieldDecl.annotations()) {
if (//
jakarta.xml.bind.annotation.XmlElementRefs.class.getName().equals(a.getAnnotationClass().binaryName()) || //
"javax.xml.bind.annotation.XmlElementRefs".equals(a.getAnnotationClass().binaryName())) {
for (final JAnnotationUse xmlElementRefAnno : ((JAnnotationArrayMember) a.getAnnotationMembers().get("value")).annotations()) {
final JAnnotationValue requiredAttribute = xmlElementRefAnno.getAnnotationMembers().get("required");
if (requiredAttribute != null) {
((Map<?, ?>) Fields.read(xmlElementRefAnno, memberValueFields)).remove("required");
}
}
}
}
if (!typeDefs.contains(fieldDecl.type())) {
continue;
}
FieldOutline fieldDef = null;
for (final FieldOutline f : classDef.getDeclaredFields()) {
if (f.getPropertyInfo().getName(false).equals(fieldDecl.name())) {
fieldDef = f;
}
}
if (fieldDef == null)
throw new IllegalStateException("FieldOutline not found for " + fieldDecl.name());
boolean doInstantiate = false;
for (final CPluginCustomization pc : findCustomizations(fieldDef.getPropertyInfo().getCustomizations(), CUSTOMIZATION_ENABLED_TAG)) {
pc.markAsAcknowledged();
doInstantiate = true;
}
// initialize field
if (doInstantiate) {
LOG.info("%s#%s = new %s()", classDef.implClass.name(), fieldDecl.name(), fieldDecl.type().name());
fieldDecl.init(JExpr._new(fieldDecl.type()));
} else {
LOG.info("Not instantiating %s#%s", classDef.implClass.name(), fieldDecl.name());
}
}
}
return true;
}
use of com.sun.tools.xjc.outline.FieldOutline in project jaxb-ri by eclipse-ee4j.
the class ObjectFactoryGeneratorImpl method populate.
protected final void populate(ClassOutlineImpl cc, JClass sigType) {
if (!cc.target.isAbstract()) {
JMethod m = objectFactory.method(JMod.PUBLIC, sigType, "create" + cc.target.getSqueezedName());
m.body()._return(JExpr._new(cc.implRef));
// add some jdoc to avoid javadoc warnings in jdk1.4
m.javadoc().append("Create an instance of ").append(cc.ref);
}
// add static factory methods for all the other constructors.
Collection<? extends Constructor> consl = cc.target.getConstructors();
if (consl.size() != 0) {
// if we are going to add constructors with parameters,
// first we need to have a default constructor.
cc.implClass.constructor(JMod.PUBLIC);
}
{
// collision check
String name = cc.target.getSqueezedName();
ClassOutlineImpl existing = valueFactoryNames.put(name, cc);
if (existing != null) {
outline.getErrorReceiver().error(existing.target.getLocator(), Messages.OBJECT_FACTORY_CONFLICT.format(name));
outline.getErrorReceiver().error(cc.target.getLocator(), Messages.OBJECT_FACTORY_CONFLICT_RELATED.format());
return;
}
}
for (Constructor cons : consl) {
// method on ObjectFactory
// [RESULT]
// Foo createFoo( T1 a, T2 b, T3 c, ... ) throws JAXBException {
// return new FooImpl(a,b,c,...);
// }
JMethod m = objectFactory.method(JMod.PUBLIC, cc.ref, "create" + cc.target.getSqueezedName());
JInvocation inv = JExpr._new(cc.implRef);
m.body()._return(inv);
// let's not throw this exception.
// m._throws(codeModel.ref(JAXBException.class));
// add some jdoc to avoid javadoc warnings in jdk1.4
m.javadoc().append("Create an instance of ").append(cc.ref).addThrows(JAXBException.class).append("if an error occurs");
// constructor
// [RESULT]
// FooImpl( T1 a, T2 b, T3 c, ... ) {
// }
JMethod c = cc.implClass.constructor(JMod.PUBLIC);
for (String fieldName : cons.fields) {
CPropertyInfo field = cc.target.getProperty(fieldName);
if (field == null) {
outline.getErrorReceiver().error(cc.target.getLocator(), Messages.ILLEGAL_CONSTRUCTOR_PARAM.format(fieldName));
continue;
}
fieldName = camelize(fieldName);
FieldOutline fo = outline.getField(field);
FieldAccessor accessor = fo.create(JExpr._this());
// declare a parameter on this factory method and set
// it to the field
inv.arg(m.param(fo.getRawType(), fieldName));
JVar $var = c.param(fo.getRawType(), fieldName);
accessor.fromRawValue(c.body(), '_' + fieldName, $var);
}
}
}
Aggregations