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 com.sun.codemodel.internal.JBlock; 29 import com.sun.codemodel.internal.JCodeModel; 30 import com.sun.codemodel.internal.JExpr; 31 import com.sun.codemodel.internal.JExpression; 32 import com.sun.codemodel.internal.JType; 33 import com.sun.codemodel.internal.JVar; 34 import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl; 35 import com.sun.tools.internal.xjc.generator.bean.MethodWriter; 36 import com.sun.tools.internal.xjc.model.CPropertyInfo; 37 import com.sun.tools.internal.xjc.outline.FieldAccessor; 38 import com.sun.tools.internal.xjc.outline.FieldOutline; 39 40 /** 41 * 42 * 43 * @author 44 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 45 */ 46 public class IsSetField extends AbstractField { 47 48 private final FieldOutline core; 49 50 private final boolean generateUnSetMethod; 51 private final boolean generateIsSetMethod; 52 53 protected IsSetField( ClassOutlineImpl outline, CPropertyInfo prop, 54 FieldOutline core, boolean unsetMethod, boolean issetMethod ) { 55 super(outline,prop); 56 this.core = core; 57 this.generateIsSetMethod = issetMethod; 58 this.generateUnSetMethod = unsetMethod; 59 60 generate(outline,prop); 61 } 62 63 64 private void generate( ClassOutlineImpl outline, CPropertyInfo prop ) { 65 // add isSetXXX and unsetXXX. 66 MethodWriter writer = outline.createMethodWriter(); 67 68 JCodeModel codeModel = outline.parent().getCodeModel(); 69 70 FieldAccessor acc = core.create(JExpr._this()); 71 72 if( generateIsSetMethod ) { 73 // [RESULT] boolean isSetXXX() 74 JExpression hasSetValue = acc.hasSetValue(); 75 if( hasSetValue==null ) { 76 // this field renderer doesn't support the isSet/unset methods generation. 77 // issue an error 78 throw new UnsupportedOperationException(); 79 } 80 writer.declareMethod(codeModel.BOOLEAN,"isSet"+this.prop.getName(true)) 81 .body()._return( hasSetValue ); 82 } 83 84 if( generateUnSetMethod ) { 85 // [RESULT] void unsetXXX() 86 acc.unsetValues( 87 writer.declareMethod(codeModel.VOID,"unset"+this.prop.getName(true)).body() ); 88 } 89 } 90 91 public JType getRawType() { 92 return core.getRawType(); 93 } 94 95 public FieldAccessor create(JExpression targetObject) { 96 return new Accessor(targetObject); 97 } 98 99 private class Accessor extends AbstractField.Accessor { 100 101 private final FieldAccessor core; 102 103 Accessor( JExpression $target ) { 104 super($target); 105 this.core = IsSetField.this.core.create($target); 106 } 107 108 109 public void unsetValues( JBlock body ) { 110 core.unsetValues(body); 111 } 112 public JExpression hasSetValue() { 113 return core.hasSetValue(); 114 } 115 public void toRawValue(JBlock block, JVar $var) { 116 core.toRawValue(block,$var); 117 } 118 119 public void fromRawValue(JBlock block, String uniqueName, JExpression $var) { 120 core.fromRawValue(block,uniqueName,$var); 121 } 122 } 123 }