1 /* 2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package metaspace.stressHierarchy.common.classloader.tree; 24 25 import java.util.HashSet; 26 import java.util.Set; 27 28 import metaspace.stressHierarchy.common.classloader.StressClassloader; 29 30 /** 31 * Node of tree 32 * 33 */ 34 public class Node { 35 36 private int level; 37 38 private Set<String> loadedClassesNames; 39 40 private int number; 41 42 private Set<Object> objects = new HashSet<Object>(); 43 44 private Node parent = null; 45 46 private StressClassloader classloader; 47 48 public Node(int level, int number) { 49 this.level = level; 50 this.number = number; 51 } 52 53 public void cleanup() { 54 loadedClassesNames = getClassLoader().getLoadedClassNames(); 55 objects.clear(); 56 getClassLoader().getLoadedClasses().clear(); 57 classloader = null; 58 } 59 60 @Override 61 public boolean equals(Object obj) { 62 if (obj instanceof Node) { 63 Node other = (Node) obj; 64 return other.getLevel() == level && other.getNumber() == number; 65 } else { 66 return false; 67 } 68 } 69 70 StressClassloader getClassLoader() { //package access level 71 return classloader; 72 } 73 74 public int getLevel() { 75 return level; 76 } 77 78 public Set<Class<?>> getLoadedClasses() { 79 return getClassLoader() == null ? null : getClassLoader().getLoadedClasses(); 80 } 81 82 public Set<String> getLoadedClassesNames() { 83 if (loadedClassesNames != null) { 84 return loadedClassesNames; 85 } else if (getClassLoader() != null) { 86 return getClassLoader().getLoadedClassNames(); 87 } 88 return null; 89 } 90 91 public int getNumber() { 92 return number; 93 } 94 95 public Node getParent() { 96 return parent; 97 } 98 99 @Override 100 public int hashCode() { 101 return level + number * 999; 102 } 103 104 public void instantiateObjects() throws InstantiationException, IllegalAccessException { 105 for (Class<?> c : getClassLoader().getLoadedClasses()) { 106 if (!c.isInterface()) { 107 objects.add(c.newInstance()); 108 } 109 } 110 } 111 112 public boolean isRoot() { 113 return (parent == null); 114 } 115 116 public void setClassLoader(StressClassloader classLoader) { 117 classloader = classLoader; 118 } 119 120 public void setParent(Node parent) { 121 this.parent = parent; 122 } 123 124 @Override 125 public String toString() { 126 return "Node@level=" + level + "_num=" + number; 127 } 128 129 public void loadClasses() throws ClassNotFoundException { 130 String className = "package_level" + getLevel() + "_num" + getNumber() + ".Dummy"; 131 getClassLoader().loadClass(className); 132 } 133 134 }