001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020:
021: package com.sun.codemodel;
022:
023: import java.io.PrintWriter;
024: import java.io.StringWriter;
025:
026: /**
027: * Modifier groups.
028: */
029: public class JMods implements JGenerable {
030:
031: //
032: // mask
033: //
034: private static int VAR = JMod.FINAL;
035:
036: private static int FIELD = (JMod.PUBLIC | JMod.PRIVATE
037: | JMod.PROTECTED | JMod.STATIC | JMod.FINAL
038: | JMod.TRANSIENT | JMod.VOLATILE);
039:
040: private static int METHOD = (JMod.PUBLIC | JMod.PRIVATE
041: | JMod.PROTECTED | JMod.FINAL | JMod.ABSTRACT | JMod.STATIC
042: | JMod.NATIVE | JMod.SYNCHRONIZED);
043:
044: private static int CLASS = (JMod.PUBLIC | JMod.PRIVATE
045: | JMod.PROTECTED | JMod.STATIC | JMod.FINAL | JMod.ABSTRACT);
046:
047: private static int INTERFACE = JMod.PUBLIC;
048:
049: /** bit-packed representation of modifiers. */
050: private int mods;
051:
052: private JMods(int mods) {
053: this .mods = mods;
054: }
055:
056: /**
057: * Gets the bit-packed representaion of modifiers.
058: */
059: public int getValue() {
060: return mods;
061: }
062:
063: private static void check(int mods, int legal, String what) {
064: if ((mods & ~legal) != 0)
065: throw new IllegalArgumentException("Illegal modifiers for "
066: + what + ": " + new JMods(mods).toString());
067: /* ## check for illegal combinations too */
068: }
069:
070: static JMods forVar(int mods) {
071: check(mods, VAR, "variable");
072: return new JMods(mods);
073: }
074:
075: static JMods forField(int mods) {
076: check(mods, FIELD, "field");
077: return new JMods(mods);
078: }
079:
080: static JMods forMethod(int mods) {
081: check(mods, METHOD, "method");
082: return new JMods(mods);
083: }
084:
085: static JMods forClass(int mods) {
086: check(mods, CLASS, "class");
087: return new JMods(mods);
088: }
089:
090: static JMods forInterface(int mods) {
091: check(mods, INTERFACE, "class");
092: return new JMods(mods);
093: }
094:
095: public boolean isAbstract() {
096: return (mods & JMod.ABSTRACT) != 0;
097: }
098:
099: public boolean isNative() {
100: return (mods & JMod.NATIVE) != 0;
101: }
102:
103: public boolean isSynchronized() {
104: return (mods & JMod.SYNCHRONIZED) != 0;
105: }
106:
107: public void setSynchronized(boolean newValue) {
108: setFlag(JMod.SYNCHRONIZED, newValue);
109: }
110:
111: // TODO: more
112:
113: private void setFlag(int bit, boolean newValue) {
114: mods = (mods & ~bit) | (newValue ? bit : 0);
115: }
116:
117: public void generate(JFormatter f) {
118: if ((mods & JMod.PUBLIC) != 0)
119: f.p("public");
120: if ((mods & JMod.PROTECTED) != 0)
121: f.p("protected");
122: if ((mods & JMod.PRIVATE) != 0)
123: f.p("private");
124: if ((mods & JMod.FINAL) != 0)
125: f.p("final");
126: if ((mods & JMod.STATIC) != 0)
127: f.p("static");
128: if ((mods & JMod.ABSTRACT) != 0)
129: f.p("abstract");
130: if ((mods & JMod.NATIVE) != 0)
131: f.p("native");
132: if ((mods & JMod.SYNCHRONIZED) != 0)
133: f.p("synchronized");
134: if ((mods & JMod.TRANSIENT) != 0)
135: f.p("transient");
136: if ((mods & JMod.VOLATILE) != 0)
137: f.p("volatile");
138: }
139:
140: public String toString() {
141: StringWriter s = new StringWriter();
142: JFormatter f = new JFormatter(new PrintWriter(s));
143: this.generate(f);
144: return s.toString();
145: }
146:
147: }
|