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
26package com.sun.tools.internal.xjc.generator.bean.field;
27
28import com.sun.codemodel.internal.JBlock;
29import com.sun.codemodel.internal.JExpr;
30import com.sun.codemodel.internal.JExpression;
31import com.sun.codemodel.internal.JFieldVar;
32import com.sun.codemodel.internal.JMod;
33import com.sun.codemodel.internal.JPrimitiveType;
34import com.sun.codemodel.internal.JType;
35import com.sun.codemodel.internal.JVar;
36import com.sun.tools.internal.xjc.generator.bean.ClassOutlineImpl;
37import com.sun.tools.internal.xjc.model.CPropertyInfo;
38import com.sun.tools.internal.xjc.outline.FieldAccessor;
39
40/**
41 * Realizes a property as a "public static final" property on the interface.
42 * This class can handle both boxed/unboxed types and both
43 * single/colllection.
44 *
45 * @author
46 *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
47 */
48final class ConstField extends AbstractField {
49//    /**
50//     * Number of items in this property, when
51//     * {@link #isCollection}==true.
52//     */
53//    private final int count=1;
54
55    /** Generated constant property on the interface. */
56    private final JFieldVar $ref;
57
58    ConstField( ClassOutlineImpl outline, CPropertyInfo prop ) {
59        super(outline,prop);
60
61        // we only support value constraints for a single-value property.
62        assert !prop.isCollection();
63
64        JPrimitiveType ptype = implType.boxify().getPrimitiveType();
65
66        // generate the constant
67        JExpression defaultValue = null;
68        if(prop.defaultValue!=null)
69            defaultValue = prop.defaultValue.compute(outline.parent());
70
71        $ref = outline.ref.field(JMod.PUBLIC|JMod.STATIC|JMod.FINAL,
72            ptype!=null?ptype:implType, prop.getName(true), defaultValue );
73        $ref.javadoc().append(prop.javadoc);
74
75        annotate($ref);
76    }
77
78    public JType getRawType() {
79//        if( isCollection )      return getInfo().array();
80        return exposedType;
81    }
82
83
84    public FieldAccessor create(JExpression target) {
85        return new Accessor(target);
86    }
87
88    private class Accessor extends AbstractField.Accessor {
89
90        Accessor( JExpression $target ) {
91            super($target);
92        }
93
94        public void unsetValues( JBlock body ) {
95            ;   // can't unset values
96        }
97        public JExpression hasSetValue() {
98            return null;    // can't generate the isSet/unset methods
99        }
100        public void toRawValue(JBlock block, JVar $var) {
101            // TODO: rethink abstraction. Those constant fields
102            // don't have "access" to them.
103            throw new UnsupportedOperationException();
104        }
105
106        public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
107            throw new UnsupportedOperationException();
108        }
109    }
110}
111