001: /*
002: Copyright (c) 2006, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.typeinfo;
030:
031: /**
032: * Base class for representing type descriptions. This provides the default
033: * implementations for methods overridden by specific subclasses.
034: */
035: public abstract class TypeDescription {
036: /** Empty field description array. */
037: public static final FieldDescription[] EMPTY_FIELD_ARRAY = {};
038:
039: /** Empty method description array. */
040: public static final MethodDescription[] EMPTY_METHOD_ARRAY = {};
041:
042: /** Type descriptor. */
043: private final String m_descriptor;
044:
045: /**
046: * Constructor.
047: *
048: * @param dtor type descriptor
049: */
050: protected TypeDescription(String dtor) {
051: m_descriptor = dtor;
052: }
053:
054: /**
055: * Check if type is an array.
056: *
057: * @return <code>true</code> if array, <code>false</code> if not
058: */
059: public boolean isArray() {
060: return false;
061: }
062:
063: /**
064: * Get array item type.
065: *
066: * @return item type
067: * @throws IllegalStateException if not an array type
068: */
069: public TypeDescription getArrayItemType() {
070: throw new IllegalStateException("Not an array");
071: }
072:
073: /**
074: * Check if type is a primitive.
075: *
076: * @return <code>true</code> if primitive, <code>false</code> if not
077: */
078: public boolean isPrimitive() {
079: return false;
080: }
081:
082: /**
083: * Get fields defined by type.
084: *
085: * @return array of fields
086: */
087: public FieldDescription[] getFields() {
088: return EMPTY_FIELD_ARRAY;
089: }
090:
091: /**
092: * Get methods defined by type.
093: *
094: * @return array of methods
095: */
096: public MethodDescription[] getMethods() {
097: return EMPTY_METHOD_ARRAY;
098: }
099:
100: /**
101: * Get descriptor for type.
102: *
103: * @return descriptor
104: */
105: public String getDescriptor() {
106: return m_descriptor;
107: }
108:
109: /**
110: * Check for equality.
111: *
112: * @param obj comparison object
113: * @return <code>true</code> if equal, <code>false</code> if not
114: */
115: public boolean equals(Object obj) {
116: if (obj == this ) {
117: return true;
118: } else if (obj instanceof TypeDescription) {
119: return m_descriptor
120: .equals(((TypeDescription) obj).m_descriptor);
121: } else {
122: return false;
123: }
124: }
125:
126: /**
127: * Get hash code. This just returns the hash code of the descriptor.
128: *
129: * @return hash code
130: */
131: public int hashCode() {
132: return m_descriptor.hashCode();
133: }
134:
135: /**
136: * Get type description in external form. Subclasses must implement this
137: * method.
138: *
139: * @return external description
140: */
141: public abstract String toString();
142: }
|