1 /* 2 * Copyright (C) 2008 The Android Open Source Project 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.android.apicheck; 18 import java.util.*; 19 20 public class ApiInfo { 21 22 private HashMap<String, PackageInfo> mPackages; 23 private HashMap<String, ClassInfo> mAllClasses; 24 ApiInfo()25 public ApiInfo() { 26 mPackages = new HashMap<String, PackageInfo>(); 27 mAllClasses = new HashMap<String, ClassInfo>(); 28 } 29 findClass(String name)30 public ClassInfo findClass(String name) { 31 return mAllClasses.get(name); 32 } 33 resolveInterfaces()34 private void resolveInterfaces() { 35 for (ClassInfo c : mAllClasses.values()) { 36 c.resolveInterfaces(this); 37 } 38 } 39 isConsistent(ApiInfo otherApi)40 public boolean isConsistent(ApiInfo otherApi) { 41 resolveInterfaces(); 42 boolean consistent = true; 43 for (PackageInfo pInfo : mPackages.values()) { 44 if (otherApi.getPackages().containsKey(pInfo.name())) { 45 if (!pInfo.isConsistent(otherApi.getPackages().get(pInfo.name()))) { 46 consistent = false; 47 } 48 } else { 49 Errors.error(Errors.REMOVED_PACKAGE, pInfo.position(), 50 "Removed package " + pInfo.name()); 51 consistent = false; 52 } 53 } 54 for (PackageInfo pInfo : otherApi.mPackages.values()) { 55 if (!pInfo.isInBoth()) { 56 Errors.error(Errors.ADDED_PACKAGE, pInfo.position(), 57 "Added package " + pInfo.name()); 58 consistent = false; 59 } 60 } 61 return consistent; 62 } 63 getPackages()64 public HashMap<String, PackageInfo> getPackages() { 65 return mPackages; 66 } 67 addPackage(PackageInfo pInfo)68 public void addPackage(PackageInfo pInfo) { 69 // track the set of organized packages in the API 70 mPackages.put(pInfo.name(), pInfo); 71 72 // accumulate a direct map of all the classes in the API 73 for (ClassInfo cl: pInfo.allClasses().values()) { 74 mAllClasses.put(cl.qualifiedName(), cl); 75 } 76 } 77 resolveSuperclasses()78 public void resolveSuperclasses() { 79 for (ClassInfo cl: mAllClasses.values()) { 80 // java.lang.Object has no superclass 81 if (!cl.qualifiedName().equals("java.lang.Object")) { 82 String scName = cl.superclassName(); 83 if (scName == null) { 84 scName = "java.lang.Object"; 85 } 86 87 ClassInfo superclass = mAllClasses.get(scName); 88 cl.setSuperClass(superclass); 89 } 90 } 91 } 92 } 93