1 /*
   2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.bind.v2.model.impl;
  27 
  28 import java.lang.annotation.Annotation;
  29 import java.beans.Introspector;
  30 
  31 import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
  32 import com.sun.xml.internal.bind.v2.model.core.PropertyInfo;
  33 import com.sun.xml.internal.bind.v2.runtime.Location;
  34 
  35 /**
  36  * {@link PropertyInfo} implementation backed by a getter and a setter.
  37  *
  38  * We allow the getter or setter to be null, in which case the bean
  39  * can only participate in unmarshalling (or marshalling)
  40  */
  41 class GetterSetterPropertySeed<TypeT,ClassDeclT,FieldT,MethodT> implements
  42         PropertySeed<TypeT,ClassDeclT,FieldT,MethodT> {
  43 
  44     protected final MethodT getter;
  45     protected final MethodT setter;
  46     private ClassInfoImpl<TypeT,ClassDeclT,FieldT,MethodT> parent;
  47 
  48     GetterSetterPropertySeed(ClassInfoImpl<TypeT,ClassDeclT,FieldT,MethodT> parent, MethodT getter, MethodT setter) {
  49         this.parent = parent;
  50         this.getter = getter;
  51         this.setter = setter;
  52 
  53         if(getter==null && setter==null)
  54             throw new IllegalArgumentException();
  55     }
  56 
  57     public TypeT getRawType() {
  58         if(getter!=null)
  59             return parent.nav().getReturnType(getter);
  60         else
  61             return parent.nav().getMethodParameters(setter)[0];
  62     }
  63 
  64     public <A extends Annotation> A readAnnotation(Class<A> annotation) {
  65         return parent.reader().getMethodAnnotation(annotation, getter,setter,this);
  66     }
  67 
  68     public boolean hasAnnotation(Class<? extends Annotation> annotationType) {
  69         return parent.reader().hasMethodAnnotation(annotationType,getName(),getter,setter,this);
  70     }
  71 
  72     public String getName() {
  73         if(getter!=null)
  74             return getName(getter);
  75         else
  76             return getName(setter);
  77     }
  78 
  79     private String getName(MethodT m) {
  80         String seed = parent.nav().getMethodName(m);
  81         String lseed = seed.toLowerCase();
  82         if(lseed.startsWith("get") || lseed.startsWith("set"))
  83             return camelize(seed.substring(3));
  84         if(lseed.startsWith("is"))
  85             return camelize(seed.substring(2));
  86         return seed;
  87     }
  88 
  89 
  90     private static String camelize(String s) {
  91         return Introspector.decapitalize(s);
  92     }
  93 
  94     /**
  95      * Use the enclosing class as the upsream {@link Location}.
  96      */
  97     public Locatable getUpstream() {
  98         return parent;
  99     }
 100 
 101     public Location getLocation() {
 102         if(getter!=null)
 103             return parent.nav().getMethodLocation(getter);
 104         else
 105             return parent.nav().getMethodLocation(setter);
 106     }
 107 }