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

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

mercurial