jaxws/src/share/classes/com/sun/codemodel/internal/JMods.java
author tbell
Mon, 04 May 2009 21:10:41 -0700
changeset 2719 99d59312294b
parent 2678 57cf2a1c1a05
permissions -rw-r--r--
6658158: Mutable statics in SAAJ (findbugs) 6658163: txw2.DatatypeWriter.BUILDIN is a mutable static (findbugs) Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
474761f14bca Initial load
duke
parents:
diff changeset
     1
/*
2678
57cf2a1c1a05 6831313: update jaxws in OpenJDK7 to 2.1 plus bug fixes from OpenJDK 6
tbell
parents: 8
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
8
474761f14bca Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
474761f14bca Initial load
duke
parents:
diff changeset
     4
 *
474761f14bca Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
474761f14bca Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
474761f14bca Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
474761f14bca Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
474761f14bca Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
474761f14bca Initial load
duke
parents:
diff changeset
    10
 *
474761f14bca Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
474761f14bca Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
474761f14bca Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
474761f14bca Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
474761f14bca Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
474761f14bca Initial load
duke
parents:
diff changeset
    16
 *
474761f14bca Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
474761f14bca Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
474761f14bca Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
474761f14bca Initial load
duke
parents:
diff changeset
    20
 *
474761f14bca Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
474761f14bca Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
474761f14bca Initial load
duke
parents:
diff changeset
    23
 * have any questions.
474761f14bca Initial load
duke
parents:
diff changeset
    24
 */
474761f14bca Initial load
duke
parents:
diff changeset
    25
474761f14bca Initial load
duke
parents:
diff changeset
    26
package com.sun.codemodel.internal;
474761f14bca Initial load
duke
parents:
diff changeset
    27
474761f14bca Initial load
duke
parents:
diff changeset
    28
import java.io.PrintWriter;
474761f14bca Initial load
duke
parents:
diff changeset
    29
import java.io.StringWriter;
474761f14bca Initial load
duke
parents:
diff changeset
    30
474761f14bca Initial load
duke
parents:
diff changeset
    31
474761f14bca Initial load
duke
parents:
diff changeset
    32
/**
474761f14bca Initial load
duke
parents:
diff changeset
    33
 * Modifier groups.
474761f14bca Initial load
duke
parents:
diff changeset
    34
 */
474761f14bca Initial load
duke
parents:
diff changeset
    35
public class JMods implements JGenerable {
474761f14bca Initial load
duke
parents:
diff changeset
    36
474761f14bca Initial load
duke
parents:
diff changeset
    37
//
474761f14bca Initial load
duke
parents:
diff changeset
    38
// mask
474761f14bca Initial load
duke
parents:
diff changeset
    39
//
474761f14bca Initial load
duke
parents:
diff changeset
    40
    private static int VAR
474761f14bca Initial load
duke
parents:
diff changeset
    41
        = JMod.FINAL;
474761f14bca Initial load
duke
parents:
diff changeset
    42
474761f14bca Initial load
duke
parents:
diff changeset
    43
    private static int FIELD
2719
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    44
        = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    45
           | JMod.STATIC | JMod.FINAL
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    46
           | JMod.TRANSIENT | JMod.VOLATILE);
8
474761f14bca Initial load
duke
parents:
diff changeset
    47
474761f14bca Initial load
duke
parents:
diff changeset
    48
    private static int METHOD
2719
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    49
        = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED | JMod.FINAL
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    50
           | JMod.ABSTRACT | JMod.STATIC | JMod.NATIVE | JMod.SYNCHRONIZED);
8
474761f14bca Initial load
duke
parents:
diff changeset
    51
474761f14bca Initial load
duke
parents:
diff changeset
    52
    private static int CLASS
2719
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    53
        = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED
99d59312294b 6658158: Mutable statics in SAAJ (findbugs)
tbell
parents: 2678
diff changeset
    54
           | JMod.STATIC | JMod.FINAL | JMod.ABSTRACT );
8
474761f14bca Initial load
duke
parents:
diff changeset
    55
474761f14bca Initial load
duke
parents:
diff changeset
    56
    private static int INTERFACE = JMod.PUBLIC;
474761f14bca Initial load
duke
parents:
diff changeset
    57
474761f14bca Initial load
duke
parents:
diff changeset
    58
    /** bit-packed representation of modifiers. */
474761f14bca Initial load
duke
parents:
diff changeset
    59
    private int mods;
474761f14bca Initial load
duke
parents:
diff changeset
    60
474761f14bca Initial load
duke
parents:
diff changeset
    61
    private JMods(int mods) {
474761f14bca Initial load
duke
parents:
diff changeset
    62
        this.mods = mods;
474761f14bca Initial load
duke
parents:
diff changeset
    63
    }
474761f14bca Initial load
duke
parents:
diff changeset
    64
474761f14bca Initial load
duke
parents:
diff changeset
    65
    /**
474761f14bca Initial load
duke
parents:
diff changeset
    66
     * Gets the bit-packed representaion of modifiers.
474761f14bca Initial load
duke
parents:
diff changeset
    67
     */
474761f14bca Initial load
duke
parents:
diff changeset
    68
    public int getValue() {
474761f14bca Initial load
duke
parents:
diff changeset
    69
        return mods;
474761f14bca Initial load
duke
parents:
diff changeset
    70
    }
474761f14bca Initial load
duke
parents:
diff changeset
    71
474761f14bca Initial load
duke
parents:
diff changeset
    72
    private static void check(int mods, int legal, String what) {
474761f14bca Initial load
duke
parents:
diff changeset
    73
        if ((mods & ~legal) != 0)
474761f14bca Initial load
duke
parents:
diff changeset
    74
            throw new IllegalArgumentException("Illegal modifiers for "
474761f14bca Initial load
duke
parents:
diff changeset
    75
                    + what + ": "
474761f14bca Initial load
duke
parents:
diff changeset
    76
                    + new JMods(mods).toString());
474761f14bca Initial load
duke
parents:
diff changeset
    77
        /* ## check for illegal combinations too */
474761f14bca Initial load
duke
parents:
diff changeset
    78
    }
474761f14bca Initial load
duke
parents:
diff changeset
    79
474761f14bca Initial load
duke
parents:
diff changeset
    80
    static JMods forVar(int mods) {
474761f14bca Initial load
duke
parents:
diff changeset
    81
        check(mods, VAR, "variable");
474761f14bca Initial load
duke
parents:
diff changeset
    82
        return new JMods(mods);
474761f14bca Initial load
duke
parents:
diff changeset
    83
    }
474761f14bca Initial load
duke
parents:
diff changeset
    84
474761f14bca Initial load
duke
parents:
diff changeset
    85
    static JMods forField(int mods) {
474761f14bca Initial load
duke
parents:
diff changeset
    86
        check(mods, FIELD, "field");
474761f14bca Initial load
duke
parents:
diff changeset
    87
        return new JMods(mods);
474761f14bca Initial load
duke
parents:
diff changeset
    88
    }
474761f14bca Initial load
duke
parents:
diff changeset
    89
474761f14bca Initial load
duke
parents:
diff changeset
    90
    static JMods forMethod(int mods) {
474761f14bca Initial load
duke
parents:
diff changeset
    91
        check(mods, METHOD, "method");
474761f14bca Initial load
duke
parents:
diff changeset
    92
        return new JMods(mods);
474761f14bca Initial load
duke
parents:
diff changeset
    93
    }
474761f14bca Initial load
duke
parents:
diff changeset
    94
474761f14bca Initial load
duke
parents:
diff changeset
    95
    static JMods forClass(int mods) {
474761f14bca Initial load
duke
parents:
diff changeset
    96
        check(mods, CLASS, "class");
474761f14bca Initial load
duke
parents:
diff changeset
    97
        return new JMods(mods);
474761f14bca Initial load
duke
parents:
diff changeset
    98
    }
474761f14bca Initial load
duke
parents:
diff changeset
    99
474761f14bca Initial load
duke
parents:
diff changeset
   100
    static JMods forInterface(int mods) {
474761f14bca Initial load
duke
parents:
diff changeset
   101
        check(mods, INTERFACE, "class");
474761f14bca Initial load
duke
parents:
diff changeset
   102
        return new JMods(mods);
474761f14bca Initial load
duke
parents:
diff changeset
   103
    }
474761f14bca Initial load
duke
parents:
diff changeset
   104
474761f14bca Initial load
duke
parents:
diff changeset
   105
    public boolean isAbstract() {
474761f14bca Initial load
duke
parents:
diff changeset
   106
        return (mods & JMod.ABSTRACT) != 0;
474761f14bca Initial load
duke
parents:
diff changeset
   107
    }
474761f14bca Initial load
duke
parents:
diff changeset
   108
474761f14bca Initial load
duke
parents:
diff changeset
   109
    public boolean isNative() {
474761f14bca Initial load
duke
parents:
diff changeset
   110
        return (mods & JMod.NATIVE) != 0;
474761f14bca Initial load
duke
parents:
diff changeset
   111
    }
474761f14bca Initial load
duke
parents:
diff changeset
   112
474761f14bca Initial load
duke
parents:
diff changeset
   113
    public boolean isSynchronized() {
474761f14bca Initial load
duke
parents:
diff changeset
   114
        return (mods & JMod.SYNCHRONIZED) != 0;
474761f14bca Initial load
duke
parents:
diff changeset
   115
    }
474761f14bca Initial load
duke
parents:
diff changeset
   116
474761f14bca Initial load
duke
parents:
diff changeset
   117
    public void setSynchronized(boolean newValue) {
474761f14bca Initial load
duke
parents:
diff changeset
   118
        setFlag( JMod.SYNCHRONIZED, newValue );
474761f14bca Initial load
duke
parents:
diff changeset
   119
    }
474761f14bca Initial load
duke
parents:
diff changeset
   120
474761f14bca Initial load
duke
parents:
diff changeset
   121
    // TODO: more
474761f14bca Initial load
duke
parents:
diff changeset
   122
474761f14bca Initial load
duke
parents:
diff changeset
   123
    private void setFlag( int bit, boolean newValue ) {
474761f14bca Initial load
duke
parents:
diff changeset
   124
        mods = (mods & ~bit) | (newValue?bit:0);
474761f14bca Initial load
duke
parents:
diff changeset
   125
    }
474761f14bca Initial load
duke
parents:
diff changeset
   126
474761f14bca Initial load
duke
parents:
diff changeset
   127
    public void generate(JFormatter f) {
474761f14bca Initial load
duke
parents:
diff changeset
   128
        if ((mods & JMod.PUBLIC) != 0)        f.p("public");
474761f14bca Initial load
duke
parents:
diff changeset
   129
        if ((mods & JMod.PROTECTED) != 0)     f.p("protected");
474761f14bca Initial load
duke
parents:
diff changeset
   130
        if ((mods & JMod.PRIVATE) != 0)       f.p("private");
474761f14bca Initial load
duke
parents:
diff changeset
   131
        if ((mods & JMod.FINAL) != 0)         f.p("final");
474761f14bca Initial load
duke
parents:
diff changeset
   132
        if ((mods & JMod.STATIC) != 0)        f.p("static");
474761f14bca Initial load
duke
parents:
diff changeset
   133
        if ((mods & JMod.ABSTRACT) != 0)      f.p("abstract");
474761f14bca Initial load
duke
parents:
diff changeset
   134
        if ((mods & JMod.NATIVE) != 0)        f.p("native");
474761f14bca Initial load
duke
parents:
diff changeset
   135
        if ((mods & JMod.SYNCHRONIZED) != 0)  f.p("synchronized");
474761f14bca Initial load
duke
parents:
diff changeset
   136
        if ((mods & JMod.TRANSIENT) != 0)     f.p("transient");
474761f14bca Initial load
duke
parents:
diff changeset
   137
        if ((mods & JMod.VOLATILE) != 0)      f.p("volatile");
474761f14bca Initial load
duke
parents:
diff changeset
   138
    }
474761f14bca Initial load
duke
parents:
diff changeset
   139
474761f14bca Initial load
duke
parents:
diff changeset
   140
    public String toString() {
474761f14bca Initial load
duke
parents:
diff changeset
   141
        StringWriter s = new StringWriter();
474761f14bca Initial load
duke
parents:
diff changeset
   142
        JFormatter f = new JFormatter(new PrintWriter(s));
474761f14bca Initial load
duke
parents:
diff changeset
   143
        this.generate(f);
474761f14bca Initial load
duke
parents:
diff changeset
   144
        return s.toString();
474761f14bca Initial load
duke
parents:
diff changeset
   145
    }
474761f14bca Initial load
duke
parents:
diff changeset
   146
474761f14bca Initial load
duke
parents:
diff changeset
   147
}