001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.apt.mirror;
027:
028: import com.sun.tools.apt.mirror.declaration.DeclarationMaker;
029: import com.sun.tools.apt.mirror.type.TypeMaker;
030: import com.sun.tools.javac.code.*;
031: import com.sun.tools.javac.code.Symbol.CompletionFailure;
032: import com.sun.tools.javac.comp.Attr;
033: import com.sun.tools.javac.comp.Enter;
034: import com.sun.tools.javac.util.Context;
035: import com.sun.tools.javac.util.Name;
036:
037: /**
038: * The environment for a run of apt.
039: */
040:
041: public class AptEnv {
042:
043: public Name.Table names; // javac's name table
044: public Symtab symtab; // javac's predefined symbols
045: public Types jctypes; // javac's type utilities
046: public Enter enter; // javac's enter phase
047: public Attr attr; // javac's attr phase (to evaluate
048: // constant initializers)
049: public TypeMaker typeMaker; // apt's internal type utilities
050: public DeclarationMaker declMaker; // apt's internal declaration utilities
051:
052: private static final Context.Key<AptEnv> aptEnvKey = new Context.Key<AptEnv>();
053:
054: public static AptEnv instance(Context context) {
055: AptEnv instance = context.get(aptEnvKey);
056: if (instance == null) {
057: instance = new AptEnv(context);
058: }
059: return instance;
060: }
061:
062: private AptEnv(Context context) {
063: context.put(aptEnvKey, this );
064:
065: names = Name.Table.instance(context);
066: symtab = Symtab.instance(context);
067: jctypes = Types.instance(context);
068: enter = Enter.instance(context);
069: attr = Attr.instance(context);
070: typeMaker = TypeMaker.instance(context);
071: declMaker = DeclarationMaker.instance(context);
072: }
073:
074: /**
075: * Does a symbol have a given flag? Forces symbol completion.
076: */
077: public static boolean hasFlag(Symbol sym, long flag) {
078: return (getFlags(sym) & flag) != 0;
079: }
080:
081: /**
082: * Returns a symbol's flags. Forces completion.
083: */
084: public static long getFlags(Symbol sym) {
085: complete(sym);
086: return sym.flags();
087: }
088:
089: /**
090: * Completes a symbol, ignoring completion failures.
091: */
092: private static void complete(Symbol sym) {
093: while (true) {
094: try {
095: sym.complete();
096: return;
097: } catch (CompletionFailure e) {
098: // Should never see two in a row, but loop just to be sure.
099: }
100: }
101: }
102: }
|