1 /* 2 * Copyright (c) 2004, 2008, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.apt.mirror; 27 28 29 import com.sun.tools.apt.mirror.declaration.DeclarationMaker; 30 import com.sun.tools.apt.mirror.type.TypeMaker; 31 import com.sun.tools.javac.code.*; 32 import com.sun.tools.javac.code.Symbol.CompletionFailure; 33 import com.sun.tools.javac.comp.Attr; 34 import com.sun.tools.javac.comp.Enter; 35 import com.sun.tools.javac.util.Context; 36 import com.sun.tools.javac.util.Names; 37 38 39 /** 40 * The environment for a run of apt. 41 */ 42 @SuppressWarnings("deprecation") 43 public class AptEnv { 44 45 public Names names; // javac's name table 46 public Symtab symtab; // javac's predefined symbols 47 public Types jctypes; // javac's type utilities 48 public Enter enter; // javac's enter phase 49 public Attr attr; // javac's attr phase (to evaluate 50 // constant initializers) 51 public TypeMaker typeMaker; // apt's internal type utilities 52 public DeclarationMaker declMaker; // apt's internal declaration utilities 53 54 55 private static final Context.Key<AptEnv> aptEnvKey = 56 new Context.Key<AptEnv>(); 57 58 public static AptEnv instance(Context context) { 59 AptEnv instance = context.get(aptEnvKey); 60 if (instance == null) { 61 instance = new AptEnv(context); 62 } 63 return instance; 64 } 65 66 private AptEnv(Context context) { 67 context.put(aptEnvKey, this); 68 69 names = Names.instance(context); 70 symtab = Symtab.instance(context); 71 jctypes = Types.instance(context); 72 enter = Enter.instance(context); 73 attr = Attr.instance(context); 74 typeMaker = TypeMaker.instance(context); 75 declMaker = DeclarationMaker.instance(context); 76 } 77 78 79 /** 80 * Does a symbol have a given flag? Forces symbol completion. 81 */ 82 public static boolean hasFlag(Symbol sym, long flag) { 83 return (getFlags(sym) & flag) != 0; 84 } 85 86 /** 87 * Returns a symbol's flags. Forces completion. 88 */ 89 public static long getFlags(Symbol sym) { 90 complete(sym); 91 return sym.flags(); 92 } 93 94 /** 95 * Completes a symbol, ignoring completion failures. 96 */ 97 private static void complete(Symbol sym) { 98 while (true) { 99 try { 100 sym.complete(); 101 return; 102 } catch (CompletionFailure e) { 103 // Should never see two in a row, but loop just to be sure. 104 } 105 } 106 } 107 }