1 /* 2 * Copyright (c) 1997, 2011, 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.tools.internal.xjc.generator.bean.field; 27 28 import java.util.List; 29 30 import com.sun.codemodel.internal.JBlock; 31 import com.sun.codemodel.internal.JConditional; 32 import com.sun.codemodel.internal.JExpr; 33 import com.sun.codemodel.internal.JExpression; 34 import com.sun.codemodel.internal.JMethod; 35 import com.sun.codemodel.internal.JType; 36 import com.sun.codemodel.internal.JVar; 37 import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl; 38 import com.sun.tools.internal.xjc.generator.bean.MethodWriter; 39 import com.sun.tools.internal.xjc.model.CPropertyInfo; 40 import com.sun.tools.internal.xjc.outline.FieldAccessor; 41 import com.sun.xml.internal.bind.api.impl.NameConverter; 42 43 /** 44 * Realizes a property through one getter and one setter. 45 * This renders: 46 * 47 * <pre> 48 * T' field; 49 * T getXXX() { ... } 50 * void setXXX(T value) { ... } 51 * </pre> 52 * 53 * <p> 54 * Normally T'=T, but under some tricky circumstances they could be different 55 * (like T'=Integer, T=int.) 56 * 57 * This realization is only applicable to fields with (1,1) 58 * or (0,1) multiplicity. 59 * 60 * @author 61 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 62 */ 63 public class SingleField extends AbstractFieldWithVar { 64 65 protected SingleField(ClassOutlineImpl context, CPropertyInfo prop) { 66 this(context,prop,false); 67 } 68 69 /** 70 * 71 * @param forcePrimitiveAccess 72 * forces the setter/getter to expose the primitive type. 73 * it's a pointless customization, but it's nevertheless in the spec. 74 */ 75 protected SingleField(ClassOutlineImpl context, CPropertyInfo prop, boolean forcePrimitiveAccess ) { 76 super(context, prop); 77 assert !exposedType.isPrimitive() && !implType.isPrimitive(); 78 79 createField(); 80 81 MethodWriter writer = context.createMethodWriter(); 82 NameConverter nc = context.parent().getModel().getNameConverter(); 83 84 // [RESULT] 85 // Type getXXX() { 86 // #ifdef default value 87 // if(value==null) 88 // return defaultValue; 89 // #endif 90 // return value; 91 // } 92 JExpression defaultValue = null; 93 if(prop.defaultValue!=null) 94 defaultValue = prop.defaultValue.compute(outline.parent()); 95 96 // if Type is a wrapper and we have a default value, 97 // we can use the primitive type. 98 JType getterType; 99 if (getOptions().enableIntrospection) { 100 if (forcePrimitiveAccess) 101 getterType = exposedType.unboxify(); 102 else 103 getterType = exposedType; 104 } else { 105 if (defaultValue != null || forcePrimitiveAccess) 106 getterType = exposedType.unboxify(); 107 else 108 getterType = exposedType; 109 } 110 111 JMethod $get = writer.declareMethod( getterType,getGetterMethod() ); 112 String javadoc = prop.javadoc; 113 if(javadoc.length()==0) 114 javadoc = Messages.DEFAULT_GETTER_JAVADOC.format(nc.toVariableName(prop.getName(true))); 115 writer.javadoc().append(javadoc); 116 117 118 if(defaultValue==null) { 119 $get.body()._return(ref()); 120 } else { 121 JConditional cond = $get.body()._if(ref().eq(JExpr._null())); 122 cond._then()._return(defaultValue); 123 cond._else()._return(ref()); 124 } 125 126 List<Object> possibleTypes = listPossibleTypes(prop); 127 writer.javadoc().addReturn() 128 .append("possible object is\n") 129 .append(possibleTypes); 130 131 // [RESULT] 132 // void setXXX(Type newVal) { 133 // this.value = newVal; 134 // } 135 JMethod $set = writer.declareMethod( codeModel.VOID, "set"+prop.getName(true) ); 136 JType setterType = exposedType; 137 if(forcePrimitiveAccess) setterType = setterType.unboxify(); 138 JVar $value = writer.addParameter( setterType, "value" ); 139 JBlock body = $set.body(); 140 if ($value.type().equals(implType)) { 141 body.assign(JExpr._this().ref(ref()), $value); 142 } else { 143 body.assign(JExpr._this().ref(ref()), castToImplType($value)); 144 } 145 146 // setter always get the default javadoc. See issue #381 147 writer.javadoc().append(Messages.DEFAULT_SETTER_JAVADOC.format(nc.toVariableName(prop.getName(true)))); 148 writer.javadoc().addParam($value) 149 .append("allowed object is\n") 150 .append(possibleTypes); 151 } 152 153 public final JType getFieldType() { 154 return implType; 155 } 156 157 public FieldAccessor create(JExpression targetObject) { 158 return new Accessor(targetObject); 159 } 160 161 protected class Accessor extends AbstractFieldWithVar.Accessor { 162 protected Accessor(JExpression $target) { 163 super($target); 164 } 165 166 public void unsetValues( JBlock body ) { 167 body.assign( $ref, JExpr._null() ); 168 } 169 public JExpression hasSetValue() { 170 return $ref.ne( JExpr._null() ); 171 } 172 } 173 }