View Javadoc
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.codemodel.internal;
27  
28  import java.io.PrintWriter;
29  import java.io.StringWriter;
30  
31  /**
32   * Modifier groups.
33   */
34  public class JMods implements JGenerable {
35  
36  //
37  // mask
38  //
39      private static int VAR = JMod.FINAL;
40      private static int FIELD = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED
41              | JMod.STATIC | JMod.FINAL
42              | JMod.TRANSIENT | JMod.VOLATILE);
43      private static int METHOD = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED | JMod.FINAL
44              | JMod.ABSTRACT | JMod.STATIC | JMod.NATIVE | JMod.SYNCHRONIZED);
45      private static int CLASS = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED
46              | JMod.STATIC | JMod.FINAL | JMod.ABSTRACT);
47      private static int INTERFACE = JMod.PUBLIC;
48      /** bit-packed representation of modifiers. */
49      private int mods;
50  
51      private JMods(int mods) {
52          this.mods = mods;
53      }
54  
55      /**
56       * Gets the bit-packed representaion of modifiers.
57       */
58      public int getValue() {
59          return mods;
60      }
61  
62      private static void check(int mods, int legal, String what) {
63          if ((mods & ~legal) != 0) {
64              throw new IllegalArgumentException("Illegal modifiers for "
65                      + what + ": "
66                      + new JMods(mods).toString());
67          }
68          /* ## check for illegal combinations too */
69      }
70  
71      static JMods forVar(int mods) {
72          check(mods, VAR, "variable");
73          return new JMods(mods);
74      }
75  
76      static JMods forField(int mods) {
77          check(mods, FIELD, "field");
78          return new JMods(mods);
79      }
80  
81      static JMods forMethod(int mods) {
82          check(mods, METHOD, "method");
83          return new JMods(mods);
84      }
85  
86      static JMods forClass(int mods) {
87          check(mods, CLASS, "class");
88          return new JMods(mods);
89      }
90  
91      static JMods forInterface(int mods) {
92          check(mods, INTERFACE, "class");
93          return new JMods(mods);
94      }
95  
96      public boolean isAbstract() {
97          return (mods & JMod.ABSTRACT) != 0;
98      }
99  
100     public boolean isNative() {
101         return (mods & JMod.NATIVE) != 0;
102     }
103 
104     public boolean isSynchronized() {
105         return (mods & JMod.SYNCHRONIZED) != 0;
106     }
107 
108     public void setSynchronized(boolean newValue) {
109         setFlag(JMod.SYNCHRONIZED, newValue);
110     }
111 
112     public void setPrivate() {
113         setFlag(JMod.PUBLIC, false);
114         setFlag(JMod.PROTECTED, false);
115         setFlag(JMod.PRIVATE, true);
116     }
117 
118     public void setProtected() {
119         setFlag(JMod.PUBLIC, false);
120         setFlag(JMod.PROTECTED, true);
121         setFlag(JMod.PRIVATE, false);
122     }
123 
124     public void setPublic() {
125         setFlag(JMod.PUBLIC, true);
126         setFlag(JMod.PROTECTED, false);
127         setFlag(JMod.PRIVATE, false);
128     }
129 
130     public void setFinal(boolean newValue) {
131         setFlag(JMod.FINAL, newValue);
132     }
133 
134     private void setFlag(int bit, boolean newValue) {
135         mods = (mods & ~bit) | (newValue ? bit : 0);
136     }
137 
138     public void generate(JFormatter f) {
139         if ((mods & JMod.PUBLIC) != 0)        f.p("public");
140         if ((mods & JMod.PROTECTED) != 0)     f.p("protected");
141         if ((mods & JMod.PRIVATE) != 0)       f.p("private");
142         if ((mods & JMod.FINAL) != 0)         f.p("final");
143         if ((mods & JMod.STATIC) != 0)        f.p("static");
144         if ((mods & JMod.ABSTRACT) != 0)      f.p("abstract");
145         if ((mods & JMod.NATIVE) != 0)        f.p("native");
146         if ((mods & JMod.SYNCHRONIZED) != 0)  f.p("synchronized");
147         if ((mods & JMod.TRANSIENT) != 0)     f.p("transient");
148         if ((mods & JMod.VOLATILE) != 0)      f.p("volatile");
149         }
150 
151     @Override
152     public String toString() {
153         StringWriter s = new StringWriter();
154         JFormatter f = new JFormatter(new PrintWriter(s));
155         this.generate(f);
156         return s.toString();
157     }
158 }