1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2023 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import org .springframework .util .ClassUtils ;
29
29
30
30
/**
31
- * A simple implementation of {@link TypeLocator} that uses the context ClassLoader
32
- * (or any ClassLoader set upon it). It supports 'well-known' packages: So if a
33
- * type cannot be found, it will try the registered imports to locate it.
31
+ * A simple implementation of {@link TypeLocator} that uses the default
32
+ * {@link ClassLoader} or a supplied {@link ClassLoader} to locate types.
33
+ *
34
+ * <p>Supports <em>well-known</em> packages, registered as
35
+ * {@linkplain #registerImport(String) import prefixes}. If a type cannot be found,
36
+ * this class will attempt to locate it using the registered import prefixes.
34
37
*
35
38
* @author Andy Clement
36
39
* @author Juergen Hoeller
40
+ * @author Sam Brannen
37
41
* @since 3.0
38
42
*/
39
43
public class StandardTypeLocator implements TypeLocator {
40
44
41
45
@ Nullable
42
46
private final ClassLoader classLoader ;
43
47
44
- private final List <String > knownPackagePrefixes = new ArrayList <>(1 );
48
+ private final List <String > importPrefixes = new ArrayList <>(1 );
45
49
46
50
47
51
/**
48
- * Create a StandardTypeLocator for the default ClassLoader
49
- * (typically, the thread context ClassLoader).
52
+ * Create a {@code StandardTypeLocator} for the default {@link ClassLoader}
53
+ * (typically, the thread context {@code ClassLoader}).
54
+ * @see ClassUtils#getDefaultClassLoader()
50
55
*/
51
56
public StandardTypeLocator () {
52
57
this (ClassUtils .getDefaultClassLoader ());
53
58
}
54
59
55
60
/**
56
- * Create a StandardTypeLocator for the given ClassLoader.
57
- * @param classLoader the ClassLoader to delegate to
61
+ * Create a {@code StandardTypeLocator} for the given {@link ClassLoader} .
62
+ * @param classLoader the {@code ClassLoader} to delegate to
58
63
*/
59
64
public StandardTypeLocator (@ Nullable ClassLoader classLoader ) {
60
65
this .classLoader = classLoader ;
@@ -65,49 +70,48 @@ public StandardTypeLocator(@Nullable ClassLoader classLoader) {
65
70
66
71
/**
67
72
* Register a new import prefix that will be used when searching for unqualified types.
68
- * Expected format is something like "java.lang".
69
- * @param prefix the prefix to register
73
+ * <p> Expected format is something like {@code "java.lang"} .
74
+ * @param prefix the import prefix to register
70
75
*/
71
76
public void registerImport (String prefix ) {
72
- this .knownPackagePrefixes .add (prefix );
77
+ this .importPrefixes .add (prefix );
73
78
}
74
79
75
80
/**
76
- * Remove that specified prefix from this locator's list of imports.
77
- * @param prefix the prefix to remove
81
+ * Remove the specified prefix from this locator's list of imports.
82
+ * @param prefix the import prefix to remove
78
83
*/
79
84
public void removeImport (String prefix ) {
80
- this .knownPackagePrefixes .remove (prefix );
85
+ this .importPrefixes .remove (prefix );
81
86
}
82
87
83
88
/**
84
- * Return a list of all the import prefixes registered with this StandardTypeLocator.
85
- * @return a list of registered import prefixes
89
+ * Get the list of import prefixes registered with this {@code StandardTypeLocator} .
90
+ * @return the list of registered import prefixes
86
91
*/
87
92
public List <String > getImportPrefixes () {
88
- return Collections .unmodifiableList (this .knownPackagePrefixes );
93
+ return Collections .unmodifiableList (this .importPrefixes );
89
94
}
90
95
91
96
92
97
/**
93
- * Find a (possibly unqualified) type reference - first using the type name as-is,
94
- * then trying any registered prefixes if the type name cannot be found.
98
+ * Find a (possibly unqualified) type reference, first using the type name as-is,
99
+ * and then trying any registered import prefixes if the type name cannot be found.
95
100
* @param typeName the type to locate
96
101
* @return the class object for the type
97
102
* @throws EvaluationException if the type cannot be found
98
103
*/
99
104
@ Override
100
105
public Class <?> findType (String typeName ) throws EvaluationException {
101
- String nameToLookup = typeName ;
102
106
try {
103
- return ClassUtils .forName (nameToLookup , this .classLoader );
107
+ return ClassUtils .forName (typeName , this .classLoader );
104
108
}
105
- catch (ClassNotFoundException ey ) {
109
+ catch (ClassNotFoundException ex ) {
106
110
// try any registered prefixes before giving up
107
111
}
108
- for (String prefix : this .knownPackagePrefixes ) {
112
+ for (String prefix : this .importPrefixes ) {
109
113
try {
110
- nameToLookup = prefix + '.' + typeName ;
114
+ String nameToLookup = prefix + '.' + typeName ;
111
115
return ClassUtils .forName (nameToLookup , this .classLoader );
112
116
}
113
117
catch (ClassNotFoundException ex ) {
0 commit comments