1
- /*
1
+ /**
2
2
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
3
3
* Copyright (C) 2012 Kristian S. Stangeland
4
4
*
5
- * This program is free software; you can redistribute it and/or modify it under the terms of the
6
- * GNU General Public License as published by the Free Software Foundation; either version 2 of
5
+ * This program is free software; you can redistribute it and/or modify it under the terms of the
6
+ * GNU General Public License as published by the Free Software Foundation; either version 2 of
7
7
* the License, or (at your option) any later version.
8
8
*
9
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
11
* See the GNU General Public License for more details.
12
12
*
13
- * You should have received a copy of the GNU General Public License along with this program;
14
- * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
13
+ * You should have received a copy of the GNU General Public License along with this program;
14
+ * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
15
15
* 02111-1307 USA
16
16
*/
17
-
18
17
package com .comphenix .protocol .utility ;
19
18
20
19
import java .util .Map ;
@@ -33,7 +32,7 @@ class CachedPackage {
33
32
private final Map <String , Optional <Class <?>>> cache ;
34
33
private final String packageName ;
35
34
private final ClassSource source ;
36
-
35
+
37
36
/**
38
37
* Construct a new cached package.
39
38
* @param packageName - the name of the current package.
@@ -44,45 +43,51 @@ public CachedPackage(String packageName, ClassSource source) {
44
43
this .cache = Maps .newConcurrentMap ();
45
44
this .source = source ;
46
45
}
47
-
46
+
48
47
/**
49
48
* Associate a given class with a class name.
50
49
* @param className - class name.
51
50
* @param clazz - type of class.
52
51
*/
53
52
public void setPackageClass (String className , Class <?> clazz ) {
54
- cache .put (className , Optional .<Class <?>>fromNullable (clazz ));
53
+ if (clazz != null ) {
54
+ cache .put (className , Optional .<Class <?>> of (clazz ));
55
+ } else {
56
+ cache .remove (className );
57
+ }
55
58
}
56
-
59
+
57
60
/**
58
61
* Retrieve the class object of a specific class in the current package.
59
62
* @param className - the specific class.
60
63
* @return Class object.
61
64
* @throws RuntimeException If we are unable to find the given class.
62
65
*/
63
66
public Class <?> getPackageClass (String className ) {
64
- try {
65
- Optional <Class <?>> result = cache .get (Preconditions .checkNotNull (className , "className cannot be NULL" ));
66
-
67
- // Concurrency is not a problem - we don't care if we look up a class twice
68
- if (result == null ) {
69
- // Look up the class dynamically
70
- result = Optional .<Class <?>>fromNullable (source .loadClass (combine (packageName , className )));
71
- if (!result .isPresent ())
72
- throw new IllegalArgumentException ("Source " + source + " returned NULL for " + className );
67
+ Preconditions .checkNotNull (className , "className cannot be null!" );
73
68
74
- cache .put (className , result );
75
- }
76
-
77
- // Class has been looked for and hasn't been found in the past
69
+ // See if we've already looked it up
70
+ if (cache .containsKey (className )) {
71
+ Optional <Class <?>> result = cache .get (className );
78
72
if (!result .isPresent ()) {
79
- throw new ClassNotFoundException ( );
73
+ throw new RuntimeException ( "Cannot find class " + className );
80
74
}
81
75
82
76
return result .get ();
83
- } catch (ClassNotFoundException e ) {
84
- setPackageClass (className , null );
85
- throw new RuntimeException ("Cannot find class " + combine (packageName , className ), e );
77
+ }
78
+
79
+ try {
80
+ // Try looking it up
81
+ Class <?> clazz = source .loadClass (combine (packageName , className ));
82
+ if (clazz == null ) {
83
+ throw new IllegalArgumentException ("Source " + source + " returned null for " + className );
84
+ }
85
+
86
+ cache .put (className , Optional .<Class <?>> of (clazz ));
87
+ return clazz ;
88
+ } catch (ClassNotFoundException ex ) {
89
+ cache .put (className , Optional .<Class <?>> absent ());
90
+ throw new RuntimeException ("Cannot find class " + className , ex );
86
91
}
87
92
}
88
93
0 commit comments