src/share/classes/com/sun/codemodel/internal/JMods.java

Mon, 04 May 2009 21:10:41 -0700

author
tbell
date
Mon, 04 May 2009 21:10:41 -0700
changeset 50
42dfec6871f6
parent 45
31822b475baa
permissions
-rw-r--r--

6658158: Mutable statics in SAAJ (findbugs)
6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs)
Reviewed-by: darcy

     1 /*
     2  * Copyright 2005-2006 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    26 package com.sun.codemodel.internal;
    28 import java.io.PrintWriter;
    29 import java.io.StringWriter;
    32 /**
    33  * Modifier groups.
    34  */
    35 public class JMods implements JGenerable {
    37 //
    38 // mask
    39 //
    40     private static int VAR
    41         = JMod.FINAL;
    43     private static int FIELD
    44         = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED
    45            | JMod.STATIC | JMod.FINAL
    46            | JMod.TRANSIENT | JMod.VOLATILE);
    48     private static int METHOD
    49         = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED | JMod.FINAL
    50            | JMod.ABSTRACT | JMod.STATIC | JMod.NATIVE | JMod.SYNCHRONIZED);
    52     private static int CLASS
    53         = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED
    54            | JMod.STATIC | JMod.FINAL | JMod.ABSTRACT );
    56     private static int INTERFACE = JMod.PUBLIC;
    58     /** bit-packed representation of modifiers. */
    59     private int mods;
    61     private JMods(int mods) {
    62         this.mods = mods;
    63     }
    65     /**
    66      * Gets the bit-packed representaion of modifiers.
    67      */
    68     public int getValue() {
    69         return mods;
    70     }
    72     private static void check(int mods, int legal, String what) {
    73         if ((mods & ~legal) != 0)
    74             throw new IllegalArgumentException("Illegal modifiers for "
    75                     + what + ": "
    76                     + new JMods(mods).toString());
    77         /* ## check for illegal combinations too */
    78     }
    80     static JMods forVar(int mods) {
    81         check(mods, VAR, "variable");
    82         return new JMods(mods);
    83     }
    85     static JMods forField(int mods) {
    86         check(mods, FIELD, "field");
    87         return new JMods(mods);
    88     }
    90     static JMods forMethod(int mods) {
    91         check(mods, METHOD, "method");
    92         return new JMods(mods);
    93     }
    95     static JMods forClass(int mods) {
    96         check(mods, CLASS, "class");
    97         return new JMods(mods);
    98     }
   100     static JMods forInterface(int mods) {
   101         check(mods, INTERFACE, "class");
   102         return new JMods(mods);
   103     }
   105     public boolean isAbstract() {
   106         return (mods & JMod.ABSTRACT) != 0;
   107     }
   109     public boolean isNative() {
   110         return (mods & JMod.NATIVE) != 0;
   111     }
   113     public boolean isSynchronized() {
   114         return (mods & JMod.SYNCHRONIZED) != 0;
   115     }
   117     public void setSynchronized(boolean newValue) {
   118         setFlag( JMod.SYNCHRONIZED, newValue );
   119     }
   121     // TODO: more
   123     private void setFlag( int bit, boolean newValue ) {
   124         mods = (mods & ~bit) | (newValue?bit:0);
   125     }
   127     public void generate(JFormatter f) {
   128         if ((mods & JMod.PUBLIC) != 0)        f.p("public");
   129         if ((mods & JMod.PROTECTED) != 0)     f.p("protected");
   130         if ((mods & JMod.PRIVATE) != 0)       f.p("private");
   131         if ((mods & JMod.FINAL) != 0)         f.p("final");
   132         if ((mods & JMod.STATIC) != 0)        f.p("static");
   133         if ((mods & JMod.ABSTRACT) != 0)      f.p("abstract");
   134         if ((mods & JMod.NATIVE) != 0)        f.p("native");
   135         if ((mods & JMod.SYNCHRONIZED) != 0)  f.p("synchronized");
   136         if ((mods & JMod.TRANSIENT) != 0)     f.p("transient");
   137         if ((mods & JMod.VOLATILE) != 0)      f.p("volatile");
   138     }
   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     }
   147 }

mercurial