Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/java/MethodSet.java
38918 views
/*1* Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.java;2627import java.util.*;2829/**30* The MethodSet structure is used to store methods for a class.31* It maintains the invariant that it never stores two methods32* with the same signature. MethodSets are able to lookup33* all methods with a given name and the unique method with a given34* signature (name, args).35*36* WARNING: The contents of this source file are not part of any37* supported API. Code that depends on them does so at its own risk:38* they are subject to change or removal without notice.39*/4041public42class MethodSet {4344/**45* A Map containing Lists of MemberDefinitions. The Lists46* contain methods which share the same name.47*/48private final Map lookupMap;4950/**51* The number of methods stored in the MethodSet.52*/53private int count;5455/**56* Is this MethodSet currently frozen? See freeze() for more details.57*/58private boolean frozen;5960/**61* Creates a brand new MethodSet62*/63public MethodSet() {64frozen = false;65lookupMap = new HashMap();66count = 0;67}6869/**70* Returns the number of distinct methods stored in the MethodSet.71*/72public int size() {73return count;74}7576/**77* Adds `method' to the MethodSet. No method of the same signature78* should be already defined.79*/80public void add(MemberDefinition method) {81// Check for late additions.82if (frozen) {83throw new CompilerError("add()");84}8586// todo: Check for method??8788Identifier name = method.getName();8990// Get a List containing all methods of this name.91List methodList = (List) lookupMap.get(name);9293if (methodList == null) {94// There is no method with this name already.95// Create a List, and insert it into the hash.96methodList = new ArrayList();97lookupMap.put(name, methodList);98}99100// Make sure that no method with the same signature has already101// been added to the MethodSet.102int size = methodList.size();103for (int i = 0; i < size; i++) {104if (((MemberDefinition) methodList.get(i))105.getType().equalArguments(method.getType())) {106throw new CompilerError("duplicate addition");107}108}109110// We add the method to the appropriate list.111methodList.add(method);112count++;113}114115/**116* Adds `method' to the MethodSet, replacing any previous definition117* with the same signature.118*/119public void replace(MemberDefinition method) {120// Check for late additions.121if (frozen) {122throw new CompilerError("replace()");123}124125// todo: Check for method??126127Identifier name = method.getName();128129// Get a List containing all methods of this name.130List methodList = (List) lookupMap.get(name);131132if (methodList == null) {133// There is no method with this name already.134// Create a List, and insert it into the hash.135methodList = new ArrayList();136lookupMap.put(name, methodList);137}138139// Replace the element which has the same signature as140// `method'.141int size = methodList.size();142for (int i = 0; i < size; i++) {143if (((MemberDefinition) methodList.get(i))144.getType().equalArguments(method.getType())) {145methodList.set(i, method);146return;147}148}149150// We add the method to the appropriate list.151methodList.add(method);152count++;153}154155/**156* If the MethodSet contains a method with the same signature157* then lookup() returns it. Otherwise, this method returns null.158*/159public MemberDefinition lookupSig(Identifier name, Type type) {160// Go through all methods of the same name and see if any161// have the right signature.162Iterator matches = lookupName(name);163MemberDefinition candidate;164165while (matches.hasNext()) {166candidate = (MemberDefinition) matches.next();167if (candidate.getType().equalArguments(type)) {168return candidate;169}170}171172// No match.173return null;174}175176/**177* Returns an Iterator of all methods contained in the178* MethodSet which have a given name.179*/180public Iterator lookupName(Identifier name) {181// Find the List containing all methods of this name, and182// return that List's Iterator.183List methodList = (List) lookupMap.get(name);184if (methodList == null) {185// If there is no method of this name, return a bogus, empty186// Iterator.187return Collections.emptyIterator();188}189return methodList.iterator();190}191192/**193* Returns an Iterator of all methods in the MethodSet194*/195public Iterator iterator() {196197//----------------------------------------------------------198// The inner class MethodIterator is used to create our199// Iterator of all methods in the MethodSet.200class MethodIterator implements Iterator {201Iterator hashIter = lookupMap.values().iterator();202Iterator listIter = Collections.emptyIterator();203204public boolean hasNext() {205if (listIter.hasNext()) {206return true;207} else {208if (hashIter.hasNext()) {209listIter = ((List) hashIter.next())210.iterator();211212// The following should be always true.213if (listIter.hasNext()) {214return true;215} else {216throw new217CompilerError("iterator() in MethodSet");218}219}220}221222// We've run out of Lists.223return false;224}225226public Object next() {227return listIter.next();228}229230public void remove() {231throw new UnsupportedOperationException();232}233}234// end MethodIterator235//----------------------------------------------------------236237// A one-liner.238return new MethodIterator();239}240241/**242* After freeze() is called, the MethodSet becomes (mostly)243* immutable. Any calls to add() or addMeet() lead to244* CompilerErrors. Note that the entries themselves are still245* (unfortunately) open for mischievous and wanton modification.246*/247public void freeze() {248frozen = true;249}250251/**252* Tells whether freeze() has been called on this MethodSet.253*/254public boolean isFrozen() {255return frozen;256}257258/**259* Returns a (big) string representation of this MethodSet260*/261public String toString() {262int len = size();263StringBuffer buf = new StringBuffer();264Iterator all = iterator();265buf.append("{");266267while (all.hasNext()) {268buf.append(all.next().toString());269len--;270if (len > 0) {271buf.append(", ");272}273}274buf.append("}");275return buf.toString();276}277}278279280