📄 javabeanwriter.java
字号:
// Define getters and setters for the bean elements for (int i = 0; i < names.size(); i += 2, j++) { String typeName = (String) names.get(i); String name = (String) names.get(i + 1); String capName = Utils.capitalizeFirstChar(name); String documentation = ""; if (elements != null) { if (elements != null && i < (elements.size()*2)) { ElementDecl elem = (ElementDecl)elements.get(i/2); documentation = elem.getDocumentation(); } } String get = "get"; if (typeName.equals("boolean")) { get = "is"; } String comment = getJavadocDescriptionPart(documentation, true); if (comment.length() > 3) { // remove the " *" at the front of the first line comment = comment.substring(2); } if (enableGetters) { try { pw.println(); pw.println(" /**"); pw.println(" * Gets the " + name + " value for this " + getClassName() + "."); pw.println(" * "); pw.println(" * @return " + name + comment); pw.println(" */"); } catch (DOMException e) { // no comment } pw.println(" public " + typeName + " " + get + capName + "() {"); if (isUnion()) { writeSimpleTypeGetter(typeName, name, "return"); } else { pw.println(" return " + name + ";"); } pw.println(" }"); pw.println(); } if (enableSetters) { try { String nm = (isUnion()) ? "_value" : name; pw.println(); pw.println(" /**"); pw.println(" * Sets the " + nm + " value for this " + getClassName() + "."); pw.println(" * "); pw.println(" * @param " + nm + comment); pw.println(" */"); } catch (DOMException e) { // no comment } if (isUnion()) { pw.println(" public void set" + capName + "(" + typeName + " _value) {"); writeSimpleTypeSetter(typeName); } else { pw.println(" public void set" + capName + "(" + typeName + " " + name + ") {"); pw.println(" this." + name + " = " + name + ";"); } pw.println(" }"); pw.println(); } // If this is a special collection type, insert extra // java code so that the serializer/deserializer can recognize // the class. This is not JAX-RPC, and will be replaced with // compliant code when JAX-RPC determines how to deal with this case. // These signatures comply with Bean Indexed Properties which seems // like the reasonable approach to take for collection types. // (It may be more efficient to handle this with an ArrayList...but // for the initial support it was easier to use an actual array.) if ((elements != null) && (j < elements.size())) { ElementDecl elem = (ElementDecl) elements.get(j); if (elem.getType().getQName().getLocalPart().indexOf("[") > 0) { String compName = typeName.substring(0, typeName.lastIndexOf("[")); if (enableGetters) { pw.println(" public " + compName + " " + get + capName + "(int i) {"); pw.println(" return this." + name + "[i];"); pw.println(" }"); pw.println(); } if (enableSetters) { pw.println(" public void set" + capName + "(int i, " + compName + " _value) {"); // According to the section 7.2 of the JavaBeans // specification, the indexed setter should not // establish or grow the array. Thus the following // code is not generated for compliance purposes. /* * int bracketIndex = typeName.indexOf("["); * String newingName = typeName.substring(0, bracketIndex + 1); * String newingSuffix = typeName.substring(bracketIndex + 1); * * pw.println(" if (this." + name + " == null ||"); * pw.println(" this." + name + ".length <= i) {"); * pw.println(" " + typeName + " a = new " + * newingName + "i + 1" + newingSuffix + ";"); * pw.println(" if (this." + name + " != null) {"); * pw.println(" for(int j = 0; j < this." + name + * ".length; j++)"); * pw.println(" a[j] = this." + name + "[j];"); * pw.println(" }"); * pw.println(" this." + name + " = a;"); * pw.println(" }"); */ pw.println(" this." + name + "[i] = _value;"); pw.println(" }"); pw.println(); } } } } } /** * Writes a general purpose equals method */ protected void writeEqualsMethod() { // The __equalsCalc field and synchronized method are necessary // in case the object has direct or indirect references to itself. pw.println(" private java.lang.Object __equalsCalc = null;"); pw.println( " public synchronized boolean equals(java.lang.Object obj) {"); // First do the general comparison checks pw.println(" if (!(obj instanceof " + className + ")) return false;"); pw.println(" " + className + " other = (" + className + ") obj;"); pw.println(" if (obj == null) return false;"); pw.println(" if (this == obj) return true;"); // Have we been here before ? return true if yes otherwise false pw.println(" if (__equalsCalc != null) {"); pw.println(" return (__equalsCalc == obj);"); pw.println(" }"); pw.println(" __equalsCalc = obj;"); // Before checking the elements, check equality of the super class String truth = "true"; if ((extendType != null) && (!type.isSimpleType() || simpleValueTypes.size() == 0)) { truth = "super.equals(obj)"; } pw.println(" boolean _equals;"); if (names.size() == 0) { pw.println(" _equals = " + truth + ";"); } else if (isUnion()) { pw.println(" _equals = " + truth + " && " + " this.toString().equals(obj.toString());"); } else { pw.println(" _equals = " + truth + " && "); for (int i = 0; i < names.size(); i += 2) { String variableType = (String) names.get(i); String variable = (String) names.get(i + 1); String get = "get"; if (variableType.equals("boolean")) { get = "is"; } if (variableType.equals("int") || variableType.equals("long") || variableType.equals("short") || variableType.equals("float") || variableType.equals("double") || variableType.equals("boolean") || variableType.equals("byte")) { pw.print(" this." + variable + " == other." + get + Utils.capitalizeFirstChar(variable) + "()"); } else if (variableType.indexOf("[") >= 0) { // Use java.util.Arrays.equals to compare arrays. pw.println(" ((this." + variable + "==null && other." + get + Utils.capitalizeFirstChar(variable) + "()==null) || "); pw.println(" (this." + variable + "!=null &&"); pw.print(" java.util.Arrays.equals(this." + variable + ", other." + get + Utils.capitalizeFirstChar(variable) + "())))"); } else { pw.println(" ((this." + variable + "==null && other." + get + Utils.capitalizeFirstChar(variable) + "()==null) || "); pw.println(" (this." + variable + "!=null &&"); pw.print(" this." + variable + ".equals(other." + get + Utils.capitalizeFirstChar(variable) + "())))"); } if (i == (names.size() - 2)) { pw.println(";"); } else { pw.println(" &&"); } } } pw.println(" __equalsCalc = null;"); pw.println(" return _equals;"); pw.println(" }"); pw.println(""); } /** * Writes a general purpose hashCode method. */ protected void writeHashCodeMethod() { // The __hashCodeCalc field and synchronized method are necessary // in case the object has direct or indirect references to itself. pw.println(" private boolean __hashCodeCalc = false;"); pw.println(" public synchronized int hashCode() {"); pw.println(" if (__hashCodeCalc) {"); pw.println(" return 0;"); pw.println(" }"); pw.println(" __hashCodeCalc = true;"); // Get the hashCode of the super class String start = "1"; if ((extendType != null) && !type.isSimpleType()) { start = "super.hashCode()"; } pw.println(" int _hashCode = " + start + ";"); if (isUnion()) { pw.println(" if (this._value != null) {"); pw.println(" _hashCode += this._value.hashCode();"); pw.println(" }"); } for (int i = 0; !isUnion() && (i < names.size()); i += 2) { String variableType = (String) names.get(i); String variable = (String) names.get(i + 1); String get = "get"; if (variableType.equals("boolean")) { get = "is"; } if (variableType.equals("int") || variableType.equals("short") || variableType.equals("byte")) { pw.println(" _hashCode += " + get + Utils.capitalizeFirstChar(variable) + "();"); } else if (variableType.equals("boolean")) { pw.println(" _hashCode += (" + get + Utils.capitalizeFirstChar(variable) + "() ? Boolean.TRUE : Boolean.FALSE).hashCode();"); } else if (variableType.equals("long")) { pw.println(" _hashCode += new Long(" + get + Utils.capitalizeFirstChar(variable) + "()).hashCode();"); } else if (variableType.equals("float")) { pw.println(" _hashCode += new Float(" + get + Utils.capitalizeFirstChar(variable) + "()).hashCode();"); } else if (variableType.equals("double")) { pw.println(" _hashCode += new Double(" + get + Utils.capitalizeFirstChar(variable) + "()).hashCode();"); } else if (variableType.indexOf("[") >= 0) { // The hashCode calculation for arrays is complicated. // Wish there was a hashCode method in java.utils.Arrays ! // Get the hashCode for each element of the array which is not an array. pw.println(" if (" + get + Utils.capitalizeFirstChar(variable) + "() != null) {"); pw.println(" for (int i=0;"); pw.println( " i<java.lang.reflect.Array.getLength(" + get + Utils.capitalizeFirstChar(variable) + "());"); pw.println(" i++) {"); pw.println( " java.lang.Object obj = java.lang.reflect.Array.get(" + get + Utils.capitalizeFirstChar(variable) + "(), i);"); pw.println(" if (obj != null &&"); pw.println(" !obj.getClass().isArray()) {"); pw.println(" _hashCode += obj.hashCode();"); pw.println(" }"); pw.println(" }"); pw.println(" }"); } else { pw.println(" if (" + get + Utils.capitalizeFirstChar(variable) + "() != null) {"); pw.println(" _hashCode += " + get + Utils.capitalizeFirstChar(variable) + "().hashCode();"); pw.println(" }"); } } // Reset the __hashCodeCalc variable and return pw.println(" __hashCodeCalc = false;"); pw.println(" return _hashCode;"); pw.println(" }"); pw.println(""); } /** Generate a java source file and/or helper source file. * If the emitter works in deploy mode and the class already exists, only the helper is generated. * Otherwise, the java bean and helper source are generated. */ public void generate() throws IOException { String fqcn = getPackage() + "." + getClassName(); if (emitter.isDeploy() && emitter.doesExist(fqcn)) { if (emitter.isHelperWanted()) { helper.generate(); } } else { super.generate(); } }} // class JavaBeanWriter
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -