summaryrefslogtreecommitdiffstats
path: root/src/com/google/doclava/apicheck/ApiInfo.java
blob: 2752f3a9ab1a693d3a0b5a8f24c9930ed8733717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (C) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.doclava.apicheck;

import com.google.doclava.ClassInfo;
import com.google.doclava.Errors;
import com.google.doclava.PackageInfo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class ApiInfo {

  private HashMap<String, PackageInfo> mPackages
      = new HashMap<String, PackageInfo>();
  private HashMap<String, ClassInfo> mAllClasses
      = new HashMap<String, ClassInfo>();
  private Map<ClassInfo,String> mClassToSuper
      = new HashMap<ClassInfo, String>();
  private Map<ClassInfo, ArrayList<String>> mClassToInterface
      = new HashMap<ClassInfo, ArrayList<String>>();


  public ClassInfo findClass(String name) {
    return mAllClasses.get(name);
  }

  protected void resolveInterfaces() {
    for (ClassInfo cl : mAllClasses.values()) {
      ArrayList<String> ifaces = mClassToInterface.get(cl);
      if (ifaces == null) {
        continue;
      }
      for (String iface : ifaces) {
        ClassInfo ci = mAllClasses.get(iface);
        if (ci == null) {
          // Interface not provided by this codebase. Inject a stub.
          ci = new ClassInfo(iface);
        }
        cl.addInterface(ci);
      }
    }
  }

  /**
   * Checks to see if this api is consistent with a newer version.
   *
   * @param otherApi the other api to test consistency against
   * @param ignoredPackages packages to skip consistency checks (will match by exact name)
   * @param ignoredClasses classes to skip consistency checks (will match by exact fully qualified
   * name)
   */
  public boolean isConsistent(ApiInfo otherApi,
      Collection<String> ignoredPackages, Collection<String> ignoredClasses) {
    boolean consistent = true;
    for (PackageInfo pInfo : mPackages.values()) {
      // TODO: Add support for matching subpackages (e.g, something like
      // test.example.* should match test.example.subpackage, and
      // test.example.** should match the above AND test.example.subpackage.more)
      if (ignoredPackages != null && ignoredPackages.contains(pInfo.name())) {
          // TODO: Log skipping this?
          continue;
      }
      if (otherApi.getPackages().containsKey(pInfo.name())) {
        if (!pInfo.isConsistent(otherApi.getPackages().get(pInfo.name()), ignoredClasses)) {
          consistent = false;
        }
      } else {
        Errors.error(Errors.REMOVED_PACKAGE, pInfo.position(), "Removed package " + pInfo.name());
        consistent = false;
      }
    }
    for (PackageInfo pInfo : otherApi.mPackages.values()) {
      if (ignoredPackages != null && ignoredPackages.contains(pInfo.name())) {
          // TODO: Log skipping this?
          continue;
      }
      if (!mPackages.containsKey(pInfo.name())) {
        Errors.error(Errors.ADDED_PACKAGE, pInfo.position(), "Added package " + pInfo.name());
        consistent = false;
      }
    }
    return consistent;
  }

  /**
   * Checks to see if this api is consistent with a newer version.
   */
  public boolean isConsistent(ApiInfo otherApi) {
    return isConsistent(otherApi, null, null);
  }

  public HashMap<String, PackageInfo> getPackages() {
    return mPackages;
  }

  protected void mapClassToSuper(ClassInfo classInfo, String superclass) {
    mClassToSuper.put(classInfo, superclass);
  }

  protected void mapClassToInterface(ClassInfo classInfo, String iface) {
    if (!mClassToInterface.containsKey(classInfo)) {
      mClassToInterface.put(classInfo, new ArrayList<String>());
    }
    mClassToInterface.get(classInfo).add(iface);
  }

  protected void addPackage(PackageInfo pInfo) {
    // track the set of organized packages in the API
    pInfo.setContainingApi(this);
    mPackages.put(pInfo.name(), pInfo);

    // accumulate a direct map of all the classes in the API
    for (ClassInfo cl : pInfo.allClasses().values()) {
      mAllClasses.put(cl.qualifiedName(), cl);
    }
  }

  protected void resolveSuperclasses() {
    for (ClassInfo cl : mAllClasses.values()) {
      // java.lang.Object has no superclass
      if (!cl.qualifiedName().equals("java.lang.Object")) {
        String scName = mClassToSuper.get(cl);
        if (scName == null) {
          scName = "java.lang.Object";
        }
        ClassInfo superclass = mAllClasses.get(scName);
        if (superclass == null) {
          // Superclass not provided by this codebase. Inject a stub.
          superclass = new ClassInfo(scName);
        }
        cl.setSuperClass(superclass);
      }
    }
  }
}