KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > model > PackageElementImpl


1 /*******************************************************************************
2  * Copyright (c) 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * wharley@bea.com - initial API and implementation
10  *
11  *******************************************************************************/

12
13 package org.eclipse.jdt.internal.compiler.apt.model;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.lang.model.element.Element;
21 import javax.lang.model.element.ElementKind;
22 import javax.lang.model.element.ElementVisitor;
23 import javax.lang.model.element.Name;
24 import javax.lang.model.element.PackageElement;
25
26 import org.eclipse.jdt.core.compiler.CharOperation;
27 import org.eclipse.jdt.internal.compiler.apt.dispatch.BaseProcessingEnvImpl;
28 import org.eclipse.jdt.internal.compiler.batch.FileSystem;
29 import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
30 import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
31 import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment;
32 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
33 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
34 import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
35
36 /**
37  * Implementation of PackageElement, which represents a package
38  */

39 public class PackageElementImpl extends ElementImpl implements PackageElement {
40
41     PackageElementImpl(BaseProcessingEnvImpl env, PackageBinding binding) {
42         super(env, binding);
43     }
44     
45     @Override JavaDoc
46     public <R, P> R accept(ElementVisitor<R, P> v, P p)
47     {
48         return v.visitPackage(this, p);
49     }
50
51     @Override JavaDoc
52     protected AnnotationBinding[] getAnnotationBindings()
53     {
54         PackageBinding packageBinding = (PackageBinding) this._binding;
55         char[][] compoundName = CharOperation.arrayConcat(packageBinding.compoundName, TypeConstants.PACKAGE_INFO_NAME);
56         ReferenceBinding type = this._env.getLookupEnvironment().getType(compoundName);
57         AnnotationBinding[] annotations = null;
58         if (type != null && type.isValidBinding()) {
59             annotations = type.getAnnotations();
60         }
61         return annotations;
62     }
63
64     @Override JavaDoc
65     public List JavaDoc<? extends Element> getEnclosedElements() {
66         PackageBinding binding = (PackageBinding)_binding;
67         LookupEnvironment environment = binding.environment;
68         char[][][] typeNames = null;
69         INameEnvironment nameEnvironment = binding.environment.nameEnvironment;
70         if (nameEnvironment instanceof FileSystem) {
71             typeNames = ((FileSystem) nameEnvironment).findTypeNames(binding.compoundName);
72         }
73         HashSet JavaDoc<Element> set = new HashSet JavaDoc<Element>();
74         if (typeNames != null) {
75             for (char[][] typeName : typeNames) {
76                 ReferenceBinding type = environment.getType(typeName);
77                 if (type != null && type.isValidBinding()) {
78                     set.add(_env.getFactory().newElement(type));
79                 }
80             }
81         }
82         ArrayList JavaDoc<Element> list = new ArrayList JavaDoc<Element>(set.size());
83         list.addAll(set);
84         return Collections.unmodifiableList(list);
85     }
86
87     @Override JavaDoc
88     public Element getEnclosingElement() {
89         // packages have no enclosing element
90
return null;
91     }
92
93     @Override JavaDoc
94     public ElementKind getKind() {
95         return ElementKind.PACKAGE;
96     }
97
98     @Override JavaDoc
99     PackageElement getPackage()
100     {
101         return this;
102     }
103
104     @Override JavaDoc
105     public Name getSimpleName() {
106         char[][] compoundName = ((PackageBinding)_binding).compoundName;
107         int length = compoundName.length;
108         if (length == 0) {
109             return new NameImpl(CharOperation.NO_CHAR);
110         }
111         return new NameImpl(compoundName[length - 1]);
112     }
113
114     @Override JavaDoc
115     public Name getQualifiedName() {
116         return new NameImpl(CharOperation.concatWith(((PackageBinding)_binding).compoundName, '.'));
117     }
118
119     @Override JavaDoc
120     public boolean isUnnamed() {
121         PackageBinding binding = (PackageBinding)_binding;
122         return binding.compoundName == CharOperation.NO_CHAR_CHAR;
123     }
124
125 }
126
Popular Tags