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.codemodel.internal;
27
28
29/**
30 * Java built-in primitive types.
31 *
32 * Instances of this class can be obtained as constants of {@link JCodeModel},
33 * such as {@link JCodeModel#BOOLEAN}.
34 */
35public final class JPrimitiveType extends JType {
36
37    private final String typeName;
38    private final JCodeModel owner;
39    /**
40     * Corresponding wrapper class.
41     * For example, this would be "java.lang.Short" for short.
42     */
43    private final JClass wrapperClass;
44
45    JPrimitiveType(JCodeModel owner, String typeName, Class<?> wrapper ) {
46        this.owner = owner;
47        this.typeName = typeName;
48        this.wrapperClass = owner.ref(wrapper);
49    }
50
51    public JCodeModel owner() { return owner; }
52
53    public String fullName() {
54        return typeName;
55    }
56
57    public String name() {
58        return fullName();
59    }
60
61    public boolean isPrimitive() {
62        return true;
63    }
64
65    private JClass arrayClass;
66    public JClass array() {
67        if(arrayClass==null)
68            arrayClass = new JArrayClass(owner,this);
69        return arrayClass;
70    }
71
72    /**
73     * Obtains the wrapper class for this primitive type.
74     * For example, this method returns a reference to java.lang.Integer
75     * if this object represents int.
76     */
77    public JClass boxify() {
78        return wrapperClass;
79    }
80
81    /**
82     * @deprecated calling this method from {@link JPrimitiveType}
83     * would be meaningless, since it's always guaranteed to
84     * return {@code this}.
85     */
86    public JType unboxify() {
87        return this;
88    }
89
90    /**
91     * @deprecated
92     *      Use {@link #boxify()}.
93     */
94    public JClass getWrapperClass() {
95        return boxify();
96    }
97
98    /**
99     * Wraps an expression of this type to the corresponding wrapper class.
100     * For example, if this class represents "float", this method will return
101     * the expression <code>new Float(x)</code> for the paramter x.
102     *
103     * REVISIT: it's not clear how this method works for VOID.
104     */
105    public JExpression wrap( JExpression exp ) {
106        return JExpr._new(boxify()).arg(exp);
107    }
108
109    /**
110     * Do the opposite of the wrap method.
111     *
112     * REVISIT: it's not clear how this method works for VOID.
113     */
114    public JExpression unwrap( JExpression exp ) {
115        // it just so happens that the unwrap method is always
116        // things like "intValue" or "booleanValue".
117        return exp.invoke(typeName+"Value");
118    }
119
120    public void generate(JFormatter f) {
121        f.p(typeName);
122    }
123}
124