ApiInfo.java revision 11b179627c411772f1eac023d915ab8532e55211
1/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.doclava.apicheck;
18
19import com.google.doclava.ClassInfo;
20import com.google.doclava.Errors;
21import com.google.doclava.PackageInfo;
22import java.util.ArrayList;
23import java.util.HashMap;
24import java.util.Map;
25
26public class ApiInfo {
27
28  private HashMap<String, PackageInfo> mPackages
29      = new HashMap<String, PackageInfo>();
30  private HashMap<String, ClassInfo> mAllClasses
31      = new HashMap<String, ClassInfo>();
32  private Map<ClassInfo,String> mClassToSuper
33      = new HashMap<ClassInfo, String>();
34  private Map<ClassInfo, ArrayList<String>> mClassToInterface
35      = new HashMap<ClassInfo, ArrayList<String>>();
36
37
38  public ClassInfo findClass(String name) {
39    return mAllClasses.get(name);
40  }
41
42  protected void resolveInterfaces() {
43    for (ClassInfo cl : mAllClasses.values()) {
44      ArrayList<String> ifaces = mClassToInterface.get(cl);
45      if (ifaces == null) {
46        continue;
47      }
48      for (String iface : ifaces) {
49        cl.addInterface(mAllClasses.get(iface));
50      }
51    }
52  }
53
54  /**
55   * Checks to see if this api is consistent with a newer version.
56   */
57  public boolean isConsistent(ApiInfo otherApi) {
58    boolean consistent = true;
59    for (PackageInfo pInfo : mPackages.values()) {
60      if (otherApi.getPackages().containsKey(pInfo.name())) {
61        if (!pInfo.isConsistent(otherApi.getPackages().get(pInfo.name()))) {
62          consistent = false;
63        }
64      } else {
65        Errors.error(Errors.REMOVED_PACKAGE, pInfo.position(), "Removed package " + pInfo.name());
66        consistent = false;
67      }
68    }
69    for (PackageInfo pInfo : otherApi.mPackages.values()) {
70      if (!mPackages.containsKey(pInfo.name())) {
71        Errors.error(Errors.ADDED_PACKAGE, pInfo.position(), "Added package " + pInfo.name());
72        consistent = false;
73      }
74    }
75    return consistent;
76  }
77
78  public HashMap<String, PackageInfo> getPackages() {
79    return mPackages;
80  }
81
82  protected void mapClassToSuper(ClassInfo classInfo, String superclass) {
83    mClassToSuper.put(classInfo, superclass);
84  }
85
86  protected void mapClassToInterface(ClassInfo classInfo, String iface) {
87    if (!mClassToInterface.containsKey(classInfo)) {
88      mClassToInterface.put(classInfo, new ArrayList<String>());
89    }
90    mClassToInterface.get(classInfo).add(iface);
91  }
92
93  protected void addPackage(PackageInfo pInfo) {
94    // track the set of organized packages in the API
95    pInfo.setContainingApi(this);
96    mPackages.put(pInfo.name(), pInfo);
97
98    // accumulate a direct map of all the classes in the API
99    for (ClassInfo cl : pInfo.allClasses().values()) {
100      mAllClasses.put(cl.qualifiedName(), cl);
101    }
102  }
103
104  protected void resolveSuperclasses() {
105    for (ClassInfo cl : mAllClasses.values()) {
106      // java.lang.Object has no superclass
107      if (!cl.qualifiedName().equals("java.lang.Object")) {
108        String scName = mClassToSuper.get(cl);
109        if (scName == null) {
110          scName = "java.lang.Object";
111        }
112        ClassInfo superclass = mAllClasses.get(scName);
113        if (superclass == null) {
114          // Superclass not provided by this codebase. Inject a stub.
115          superclass = new ClassInfo(scName);
116        }
117        cl.setSuperClass(superclass);
118      }
119    }
120  }
121}
122