NonDelegatingClassLoader.java
3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.launch;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashSet;
import java.util.Set;
/**
* ClassLoader which only allows whitelisted classes to be loaded, used to
* pre-load packet transformer classes to ensure that they don't reference any
* external classes.
*
* @author Adam Mummery-Smith
*/
public class NonDelegatingClassLoader extends URLClassLoader
{
/**
* Class names which we can load with this loader
*/
private final Set<String> validClassNames = new HashSet<String>();
/**
* Packages which we can load with this loader
*/
private final Set<String> validPackages = new HashSet<String>();
/**
* Class names which will be forcibly delegated to the parent ClassLoader
*/
private final Set<String> delegatedClassNames = new HashSet<String>();
/**
* Package names which will be forcibly delegated to the parent ClassLoader
*/
private final Set<String> delegatedPackages = new HashSet<String>();
private final ClassLoader parent;
private boolean valid = true;
private String invalidClassName = null;
NonDelegatingClassLoader(URL[] urls, ClassLoader parent)
{
super(urls, null);
this.parent = parent;
this.validClassNames.add("java.lang.Object");
this.validPackages.add("java.");
}
public boolean isValid()
{
return this.valid;
}
public String getInvalidClassName()
{
return this.invalidClassName;
}
public void reset()
{
this.valid = true;
this.invalidClassName = null;
}
public void addValidClassName(String className)
{
this.validClassNames.add(className);
}
public void addValidPackage(String packageName)
{
if (!packageName.endsWith(".")) packageName += ".";
this.validPackages.add(packageName);
}
public void addDelegatedClassName(String className)
{
this.delegatedClassNames.add(className);
this.validClassNames.add(className);
}
public void addDelegatedPackage(String packageName)
{
if (!packageName.endsWith(".")) packageName += ".";
this.delegatedPackages.add(packageName);
this.validPackages.add(packageName);
}
public Class<?> addAndLoadClass(String name) throws ClassNotFoundException
{
this.reset();
this.addValidClassName(name);
return this.loadClass(name);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException
{
if (this.parent != null)
{
if (this.delegatedClassNames.contains(name))
{
return this.parent.loadClass(name);
}
for (String delegatedPackage : this.delegatedPackages)
{
if (name.startsWith(delegatedPackage))
{
return this.parent.loadClass(name);
}
}
}
return super.loadClass(name);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException
{
if (name == null) return null;
if (this.validClassNames.contains(name))
{
return super.findClass(name);
}
for (String validPackage : this.validPackages)
{
if (name.startsWith(validPackage))
{
return super.findClass(name);
}
}
this.valid = false;
this.invalidClassName = name;
return super.findClass(name);
}
}