Skip to content

Commit b19f044

Browse files
committed
Revert last 2 commits - reverted to original DynamicClassLoader
1 parent f49beea commit b19f044

2 files changed

Lines changed: 31 additions & 36 deletions

File tree

src/main/java/org/hyperskill/hstest/dynamic/DynamicClassLoader.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,23 @@ public DynamicClassLoader(Class<?> clazz) {
2424
}
2525

2626
protected synchronized Class<?> loadClass(String name, boolean resolve)
27-
throws ClassNotFoundException {
28-
Class<?> result = findClass(name);
27+
throws ClassNotFoundException {
28+
29+
Class<?> c = findLoadedClass(name);
30+
if (c == null) {
31+
try {
32+
if (getParent() != null) {
33+
c = getParent().loadClass(name);
34+
}
35+
} catch (ClassNotFoundException e) {
36+
c = findClass(name);
37+
}
38+
}
39+
2940
if (resolve) {
30-
resolveClass(result);
41+
resolveClass(c);
3142
}
32-
return result;
43+
return c;
3344
}
3445

3546
protected Class<?> findClass(String name) throws ClassNotFoundException {
@@ -45,7 +56,7 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
4556
if (classBytes == null) {
4657
File f = findFile(name);
4758
if (f == null) {
48-
return findSystemClass(name);
59+
throw new ClassNotFoundException(name);
4960
}
5061

5162
classBytes = loadFileAsBytes(f);
@@ -55,10 +66,10 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
5566
result = defineClass(name, classBytes, 0, classBytes.length);
5667
} catch (IOException e) {
5768
throw new ClassNotFoundException(
58-
"Cannot load class " + name + ": " + e);
69+
"Cannot load class " + name + ": " + e);
5970
} catch (ClassFormatError e) {
6071
throw new ClassNotFoundException(
61-
"Format of class file incorrect for class " + name + " : " + e);
72+
"Format of class file incorrect for class " + name + " : " + e);
6273
}
6374

6475
savedClasses.put(name, result);
@@ -67,10 +78,10 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
6778

6879
private File findFile(String name) {
6980
File f = new File(
70-
searchLocation
71-
+ File.separator
72-
+ name.replace(".", File.separator)
73-
+ ".class"
81+
searchLocation
82+
+ File.separator
83+
+ name.replace(".", File.separator)
84+
+ ".class"
7485
);
7586

7687
if (f.exists()) {

src/main/java/org/hyperskill/hstest/testing/execution/MainMethodExecutor.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,13 @@ private void initByPackageName(String packageName) {
7979
}
8080

8181
private void initByClassName(String className) {
82-
this.className = className;
83-
84-
if (!useSeparateClassLoader) {
85-
try {
86-
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
87-
initByClassInstance(clazz);
88-
} catch (ClassNotFoundException | NoClassDefFoundError ex) {
89-
throw new ErrorWithFeedback(
90-
"Cannot find either a package or a class named \"" + className + "\". " +
91-
"Check if you've created one of these.");
92-
}
82+
try {
83+
Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
84+
initByClassInstance(clazz);
85+
} catch (ClassNotFoundException | NoClassDefFoundError ex) {
86+
throw new ErrorWithFeedback(
87+
"Cannot find either a package or a class named \"" + className + "\". " +
88+
"Check if you've created one of these.");
9389
}
9490
}
9591

@@ -138,27 +134,15 @@ private void initMethod() {
138134

139135
try {
140136
if (useSeparateClassLoader) {
141-
if (clazz == null) {
142-
clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
143-
}
144-
145137
ClassLoader cl = new DynamicClassLoader(clazz);
146138

147-
if (runClass == null) {
148-
runClass = cl.loadClass(className);
149-
} else {
150-
className = clazz.getName();
151-
runClass = cl.loadClass(className);
152-
}
153-
139+
className = clazz.getName();
140+
runClass = cl.loadClass(className);
154141
methodToInvoke = getMainMethod(runClass);
155142
group = new ThreadGroup(runClass.getSimpleName());
156143
group.setDaemon(true);
157144

158145
} else {
159-
if (runClass == null) {
160-
throw new UnexpectedError("runClass is null in initMethod");
161-
}
162146
className = clazz.getName();
163147
methodToInvoke = getMainMethod(runClass);
164148
group = new ThreadGroup(runClass.getSimpleName());

0 commit comments

Comments
 (0)