duke@1: /* tbell@45: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.codemodel.internal; duke@1: duke@1: import java.io.PrintWriter; duke@1: import java.io.StringWriter; duke@1: duke@1: duke@1: /** duke@1: * Modifier groups. duke@1: */ duke@1: public class JMods implements JGenerable { duke@1: duke@1: // duke@1: // mask duke@1: // duke@1: private static int VAR duke@1: = JMod.FINAL; duke@1: duke@1: private static int FIELD tbell@50: = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED tbell@50: | JMod.STATIC | JMod.FINAL tbell@50: | JMod.TRANSIENT | JMod.VOLATILE); duke@1: duke@1: private static int METHOD tbell@50: = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED | JMod.FINAL tbell@50: | JMod.ABSTRACT | JMod.STATIC | JMod.NATIVE | JMod.SYNCHRONIZED); duke@1: duke@1: private static int CLASS tbell@50: = (JMod.PUBLIC | JMod.PRIVATE | JMod.PROTECTED tbell@50: | JMod.STATIC | JMod.FINAL | JMod.ABSTRACT ); duke@1: duke@1: private static int INTERFACE = JMod.PUBLIC; duke@1: duke@1: /** bit-packed representation of modifiers. */ duke@1: private int mods; duke@1: duke@1: private JMods(int mods) { duke@1: this.mods = mods; duke@1: } duke@1: duke@1: /** duke@1: * Gets the bit-packed representaion of modifiers. duke@1: */ duke@1: public int getValue() { duke@1: return mods; duke@1: } duke@1: duke@1: private static void check(int mods, int legal, String what) { duke@1: if ((mods & ~legal) != 0) duke@1: throw new IllegalArgumentException("Illegal modifiers for " duke@1: + what + ": " duke@1: + new JMods(mods).toString()); duke@1: /* ## check for illegal combinations too */ duke@1: } duke@1: duke@1: static JMods forVar(int mods) { duke@1: check(mods, VAR, "variable"); duke@1: return new JMods(mods); duke@1: } duke@1: duke@1: static JMods forField(int mods) { duke@1: check(mods, FIELD, "field"); duke@1: return new JMods(mods); duke@1: } duke@1: duke@1: static JMods forMethod(int mods) { duke@1: check(mods, METHOD, "method"); duke@1: return new JMods(mods); duke@1: } duke@1: duke@1: static JMods forClass(int mods) { duke@1: check(mods, CLASS, "class"); duke@1: return new JMods(mods); duke@1: } duke@1: duke@1: static JMods forInterface(int mods) { duke@1: check(mods, INTERFACE, "class"); duke@1: return new JMods(mods); duke@1: } duke@1: duke@1: public boolean isAbstract() { duke@1: return (mods & JMod.ABSTRACT) != 0; duke@1: } duke@1: duke@1: public boolean isNative() { duke@1: return (mods & JMod.NATIVE) != 0; duke@1: } duke@1: duke@1: public boolean isSynchronized() { duke@1: return (mods & JMod.SYNCHRONIZED) != 0; duke@1: } duke@1: duke@1: public void setSynchronized(boolean newValue) { duke@1: setFlag( JMod.SYNCHRONIZED, newValue ); duke@1: } duke@1: duke@1: // TODO: more duke@1: duke@1: private void setFlag( int bit, boolean newValue ) { duke@1: mods = (mods & ~bit) | (newValue?bit:0); duke@1: } duke@1: duke@1: public void generate(JFormatter f) { duke@1: if ((mods & JMod.PUBLIC) != 0) f.p("public"); duke@1: if ((mods & JMod.PROTECTED) != 0) f.p("protected"); duke@1: if ((mods & JMod.PRIVATE) != 0) f.p("private"); duke@1: if ((mods & JMod.FINAL) != 0) f.p("final"); duke@1: if ((mods & JMod.STATIC) != 0) f.p("static"); duke@1: if ((mods & JMod.ABSTRACT) != 0) f.p("abstract"); duke@1: if ((mods & JMod.NATIVE) != 0) f.p("native"); duke@1: if ((mods & JMod.SYNCHRONIZED) != 0) f.p("synchronized"); duke@1: if ((mods & JMod.TRANSIENT) != 0) f.p("transient"); duke@1: if ((mods & JMod.VOLATILE) != 0) f.p("volatile"); duke@1: } duke@1: duke@1: public String toString() { duke@1: StringWriter s = new StringWriter(); duke@1: JFormatter f = new JFormatter(new PrintWriter(s)); duke@1: this.generate(f); duke@1: return s.toString(); duke@1: } duke@1: duke@1: }