001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.core.ext.typeinfo;
017:
018: import java.lang.annotation.Annotation;
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: /**
023: * Represents a logical package.
024: */
025: public class JPackage implements HasAnnotations {
026:
027: private final String name;
028:
029: private final Annotations annotations = new Annotations();
030:
031: private final Map<String, JClassType> types = new HashMap<String, JClassType>();
032:
033: JPackage(String name) {
034: this .name = name;
035: }
036:
037: public void addAnnotations(
038: Map<Class<? extends Annotation>, Annotation> annotations) {
039: this .annotations.addAnnotations(annotations);
040: }
041:
042: public JClassType findType(String typeName) {
043: String[] parts = typeName.split("\\.");
044: return findType(parts);
045: }
046:
047: public JClassType findType(String[] typeName) {
048: return findTypeImpl(typeName, 0);
049: }
050:
051: public <T extends Annotation> T getAnnotation(
052: Class<T> annotationClass) {
053: return annotations.getAnnotation(annotationClass);
054: }
055:
056: public Annotation[] getAnnotations() {
057: return annotations.getAnnotations();
058: }
059:
060: public Annotation[] getDeclaredAnnotations() {
061: return annotations.getDeclaredAnnotations();
062: }
063:
064: public String getName() {
065: return name;
066: }
067:
068: public JClassType getType(String typeName) throws NotFoundException {
069: JClassType result = findType(typeName);
070: if (result == null) {
071: throw new NotFoundException();
072: }
073: return result;
074: }
075:
076: public JClassType[] getTypes() {
077: return types.values().toArray(TypeOracle.NO_JCLASSES);
078: }
079:
080: public boolean isAnnotationPresent(
081: Class<? extends Annotation> annotationClass) {
082: return annotations.isAnnotationPresent(annotationClass);
083: }
084:
085: public boolean isDefault() {
086: return "".equals(name);
087: }
088:
089: @Override
090: public String toString() {
091: return "package " + name;
092: }
093:
094: void addType(JClassType type) {
095: types.put(type.getSimpleSourceName(), type);
096: }
097:
098: JClassType findTypeImpl(String[] typeName, int index) {
099: JClassType found = types.get(typeName[index]);
100: if (found == null) {
101: return null;
102: } else if (index < typeName.length - 1) {
103: return found.findNestedTypeImpl(typeName, index + 1);
104: } else {
105: return found;
106: }
107: }
108:
109: void remove(JClassType type) {
110: types.remove(type.getSimpleSourceName());
111: // JDT will occasionally remove non-existent items, such as packages.
112: }
113: }
|