001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.classfile;
042:
043: import java.util.ArrayList;
044:
045: /**
046: * A container for a group of classes/placeholders with the same name and different classloaders,
047: * plus the functionality to browse this group and check for compatible classes.
048: *
049: * @author Tomas Hurka
050: * @author Misha Dmitirev
051: */
052: public class SameNameClassGroup {
053: //~ Instance fields ----------------------------------------------------------------------------------------------------------
054:
055: private ArrayList classes;
056:
057: //~ Constructors -------------------------------------------------------------------------------------------------------------
058:
059: public SameNameClassGroup() {
060: classes = new ArrayList(4); // Hope we are not going to have too many class versions...
061: }
062:
063: //~ Methods ------------------------------------------------------------------------------------------------------------------
064:
065: public ArrayList getAll() {
066: return classes;
067: }
068:
069: public void add(BaseClassInfo clazz) {
070: classes.add(clazz);
071: }
072:
073: /**
074: * Check if class clazz with its existing loader is compatible with loader classLoaderId - that is,
075: * loader classLoaderId will return this class if asked for the class with this name.
076: */
077: public static BaseClassInfo checkForCompatibility(
078: BaseClassInfo clazz, int classLoaderId) {
079: int entryLoader = clazz.getLoaderId();
080:
081: if (entryLoader == classLoaderId) {
082: return clazz;
083: } else {
084: if (isParentLoaderTo(entryLoader, classLoaderId)) {
085: return clazz;
086: } else if (isParentLoaderTo(classLoaderId, entryLoader)) { // This can happen at least with placeholders
087: clazz.setLoaderId(classLoaderId);
088:
089: return clazz;
090: } else if (classLoaderId > 0) {
091: // In some cases, the class loader graph for the app may be a non-tree structure, i.e. one class loader may delegate
092: // not just to its parent loader, but to some other loader(s) as well. In that case, our last resort is to ask for
093: // its defining loader.
094: int loader = ClassRepository.getDefiningClassLoaderId(
095: clazz.getName(), classLoaderId);
096:
097: if (loader == -1) {
098: return null;
099: }
100:
101: if (loader == entryLoader) {
102: return clazz;
103: }
104: }
105:
106: return null;
107: }
108: }
109:
110: /** Find a class compatible with the given loader (see definition in checkFroCompatibility()) in this group. */
111: public BaseClassInfo findCompatibleClass(int classLoaderId) {
112: int size = classes.size();
113: for (int i = 0; i < size; i++) {
114: BaseClassInfo clazz = (BaseClassInfo) classes.get(i);
115: if (clazz.getLoaderId() == classLoaderId) {
116: return clazz;
117: }
118: }
119: for (int i = 0; i < size; i++) {
120: BaseClassInfo clazz = (BaseClassInfo) classes.get(i);
121: clazz = checkForCompatibility(clazz, classLoaderId);
122:
123: if (clazz != null) {
124: return clazz;
125: }
126: }
127:
128: return null;
129: }
130:
131: public void replace(BaseClassInfo clazz1, BaseClassInfo clazz2) {
132: classes.remove(clazz1);
133: classes.add(clazz2);
134: }
135:
136: private static boolean isParentLoaderTo(int testParentLoader,
137: int testChildLoader) {
138: int parent = ClassLoaderTable.getParentLoader(testChildLoader);
139:
140: while (parent != testParentLoader) {
141: if (parent == 0) {
142: return false;
143: } else {
144: parent = ClassLoaderTable.getParentLoader(parent);
145: }
146: }
147:
148: return true;
149: }
150: }
|