0N/A/*
797N/A * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.apt.mirror.declaration;
0N/A
0N/A
0N/Aimport java.lang.annotation.Annotation;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.LinkedList;
0N/Aimport java.util.Collection;
0N/Aimport java.util.EnumSet;
0N/Aimport javax.tools.JavaFileObject;
0N/A
0N/Aimport com.sun.mirror.declaration.*;
0N/Aimport com.sun.mirror.util.*;
0N/Aimport com.sun.tools.apt.mirror.AptEnv;
0N/Aimport com.sun.tools.apt.mirror.util.SourcePositionImpl;
0N/Aimport com.sun.tools.javac.code.*;
0N/Aimport com.sun.tools.javac.code.Symbol.*;
0N/Aimport com.sun.tools.javac.comp.AttrContext;
0N/Aimport com.sun.tools.javac.comp.Env;
0N/Aimport com.sun.tools.javac.tree.*;
0N/Aimport com.sun.tools.javac.util.Name;
0N/Aimport com.sun.tools.javac.util.Position;
0N/A
0N/Aimport static com.sun.mirror.declaration.Modifier.*;
0N/Aimport static com.sun.tools.javac.code.Kinds.*;
0N/A
0N/A
0N/A/**
0N/A * Implementation of Declaration
0N/A */
330N/A@SuppressWarnings("deprecation")
0N/Apublic abstract class DeclarationImpl implements Declaration {
0N/A
0N/A protected final AptEnv env;
0N/A public final Symbol sym;
0N/A
505N/A protected static final DeclarationFilter identityFilter =
0N/A new DeclarationFilter();
0N/A
0N/A
0N/A /**
0N/A * "sym" should be completed before this constructor is called.
0N/A */
0N/A protected DeclarationImpl(AptEnv env, Symbol sym) {
0N/A this.env = env;
0N/A this.sym = sym;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * <p> ParameterDeclarationImpl overrides this implementation.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj instanceof DeclarationImpl) {
0N/A DeclarationImpl that = (DeclarationImpl) obj;
0N/A return sym == that.sym && env == that.env;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * <p> ParameterDeclarationImpl overrides this implementation.
0N/A */
0N/A public int hashCode() {
0N/A return sym.hashCode() + env.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public String getDocComment() {
0N/A // Our doc comment is contained in a map in our toplevel,
0N/A // indexed by our tree. Find our enter environment, which gives
0N/A // us our toplevel. It also gives us a tree that contains our
0N/A // tree: walk it to find our tree. This is painful.
0N/A Env<AttrContext> enterEnv = getEnterEnv();
0N/A if (enterEnv == null)
0N/A return null;
0N/A JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
0N/A return enterEnv.toplevel.docComments.get(tree);
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public Collection<AnnotationMirror> getAnnotationMirrors() {
0N/A Collection<AnnotationMirror> res =
0N/A new ArrayList<AnnotationMirror>();
0N/A for (Attribute.Compound a : sym.getAnnotationMirrors()) {
0N/A res.add(env.declMaker.getAnnotationMirror(a, this));
0N/A }
0N/A return res;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * Overridden by ClassDeclarationImpl to handle @Inherited.
0N/A */
0N/A public <A extends Annotation> A getAnnotation(Class<A> annoType) {
0N/A return getAnnotation(annoType, sym);
0N/A }
0N/A
0N/A protected <A extends Annotation> A getAnnotation(Class<A> annoType,
0N/A Symbol annotated) {
0N/A if (!annoType.isAnnotation()) {
0N/A throw new IllegalArgumentException(
0N/A "Not an annotation type: " + annoType);
0N/A }
0N/A String name = annoType.getName();
0N/A for (Attribute.Compound attr : annotated.getAnnotationMirrors()) {
0N/A if (name.equals(attr.type.tsym.flatName().toString())) {
0N/A return AnnotationProxyMaker.generateAnnotation(env, attr,
0N/A annoType);
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A // Cache for modifiers.
0N/A private EnumSet<Modifier> modifiers = null;
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public Collection<Modifier> getModifiers() {
0N/A if (modifiers == null) {
0N/A modifiers = EnumSet.noneOf(Modifier.class);
0N/A long flags = AptEnv.getFlags(sym);
0N/A
0N/A if (0 != (flags & Flags.PUBLIC)) modifiers.add(PUBLIC);
0N/A if (0 != (flags & Flags.PROTECTED)) modifiers.add(PROTECTED);
0N/A if (0 != (flags & Flags.PRIVATE)) modifiers.add(PRIVATE);
0N/A if (0 != (flags & Flags.ABSTRACT)) modifiers.add(ABSTRACT);
0N/A if (0 != (flags & Flags.STATIC)) modifiers.add(STATIC);
0N/A if (0 != (flags & Flags.FINAL)) modifiers.add(FINAL);
0N/A if (0 != (flags & Flags.TRANSIENT)) modifiers.add(TRANSIENT);
0N/A if (0 != (flags & Flags.VOLATILE)) modifiers.add(VOLATILE);
0N/A if (0 != (flags & Flags.SYNCHRONIZED)) modifiers.add(SYNCHRONIZED);
0N/A if (0 != (flags & Flags.NATIVE)) modifiers.add(NATIVE);
0N/A if (0 != (flags & Flags.STRICTFP)) modifiers.add(STRICTFP);
0N/A }
0N/A return modifiers;
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A * Overridden in some subclasses.
0N/A */
0N/A public String getSimpleName() {
0N/A return sym.name.toString();
0N/A }
0N/A
0N/A /**
0N/A * {@inheritDoc}
0N/A */
0N/A public SourcePosition getPosition() {
0N/A // Find the toplevel. From there use a tree-walking utility
0N/A // that finds the tree for our symbol, and with it the position.
0N/A Env<AttrContext> enterEnv = getEnterEnv();
0N/A if (enterEnv == null)
0N/A return null;
0N/A JCTree.JCCompilationUnit toplevel = enterEnv.toplevel;
0N/A JavaFileObject sourcefile = toplevel.sourcefile;
0N/A if (sourcefile == null)
0N/A return null;
0N/A int pos = TreeInfo.positionFor(sym, toplevel);
0N/A
0N/A return new SourcePositionImpl(sourcefile, pos, toplevel.lineMap);
0N/A }
0N/A
0N/A /**
0N/A * Applies a visitor to this declaration.
0N/A *
0N/A * @param v the visitor operating on this declaration
0N/A */
0N/A public void accept(DeclarationVisitor v) {
0N/A v.visitDeclaration(this);
0N/A }
0N/A
0N/A
0N/A private Collection<Symbol> members = null; // cache for getMembers()
0N/A
0N/A /**
0N/A * Returns the symbols of type or package members (and constructors)
0N/A * that are not synthetic or otherwise unwanted.
0N/A * Caches the result if "cache" is true.
0N/A */
0N/A protected Collection<Symbol> getMembers(boolean cache) {
0N/A if (members != null) {
0N/A return members;
0N/A }
0N/A LinkedList<Symbol> res = new LinkedList<Symbol>();
0N/A for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {
0N/A if (e.sym != null && !unwanted(e.sym)) {
0N/A res.addFirst(e.sym);
0N/A }
0N/A }
0N/A return cache ? (members = res) : res;
0N/A }
0N/A
0N/A /**
0N/A * Tests whether this is a symbol that should never be seen by clients,
0N/A * such as a synthetic class.
0N/A * Note that a class synthesized by the compiler may not be flagged as
0N/A * synthetic: see bugid 4959932.
0N/A */
0N/A private static boolean unwanted(Symbol s) {
0N/A return AptEnv.hasFlag(s, Flags.SYNTHETIC) ||
0N/A (s.kind == TYP &&
0N/A !DeclarationMaker.isJavaIdentifier(s.name.toString()));
0N/A }
0N/A
0N/A /**
0N/A * Returns this declaration's enter environment, or null if it
0N/A * has none.
0N/A */
0N/A private Env<AttrContext> getEnterEnv() {
0N/A // Get enclosing class of sym, or sym itself if it is a class
0N/A // or package.
0N/A TypeSymbol ts = (sym.kind != PCK)
0N/A ? sym.enclClass()
0N/A : (PackageSymbol) sym;
0N/A return (ts != null)
0N/A ? env.enter.getEnv(ts)
0N/A : null;
0N/A }
0N/A}