01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.harmony.lang;
18:
19: import java.util.ArrayList;
20:
21: /**
22: * @author Evgueni Brevnov, Roman S. Bushmanov
23: * @version $Revision: 1.1.6.4 $
24: */
25: public class ClassLoaderInfo {
26:
27: private static final ClassLoader[] systemLoaders;
28:
29: static {
30: ClassLoader systemClassLoader = ClassLoader
31: .getSystemClassLoader();
32: ClassLoader parentClassLoader = systemClassLoader.getParent();
33: if (parentClassLoader == null) {
34: systemLoaders = new ClassLoader[] { systemClassLoader };
35: } else {
36: ArrayList<ClassLoader> loaders = new ArrayList<ClassLoader>();
37: loaders.add(systemClassLoader);
38: do {
39: loaders.add(parentClassLoader);
40: parentClassLoader = parentClassLoader.getParent();
41: } while (parentClassLoader != null);
42: systemLoaders = (ClassLoader[]) loaders
43: .toArray(new ClassLoader[loaders.size()]);
44: }
45: }
46:
47: /**
48: * Answers if the specified class loader is the system class loader or on of
49: * its ancestors.
50: *
51: * @param classLoader class loader to test
52: * @return true if the specified class loader is the system class loader or
53: * on of its ancestors, false otherwise. This method returns true if
54: * null is passed.
55: */
56: public static boolean isSystemClassLoader(ClassLoader classLoader) {
57: if (classLoader == null) {
58: return true;
59: }
60: for (int i = 0; i < systemLoaders.length; i++) {
61: if (classLoader == systemLoaders[i]) {
62: return true;
63: }
64: }
65: return false;
66: }
67:
68: /**
69: * Answers if the specified class was defined by the system class loader or
70: * on of its ancestors. The behaviour is undefined if clazz is equal to null.
71: *
72: * @param clazz class which class loader should be tested
73: * @return true if the specified class was defined by the system class
74: * loader or on of its ancestors, false otherwise. This method
75: * returns true if defining class loader is null.
76: */
77: public static boolean hasSystemClassLoader(Class<?> clazz) {
78: for (int i = 0; i < systemLoaders.length; i++) {
79: ClassLoader loader = clazz.getClassLoader();
80: if (loader == null || loader == systemLoaders[i]) {
81: return true;
82: }
83: }
84: return false;
85:
86: }
87: }
|