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 ConstructorInfo implements AbstractMethodInfo { 21 22 private String mName; 23 private String mType; 24 private boolean mIsStatic; 25 private boolean mIsFinal; 26 private String mDeprecated; 27 private String mScope; 28 private List<String> mExceptions; 29 private List<ParameterInfo> mParameters; 30 private boolean mExistsInBoth; 31 private SourcePositionInfo mSourcePosition; 32 private ClassInfo mClass; 33 ConstructorInfo(String name, String type, boolean isStatic, boolean isFinal, String deprecated, String scope, SourcePositionInfo pos, ClassInfo clazz)34 public ConstructorInfo(String name, String type, boolean isStatic, boolean isFinal, 35 String deprecated, String scope, SourcePositionInfo pos, ClassInfo clazz) { 36 mName = name; 37 mType = type; 38 mIsStatic = isStatic; 39 mIsFinal = isFinal; 40 mDeprecated= deprecated; 41 mScope = scope; 42 mExistsInBoth = false; 43 mExceptions = new ArrayList<String>(); 44 mParameters = new ArrayList<ParameterInfo>(); 45 mSourcePosition = pos; 46 mClass = clazz; 47 } 48 addParameter(ParameterInfo pInfo)49 public void addParameter(ParameterInfo pInfo) { 50 mParameters.add(pInfo); 51 } 52 addException(String exec)53 public void addException(String exec) { 54 mExceptions.add(exec); 55 } 56 getHashableName()57 public String getHashableName() { 58 StringBuilder result = new StringBuilder(); 59 result.append(name()); 60 for (ParameterInfo pInfo : mParameters) { 61 result.append(":").append(pInfo.getType()); 62 } 63 return result.toString(); 64 } 65 isInBoth()66 public boolean isInBoth() { 67 return mExistsInBoth; 68 } 69 position()70 public SourcePositionInfo position() { 71 return mSourcePosition; 72 } 73 name()74 public String name() { 75 return mName; 76 } 77 qualifiedName()78 public String qualifiedName() { 79 String baseName = (mClass != null) 80 ? (mClass.qualifiedName() + ".") 81 : ""; 82 return baseName + name(); 83 } 84 prettySignature()85 public String prettySignature() { 86 String params = ""; 87 for (ParameterInfo pInfo : mParameters) { 88 if (params.length() > 0) { 89 params += ", "; 90 } 91 params += pInfo.getType(); 92 } 93 return qualifiedName() + '(' + params + ')'; 94 } 95 isConsistent(ConstructorInfo mInfo)96 public boolean isConsistent(ConstructorInfo mInfo) { 97 mInfo.mExistsInBoth = true; 98 mExistsInBoth = true; 99 boolean consistent = true; 100 101 if (mIsFinal != mInfo.mIsFinal) { 102 consistent = false; 103 Errors.error(Errors.CHANGED_FINAL, mInfo.position(), 104 "Constructor " + mInfo.qualifiedName() + " has changed 'final' qualifier"); 105 } 106 107 if (mIsStatic != mInfo.mIsStatic) { 108 consistent = false; 109 Errors.error(Errors.CHANGED_FINAL, mInfo.position(), 110 "Constructor " + mInfo.qualifiedName() + " has changed 'static' qualifier"); 111 } 112 113 if (!mScope.equals(mInfo.mScope)) { 114 consistent = false; 115 Errors.error(Errors.CHANGED_SCOPE, mInfo.position(), 116 "Constructor " + mInfo.qualifiedName() + " changed scope from " 117 + mScope + " to " + mInfo.mScope); 118 } 119 120 if (!mDeprecated.equals(mInfo.mDeprecated)) { 121 consistent = false; 122 Errors.error(Errors.CHANGED_DEPRECATED, mInfo.position(), 123 "Constructor " + mInfo.qualifiedName() + " has changed deprecation state"); 124 } 125 126 for (String exec : mExceptions) { 127 if (!mInfo.mExceptions.contains(exec)) { 128 Errors.error(Errors.CHANGED_THROWS, mInfo.position(), 129 "Constructor " + mInfo.qualifiedName() + " no longer throws exception " 130 + exec); 131 consistent = false; 132 } 133 } 134 135 for (String exec : mInfo.mExceptions) { 136 if (!mExceptions.contains(exec)) { 137 Errors.error(Errors.CHANGED_THROWS, mInfo.position(), 138 "Constructor " + mInfo.qualifiedName() + " added thrown exception " 139 + exec); 140 consistent = false; 141 } 142 } 143 144 return consistent; 145 } 146 147 148 } 149