in ide/schema2beans/src/org/netbeans/modules/schema2beansdev/JavaBeanClass.java [365:1075]
public void genAccessors() throws IOException {
int size = attrList.size();
for (int i = 0; i < size; i++) {
Property a = (Property)attrList.get(i);
boolean indexed = a.isIndexed();
boolean isArrayStyle = (config.getIndexedPropertyType() == null);
boolean isWrapper = false;
boolean isScalar = a.isScalar();
String scalarType = a.getScalarType();
String attr = "_"+a.name;
String propertyName = a.beanIntrospectorName();
MetaElement me = getMetaElement(a);
String type = a.getType();
String baseType = type;
if (indexed)
type = baseType + "[]";
Signatures sigs = getSignatures(a);
Signature sig;
//System.out.println("a.name="+a.name+" a.type="+a.type+" a.dtdName="+a.dtdName+" isWrapper="+isWrapper+" a.classType="+a.classType+" me="+me);
select(DECL_SECTION);
if (indexed) {
if (isArrayStyle) {
gen(PRIVATE, baseType+"[]", attr);
gen(" = new ", baseType, "[0]");
eol();
} else {
String paramType = (config.jdkTarget >= 150
? "<"+JavaUtil.toObjectType(baseType)+">"
: "");
gen(PRIVATE, "java.util.List" + paramType, attr);
gen(" = new "+config.getIndexedPropertyType()+paramType+"();");
tabIn();
if (config.jdkTarget < 150)
comment("List<"+baseType+">");
else
cr();
}
} else {
gen(PRIVATE, type, attr);
if (a.getDefaultValue() != null && (a.elementInstance == Common.TYPE_1 || a.elementInstance == Common.TYPE_1_N)) {
gen(" = ");
if (!JavaUtil.checkValueToType(type, a.getDefaultValue())) {
config.messageOut.println(Common.getMessage("MSG_NotAGoodValue", a.getDefaultValue(), type));
}
gen(JavaUtil.instanceFrom(type, a.getDefaultValue()));
eol();
if (config.isOptionalScalars() && a.isScalar()) {
gen(PRIVATE, "boolean", a.getScalarIsSet());
gen(" = true");
eol();
}
} else {
eol();
if (config.isOptionalScalars() && a.isScalar()) {
gen(PRIVATE, "boolean", a.getScalarIsSet());
gen(" = false");
eol();
}
}
}
select(ACCESS_SECTION);
comment("This attribute is ", Common.instanceToString(a.elementInstance));
// Generate setter
if (config.jdkTarget >= 150) {
boolean first = true;
for (Iterator extraDataIt = a.extraDataIterator(); extraDataIt.hasNext(); ) {
Object extraData = extraDataIt.next();
if (extraData instanceof HasAnnotation) {
HasAnnotation annotator = (HasAnnotation) extraData;
if (first)
first = false;
else
jw.write(' ');
jw.write('@');
jw.write(annotator.genAnnotation());
}
}
if (!first)
jw.cr();
}
gen(sigs.findSignature(SETTER));
sp();
begin();
if (a.getPropertyInterface() != null) {
jw.writeEol(type+" value = ("+type+") valueInterface");
}
if (indexed) {
gen("if (value == null)");
cr();
tabIn();
if (baseType.equals("byte[]"))
jw.writeEol("value = new byte[0][0]");
else
jw.writeEol("value = new ", baseType, "[0]");
}
if (!indexed) {
SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a.searchExtraData(SchemaRep.WhiteSpace.class);
if (ws != null)
genWhiteSpaceRestriction(ws, "value", baseType);
}
if (config.isGeneratePropertyEvents() || config.isVetoable()) {
if (indexed) {
jw.beginIf("value.length == "+a.getSizeMethod()+"()");
jw.writeEol("boolean same = true");
jw.beginFor("int i = 0", "i < value.length", "++i");
jw.beginIf("!("+JavaUtil.genEquals(type, "value[i]",
a.getReadMethod(true) + "(i)")+")");
jw.writeEol("same = false");
jw.writeEol("break");
jw.end();
jw.end();
jw.beginIf("same");
} else {
jw.beginIf(JavaUtil.genEquals(type, "value", attr));
}
jw.comment("No change.");
jw.writeEol("return");
jw.end();
if (indexed)
jw.end();
if (a.isBean) {
if (config.isGeneratePropertyEvents())
comment("Make the foreign beans take on our property change event listeners.");
if (config.isGenerateParentRefs())
comment("Maintain the parent reference.");
String iterVar = "value";
if (indexed) {
jw.beginFor("int i = 0", "i < value.length", "++i");
iterVar = "value[i]";
}
jw.beginIf(iterVar+" != null");
if (config.isGeneratePropertyEvents())
geneol(iterVar+"._setPropertyChangeSupport(eventListeners)");
if (config.isVetoable())
geneol(iterVar+"._setVetoableChangeSupport(vetos)");
if (config.isGenerateParentRefs())
jw.writeEol(iterVar+"._setParent(this)");
jw.end();
if (indexed)
jw.end();
}
jw.writeEol("java.beans.PropertyChangeEvent event = null");
gen("if (");
jw.setFirst(" || ");
if (config.isGeneratePropertyEvents()) {
jw.writeNext("eventListeners != null");
}
if (config.isGenerateStoreEvents()) {
jw.writeNext("storeEvents");
}
if (config.isVetoable()) {
jw.writeNext("vetos != null");
}
gen(") ");
begin();
if (indexed) {
jw.comment("See if only 1 thing changed.");
jw.writeEol("int addIndex = -1");
jw.writeEol("int removeIndex = -1");
jw.writeEol("int oldSize = size", a.name, "()");
jw.writeEol("int newSize = value.length");
jw.beginIf("oldSize + 1 == newSize || oldSize == newSize + 1");
jw.writeEol("boolean checkAddOrRemoveOne = true");
jw.writeEol("int oldIndex = 0, newIndex = 0");
jw.beginFor("",
"oldIndex < oldSize && newIndex < newSize",
"++newIndex, ++oldIndex");
//jw.writeEol("System.out.println(\"oldIndex=\"+oldIndex+\" newIndex=\"+newIndex)");
//jw.writeEol("System.out.println(\"addIndex=\"+addIndex+\" removeIndex=\"+removeIndex)");
jw.beginIf(JavaUtil.genEquals(baseType, "value[newIndex]",
a.getReadMethod(true)+"(oldIndex)"));
jw.comment("Same, so just continue.");
jw.endElseBeginIf("addIndex != -1 || removeIndex != -1");
jw.comment("More than 1 difference detected.");
jw.writeEol("addIndex = removeIndex = -1");
jw.writeEol("checkAddOrRemoveOne = false");
jw.writeEol("break");
jw.endElseBeginIf("oldIndex + 1 < oldSize && ("+JavaUtil.genEquals(baseType, "value[newIndex]", a.getReadMethod(true)+"(oldIndex+1)")+")");
jw.writeEol("removeIndex = oldIndex");
jw.writeEol("++oldIndex");
jw.endElseBeginIf("newIndex + 1 < newSize && ("+JavaUtil.genEquals(baseType, "value[newIndex+1]", a.getReadMethod(true)+"(oldIndex)")+")");
jw.writeEol("addIndex = newIndex");
jw.writeEol("++newIndex");
jw.endElseBegin();
jw.comment("More than 1 difference.");
jw.writeEol("addIndex = removeIndex = -1");
jw.writeEol("checkAddOrRemoveOne = false");
jw.writeEol("break");
jw.end();
jw.end(); // for
// Only do this check if we fell off the end (oldIndex == newIndex)
jw.beginIf("checkAddOrRemoveOne && addIndex == -1 && removeIndex == -1");
jw.beginIf("oldSize + 1 == newSize");
jw.comment("Added last one");
jw.writeEol("addIndex = oldSize");
jw.endElseBeginIf("oldSize == newSize + 1");
jw.comment("Removed last one");
jw.writeEol("removeIndex = newSize");
jw.end();
jw.end();
jw.end();
//jw.writeEol("System.out.println(\"addIndex=\"+addIndex+\" removeIndex=\"+removeIndex)");
jw.beginIf("addIndex >= 0");
jw.write("event = ");
genNewEvent(a, "addIndex", "null", "value[addIndex]", baseType);
jw.eol();
if (!isArrayStyle && !config.isVetoable() && !config.isKeepElementPositions() && !config.isGenerateStoreEvents()) {
SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a.searchExtraData(SchemaRep.WhiteSpace.class);
if (ws != null)
genWhiteSpaceRestriction(ws, "value[addIndex]", baseType);
jw.writeEol(attr+".add(addIndex, value[addIndex])");
genMadeChange();
jw.writeEol("eventListeners.firePropertyChange(event)");
jw.writeEol("return");
}
jw.endElseBeginIf("removeIndex >= 0");
jw.write("event = ");
genNewEvent(a, "removeIndex", a.getReadMethod(true)+"(removeIndex)", "null", baseType);
jw.eol();
if (!isArrayStyle && !config.isVetoable() && !config.isKeepElementPositions() && !config.isGenerateStoreEvents()) {
jw.writeEol(attr+".remove(removeIndex)");
genMadeChange();
jw.writeEol("eventListeners.firePropertyChange(event)");
jw.writeEol("return");
}
jw.endElseBegin();
}
jw.write("event = ");
genNewEvent(a, indexed ? "-1" : "",
a.getReadMethod(false)+"()",
"value", type);
jw.eol();
end();
if (indexed) {
jw.end();
}
}
if (config.isVetoable()) {
gencr("if (vetos != null)");
tabIn();
geneol("vetos.fireVetoableChange(event)");
}
if (indexed) {
if (config.isKeepElementPositions()) {
jw.comment("Figure out where this type belongs.");
jw.writeEol("int elementCount = fetchChildCount()");
jw.writeEol("int destPos = 0");
jw.writeEol("int srcPos = 0");
// Remove all of the old entries
jw.beginFor("", "destPos < elementCount && srcPos < value.length",
"++destPos");
jw.beginIf("elementTypesByPosition[destPos] == "+i);
jw.comment("replace it");
jw.writeEol("elementsByPosition[destPos] = value[srcPos++]");
jw.end();
jw.end();
jw.comment("Handle when the replacement array is smaller.");
jw.beginWhile("destPos < elementCount");
jw.beginIf("elementTypesByPosition[destPos] == "+i);
jw.writeEol("deleteElement(destPos)");
jw.writeEol("--elementCount");
jw.endElseBegin();
jw.writeEol("++destPos");
jw.end();
jw.end();
jw.comment("Handle when replacement array is larger.");
jw.beginFor("", "srcPos < value.length", "++srcPos");
jw.writeEol("insertElementByPosition(destPos++, ",
JavaUtil.toObject("value[srcPos]", baseType,
config.isForME(), config.isJava5()),
", "+i+")");
jw.end();
}
if (isArrayStyle) {
jw.writeEol(attr, " = value");
} else {
jw.writeEol(attr, ".clear()");
if ("java.util.ArrayList".equals(config.getIndexedPropertyType())) {
jw.writeEol("((", config.getIndexedPropertyType(),
") "+attr, ").ensureCapacity(value.length)");
}
jw.beginFor("int i = 0", "i < value.length", "++i");
SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a.searchExtraData(SchemaRep.WhiteSpace.class);
if (ws != null)
genWhiteSpaceRestriction(ws, "value[i]", baseType);
jw.write(attr, ".add(");
String objectValue = JavaUtil.toObject("value[i]", baseType,
config.isForME(), config.isJava5());
jw.writeEol(objectValue, ")");
jw.end();
}
} else {
geneol(attr+" = value");
if (config.isOptionalScalars() && a.isScalar()) {
jw.writeEol(a.getScalarIsSet(), " = true");
}
if (config.isKeepElementPositions()) {
jw.comment("Figure out where this type belongs.");
jw.writeEol("int elementCount = fetchChildCount()");
jw.writeEol("int pos = findFirstOfElementType("+i+")");
if (!isScalar) {
jw.beginIf("value != null");
}
jw.beginIf("pos >= elementCount");
jw.comment("It's the last one to be added");
jw.writeEol("expandElementsByPosition(elementCount+1)");
jw.writeEol("elementTypesByPosition[pos] = "+i);
jw.end();
jw.writeEol("elementsByPosition[pos] = ",
JavaUtil.toObject("value", type,
config.isForME(), config.isJava5()));
if (!isScalar) {
jw.endElseBegin();
jw.beginIf("pos < elementCount");
jw.writeEol("deleteElement(pos)");
jw.end();
jw.end();
}
}
}
genMadeChange();
if (config.isGeneratePropertyEvents()) {
if (config.isGenerateStoreEvents()) {
gencr("if (storeEvents)");
tabIn();
geneol("storedEvents.add(event)");
gen("else ");
}
gencr("if (event != null)");
tabIn();
geneol("eventListeners.firePropertyChange(event)");
}
genResetMutuallyExclusive(a, true);
end();
cr();
if (indexed) {
gen(sigs.findSignature(SETTERINDEXED));
sp();
begin();
if (a.getPropertyInterface() != null) {
jw.writeEol(baseType+" value = ("+baseType+") valueInterface");
}
if (config.isGeneratePropertyEvents()) {
jw.beginIf(JavaUtil.genEquals(type, "value",
a.getReadMethod(true)+"(index)",
true));
jw.comment("No change.");
jw.writeEol("return");
jw.end();
}
if (a.isBean && config.isGenerateParentRefs()) {
jw.beginIf("value != null");
jw.writeEol("value._setParent(this)");
jw.end();
}
if (config.isGeneratePropertyEvents()) {
if (a.isBean) {
jw.beginIf("value != null");
comment("Make the foreign beans take on our property change event listeners.");
geneol("value._setPropertyChangeSupport(eventListeners)");
if (config.isVetoable())
geneol("value._setVetoableChangeSupport(vetos)");
jw.end();
}
gen("if (");
if (config.isGenerateStoreEvents()) {
gen("storeEvents || ");
}
gen("eventListeners != null) ");
begin();
jw.write("java.beans.PropertyChangeEvent event = ");
genNewEvent(a, "index", a.getReadMethod(true)+"(index)",
"value", baseType);
jw.eol();
if (config.isVetoable()) {
gencr("if (vetos != null)");
tabIn();
geneol("vetos.fireVetoableChange(event)");
}
if (config.isGenerateStoreEvents()) {
gencr("if (storeEvents)");
tabIn();
geneol("storedEvents.add(event)");
gencr("else");
tabIn();
}
geneol("eventListeners.firePropertyChange(event)");
end();
}
if (isArrayStyle) {
jw.writeEol(attr, "[index] = value");
} else {
if (!a.isDirectChild()) {
// Check to see if we need to grow before we set.
jw.beginFor("int size = "+attr+".size()",
"index >= size", "++size");
jw.writeEol(attr, ".add(null)");
jw.end();
}
jw.write(attr, ".set(index, ");
jw.write(JavaUtil.toObject("value", baseType, config.isForME(), config.isJava5()));
jw.writeEol(")");
}
if (config.isKeepElementPositions()) {
jw.writeEol("int pos = findElementType("+i+", index)");
jw.writeEol("elementsByPosition[pos] = ",
JavaUtil.toObject("value", baseType,
config.isForME(), config.isJava5()));
}
genMadeChange();
end();
cr();
}
// Generate getter
gen(sigs.findSignature(GETTER));
sp();
begin();
if (indexed && !isArrayStyle) {
if (baseType.equals("byte[]"))
jw.writeEol(type, " arr = new byte[", attr, ".size()][]");
else
jw.writeEol(type, " arr = new ", baseType+"["+attr, ".size()]");
if (isScalar) {
jw.beginFor("int i = 0", "i < arr.length", "++i");
jw.write("arr[i] = ");
jw.write(JavaUtil.fromObject(baseType, attr+".get(i)"));
jw.eol();
jw.end();
jw.write("return arr");
} else {
gen("return ("+type+") "+attr+".toArray(arr)");
}
} else
gen("return "+attr);
eol();
end();
cr();
if (indexed) {
if (!config.isMinFeatures() && !isArrayStyle) {
gen(sigs.findSignature(GETTERLIST));
sp();
begin();
gen("return "+attr);
eol();
end();
cr();
}
gen(sigs.findSignature(GETTERINDEXED));
sp();
begin();
jw.write("return ");
if (isArrayStyle) {
jw.writeEol(attr, "[index]");
} else {
jw.writeEol(JavaUtil.fromObject(baseType, attr+".get(index)"));
}
end();
cr();
comment("Return the number of "+propertyName);
gen(sigs.findSignature(SIZE));
sp();
begin();
if (isArrayStyle) {
jw.writeEol("return ", attr, ".length");
} else {
jw.writeEol("return ", attr, ".size()");
}
end();
cr();
if (!isArrayStyle) {
gen(sigs.findSignature(ADD));
sp();
begin();
if (a.getPropertyInterface() != null) {
jw.writeEol(baseType+" value = ("+baseType+") valueInterface");
}
if (a.isBean && config.isGenerateParentRefs()) {
jw.beginIf("value != null");
jw.writeEol("value._setParent(this)");
jw.end();
}
if (a.isBean && config.isGeneratePropertyEvents()) {
jw.beginIf("value != null");
comment("Make the foreign beans take on our property change event listeners.");
geneol("value._setPropertyChangeSupport(eventListeners)");
if (config.isVetoable())
geneol("value._setVetoableChangeSupport(vetos)");
jw.end();
}
jw.writeEol(attr, ".add(",
JavaUtil.toObject("value", baseType,
config.isForME(), config.isJava5()),
")");
if (config.isKeepElementPositions()) {
GraphLink gl = a.getGraphLink();
//jw.comment("gl="+gl);
//jw.comment("gl.lastInGroup="+gl.getLastInGroup());
//jw.comment("gl.lastInGroup.object="+gl.getLastInGroup().getObject());
int lastPropNum;
if (gl == null) {
lastPropNum = i;
} else {
Property lastProp = (Property) gl.getLastSibling().getObject();
if (lastProp == a) {
lastPropNum = i;
} else {
for (lastPropNum = 0; lastPropNum < size; ++lastPropNum)
if (attrList.get(lastPropNum) == lastProp)
break;
if (lastPropNum == size) {
jw.comment("Did not find lastPropNum");
lastPropNum = i;
}
}
}
jw.writeEol("int pos = findLastOfElementType("+lastPropNum+")+1");
jw.writeEol("insertElementByPosition(pos, ",
JavaUtil.toObject("value", baseType,
config.isForME(), config.isJava5()),
", "+i+")");
}
if (config.isGeneratePropertyEvents()) {
gen("if (");
if (config.isGenerateStoreEvents()) {
gen("storeEvents || ");
}
gen("eventListeners != null) ");
begin();
jw.write("java.beans.PropertyChangeEvent event = ");
genNewEvent(a, attr+".size()-1", "null",
"value", baseType);
jw.eol();
if (config.isVetoable()) {
gencr("if (vetos != null)");
tabIn();
geneol("vetos.fireVetoableChange(event)");
}
if (config.isGenerateStoreEvents()) {
gencr("if (storeEvents)");
tabIn();
geneol("storedEvents.add(event)");
gencr("else");
tabIn();
}
geneol("eventListeners.firePropertyChange(event)");
end();
}
jw.writeEol("int positionOfNewItem = "+attr+".size()-1");
if (isMutuallyExclusive(a)) {
jw.beginIf("positionOfNewItem == 0");
genResetMutuallyExclusive(a, false);
jw.end();
}
genMadeChange();
geneol("return positionOfNewItem");
end();
cr();
jw.bigComment("Search from the end looking for @param value, and then remove it.");
gen(sigs.findSignature(REMOVE));
sp();
begin();
if (a.getPropertyInterface() != null) {
jw.writeEol(baseType+" value = ("+baseType+") valueInterface");
}
jw.writeEol("int pos = ", attr, ".indexOf(",
JavaUtil.toObject("value", baseType,
config.isForME(), config.isJava5())+")");
gen("if (pos >= 0) ");
begin();
geneol(attr+".remove(pos)");
if (config.isKeepElementPositions()) {
jw.writeEol("int elementPos = findElementType("+i+", pos)");
jw.writeEol("deleteElement(elementPos)");
}
if (config.isGeneratePropertyEvents()) {
gen("if (");
if (config.isGenerateStoreEvents()) {
gen("storeEvents || ");
}
gen("eventListeners != null) ");
begin();
gen("java.beans.PropertyChangeEvent event = ");
genNewEvent(a, "pos", "value", "null", baseType);
jw.eol();
if (config.isVetoable()) {
gencr("if (vetos != null)");
tabIn();
geneol("vetos.fireVetoableChange(event)");
}
if (config.isGenerateStoreEvents()) {
gencr("if (storeEvents)");
tabIn();
geneol("storedEvents.add(event)");
gencr("else");
tabIn();
}
geneol("eventListeners.firePropertyChange(event)");
end();
}
end();
genMadeChange();
geneol("return pos");
end();
cr();
}
MetaProperty mp = getMetaProperty(a.name);
GraphNode graphNode = a.getGraphNode();
SchemaRep.Key key = null;
if (graphNode != null)
key = (SchemaRep.Key)
graphNode.searchExtraData(SchemaRep.Key.class);
if (a.isBean &&
((key != null) || (mp != null && mp.isKey()))) {
//System.out.println("mp for "+a.name);
//System.out.println(mp.dumpBeanNode());
config.messageOut.println("Found key: "+key);
SchemaRep.Selector selector = (SchemaRep.Selector)
key.findSubElement(SchemaRep.Selector.class);
SchemaRep.Field field = (SchemaRep.Field)
key.findSubElement(SchemaRep.Field.class);
genFinder(a, graphNode, attr, key.getElementName(),
baseType, a.name,
selector.getXPath(), field.getXPath(), false);
}
} // end if (indexed)
genDefaultsAccessable(a);
if (a.isBean && !a.getBeanElement().isAbstract) {
genNewMethod(a.getPropertyInterface(), baseType,
getTypeFullClassName(a));
}
}
if (config.isKeepElementPositions()) {
select(DECL_SECTION);
jw.writeEol("private java.lang.Object[] elementsByPosition = new java.lang.Object[0]"); // we might be storing String, int's, and beans in there.
jw.writeEol("private int[] elementTypesByPosition = new int[0]");
}
if (beanElement.isRoot && !config.isMinFeatures()) {
select(DECL_SECTION);
jw.write("private java.lang.String schemaLocation");
if (mdd.getSchemaLocation() != null) {
jw.write(" = ");
jw.write(JavaUtil.instanceFrom("java.lang.String",
mdd.getSchemaLocation()));
}
jw.eol();
select(ACCESS_SECTION);
jw.beginMethod("_setSchemaLocation", "String location", null, "void", jw.PUBLIC);
jw.writeEol("schemaLocation = location");
genMadeChange();
jw.end();
jw.cr();
jw.beginMethod("_getSchemaLocation", "", null, "String", jw.PUBLIC);
jw.writeEol("return schemaLocation");
jw.endMethod();
}
if (config.isGenerateParentRefs()) {
select(DECL_SECTION);
String parentType = parentBeanType();
jw.writeEol("private ", parentType, " parent");
select(BODY_SECTION);
jw.beginMethod("_setParent", parentType+" parent");
jw.writeEol("this.parent = parent");
jw.endMethod();
jw.beginMethod("_getXPathExpr", "", null, "String", jw.PUBLIC);
jw.beginIf("parent == null");
jw.writeEol("return \"/", beanElement.node.getName(), "\"");
jw.endElseBegin();
jw.writeEol("String parentXPathExpr = parent._getXPathExpr()");
jw.writeEol("String myExpr = parent.nameChild(this, false, false, true)");
jw.writeEol("return parentXPathExpr + \"/\" + myExpr");
jw.end();
jw.endMethod();
jw.beginMethod("_getXPathExpr", "Object childObj", null, "String", jw.PUBLIC);
jw.writeEol("String childName = nameChild(childObj, false, false, true)");
jw.beginIf("childName == null");
jw.writeEol("throw new IllegalArgumentException(\"childObj (\"+childObj.toString()+\") is not a child of this bean ("+className+").\")");
jw.end();
jw.writeEol("return _getXPathExpr() + \"/\" + childName");
jw.endMethod();
}
// Look for finders
for (Iterator it = beanElement.getGraphNode().extraDataIterator(); it.hasNext(); ) {
Object data = it.next();
if (data instanceof BeanBuilder.Finder) {
BeanBuilder.Finder finder = (BeanBuilder.Finder) data;
//System.out.println("Found finder: "+finder);
genFinder(beanElement.getGraphNode(), finder);
}
}
}