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.tools.internal.xjc.generator.bean.field; 27 28 import com.sun.codemodel.internal.JBlock; 29 import com.sun.codemodel.internal.JExpr; 30 import com.sun.codemodel.internal.JExpression; 31 import com.sun.codemodel.internal.JMethod; 32 import com.sun.codemodel.internal.JPrimitiveType; 33 import com.sun.codemodel.internal.JType; 34 import com.sun.codemodel.internal.JVar; 35 import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl; 36 import com.sun.tools.internal.xjc.generator.bean.MethodWriter; 37 import com.sun.tools.internal.xjc.model.CPropertyInfo; 38 import com.sun.tools.internal.xjc.outline.Aspect; 39 import com.sun.tools.internal.xjc.outline.FieldAccessor; 40 import com.sun.xml.internal.bind.api.impl.NameConverter; 41 42 /** 43 * A required primitive property. 44 * 45 * @author 46 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 47 */ 48 public class UnboxedField extends AbstractFieldWithVar { 49 50 /** 51 * The primitive version of {@link #implType} and {@link #exposedType}. 52 */ 53 private final JPrimitiveType ptype; 54 55 56 protected UnboxedField( ClassOutlineImpl outline, CPropertyInfo prop ) { 57 super(outline,prop); 58 // primitive types don't have this distintion 59 assert implType==exposedType; 60 61 ptype = (JPrimitiveType) implType; 62 assert ptype!=null; 63 64 createField(); 65 66 // apparently a required attribute can be still defaulted. 67 // so this assertion is incorrect. 68 // assert prop.defaultValue==null; 69 70 MethodWriter writer = outline.createMethodWriter(); 71 NameConverter nc = outline.parent().getModel().getNameConverter(); 72 73 JBlock body; 74 75 // [RESULT] 76 // Type getXXX() { 77 // return value; 78 // } 79 JMethod $get = writer.declareMethod( ptype, getGetterMethod() ); 80 String javadoc = prop.javadoc; 81 if(javadoc.length()==0) 82 javadoc = Messages.DEFAULT_GETTER_JAVADOC.format(nc.toVariableName(prop.getName(true))); 83 writer.javadoc().append(javadoc); 84 85 $get.body()._return(ref()); 86 87 88 // [RESULT] 89 // void setXXX( Type value ) { 90 // this.value = value; 91 // } 92 JMethod $set = writer.declareMethod( codeModel.VOID, "set"+prop.getName(true) ); 93 JVar $value = writer.addParameter( ptype, "value" ); 94 body = $set.body(); 95 body.assign(JExpr._this().ref(ref()),$value); 96 // setter always get the default javadoc. See issue #381 97 writer.javadoc().append(Messages.DEFAULT_SETTER_JAVADOC.format(nc.toVariableName(prop.getName(true)))); 98 99 } 100 101 protected JType getType(Aspect aspect) { 102 return super.getType(aspect).boxify().getPrimitiveType(); 103 } 104 105 protected JType getFieldType() { 106 return ptype; 107 } 108 109 public FieldAccessor create(JExpression targetObject) { 110 return new Accessor(targetObject) { 111 112 public void unsetValues( JBlock body ) { 113 // you can't unset a value 114 } 115 116 public JExpression hasSetValue() { 117 return JExpr.TRUE; 118 } 119 }; 120 } 121 }