001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.model.impl;
038:
039: import java.lang.annotation.Annotation;
040: import java.beans.Introspector;
041:
042: import com.sun.xml.bind.v2.model.annotation.Locatable;
043: import com.sun.xml.bind.v2.model.core.PropertyInfo;
044: import com.sun.xml.bind.v2.runtime.Location;
045:
046: /**
047: * {@link PropertyInfo} implementation backed by a getter and a setter.
048: *
049: * We allow the getter or setter to be null, in which case the bean
050: * can only participate in unmarshalling (or marshalling)
051: */
052: class GetterSetterPropertySeed<TypeT, ClassDeclT, FieldT, MethodT>
053: implements PropertySeed<TypeT, ClassDeclT, FieldT, MethodT> {
054:
055: protected final MethodT getter;
056: protected final MethodT setter;
057: private ClassInfoImpl<TypeT, ClassDeclT, FieldT, MethodT> parent;
058:
059: GetterSetterPropertySeed(
060: ClassInfoImpl<TypeT, ClassDeclT, FieldT, MethodT> parent,
061: MethodT getter, MethodT setter) {
062: this .parent = parent;
063: this .getter = getter;
064: this .setter = setter;
065:
066: if (getter == null && setter == null)
067: throw new IllegalArgumentException();
068: }
069:
070: public TypeT getRawType() {
071: if (getter != null)
072: return parent.nav().getReturnType(getter);
073: else
074: return parent.nav().getMethodParameters(setter)[0];
075: }
076:
077: public <A extends Annotation> A readAnnotation(Class<A> annotation) {
078: return parent.reader().getMethodAnnotation(annotation, getter,
079: setter, this );
080: }
081:
082: public boolean hasAnnotation(
083: Class<? extends Annotation> annotationType) {
084: return parent.reader().hasMethodAnnotation(annotationType,
085: getName(), getter, setter, this );
086: }
087:
088: public String getName() {
089: if (getter != null)
090: return getName(getter);
091: else
092: return getName(setter);
093: }
094:
095: private String getName(MethodT m) {
096: String seed = parent.nav().getMethodName(m);
097: String lseed = seed.toLowerCase();
098: if (lseed.startsWith("get") || lseed.startsWith("set"))
099: return camelize(seed.substring(3));
100: if (lseed.startsWith("is"))
101: return camelize(seed.substring(2));
102: return seed;
103: }
104:
105: private static String camelize(String s) {
106: return Introspector.decapitalize(s);
107: }
108:
109: /**
110: * Use the enclosing class as the upsream {@link Location}.
111: */
112: public Locatable getUpstream() {
113: return parent;
114: }
115:
116: public Location getLocation() {
117: if (getter != null)
118: return parent.nav().getMethodLocation(getter);
119: else
120: return parent.nav().getMethodLocation(setter);
121: }
122: }
|