Home | History | Annotate | Download | only in apicheck
      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 
     17 package com.google.doclava.apicheck;
     18 
     19 import com.google.doclava.ClassInfo;
     20 import com.google.doclava.Errors;
     21 import com.google.doclava.PackageInfo;
     22 import java.util.ArrayList;
     23 import java.util.HashMap;
     24 import java.util.Map;
     25 
     26 public 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     mPackages.put(pInfo.name(), pInfo);
     96 
     97     // accumulate a direct map of all the classes in the API
     98     for (ClassInfo cl : pInfo.allClasses().values()) {
     99       mAllClasses.put(cl.qualifiedName(), cl);
    100     }
    101   }
    102 
    103   protected void resolveSuperclasses() {
    104     for (ClassInfo cl : mAllClasses.values()) {
    105       // java.lang.Object has no superclass
    106       if (!cl.qualifiedName().equals("java.lang.Object")) {
    107         String scName = mClassToSuper.get(cl);
    108         if (scName == null) {
    109           scName = "java.lang.Object";
    110         }
    111         ClassInfo superclass = mAllClasses.get(scName);
    112         if (superclass == null) {
    113           // Superclass not provided by this codebase. Inject a stub.
    114           superclass = new ClassInfo(scName);
    115         }
    116         cl.setSuperClass(superclass);
    117       }
    118     }
    119   }
    120 }
    121