Skip to content

Commit 311c58e

Browse files
committed
Polish [Standard]TypeLocator
1 parent 40f1cf6 commit 311c58e

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

spring-expression/src/main/java/org/springframework/expression/TypeLocator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,9 @@
1818

1919
/**
2020
* Implementers of this interface are expected to be able to locate types.
21-
* They may use a custom {@link ClassLoader} and/or deal with common
22-
* package prefixes (e.g. {@code java.lang}) however they wish.
21+
*
22+
* <p>They may use a custom {@link ClassLoader} and/or deal with common package
23+
* prefixes (for example, {@code java.lang}) however they wish.
2324
*
2425
* <p>See {@link org.springframework.expression.spel.support.StandardTypeLocator}
2526
* for an example implementation.
@@ -31,8 +32,9 @@
3132
public interface TypeLocator {
3233

3334
/**
34-
* Find a type by name. The name may or may not be fully qualified
35-
* (e.g. {@code String} or {@code java.lang.String}).
35+
* Find a type by name.
36+
* <p>The name may or may not be fully qualified &mdash; for example,
37+
* {@code String} or {@code java.lang.String}.
3638
* @param typeName the type to be located
3739
* @return the {@code Class} object representing that type
3840
* @throws EvaluationException if there is a problem finding the type

spring-expression/src/main/java/org/springframework/expression/spel/support/StandardTypeLocator.java

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,33 +28,38 @@
2828
import org.springframework.util.ClassUtils;
2929

3030
/**
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.
3437
*
3538
* @author Andy Clement
3639
* @author Juergen Hoeller
40+
* @author Sam Brannen
3741
* @since 3.0
3842
*/
3943
public class StandardTypeLocator implements TypeLocator {
4044

4145
@Nullable
4246
private final ClassLoader classLoader;
4347

44-
private final List<String> knownPackagePrefixes = new ArrayList<>(1);
48+
private final List<String> importPrefixes = new ArrayList<>(1);
4549

4650

4751
/**
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()
5055
*/
5156
public StandardTypeLocator() {
5257
this(ClassUtils.getDefaultClassLoader());
5358
}
5459

5560
/**
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
5863
*/
5964
public StandardTypeLocator(@Nullable ClassLoader classLoader) {
6065
this.classLoader = classLoader;
@@ -65,49 +70,48 @@ public StandardTypeLocator(@Nullable ClassLoader classLoader) {
6570

6671
/**
6772
* 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
7075
*/
7176
public void registerImport(String prefix) {
72-
this.knownPackagePrefixes.add(prefix);
77+
this.importPrefixes.add(prefix);
7378
}
7479

7580
/**
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
7883
*/
7984
public void removeImport(String prefix) {
80-
this.knownPackagePrefixes.remove(prefix);
85+
this.importPrefixes.remove(prefix);
8186
}
8287

8388
/**
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
8691
*/
8792
public List<String> getImportPrefixes() {
88-
return Collections.unmodifiableList(this.knownPackagePrefixes);
93+
return Collections.unmodifiableList(this.importPrefixes);
8994
}
9095

9196

9297
/**
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.
95100
* @param typeName the type to locate
96101
* @return the class object for the type
97102
* @throws EvaluationException if the type cannot be found
98103
*/
99104
@Override
100105
public Class<?> findType(String typeName) throws EvaluationException {
101-
String nameToLookup = typeName;
102106
try {
103-
return ClassUtils.forName(nameToLookup, this.classLoader);
107+
return ClassUtils.forName(typeName, this.classLoader);
104108
}
105-
catch (ClassNotFoundException ey) {
109+
catch (ClassNotFoundException ex) {
106110
// try any registered prefixes before giving up
107111
}
108-
for (String prefix : this.knownPackagePrefixes) {
112+
for (String prefix : this.importPrefixes) {
109113
try {
110-
nameToLookup = prefix + '.' + typeName;
114+
String nameToLookup = prefix + '.' + typeName;
111115
return ClassUtils.forName(nameToLookup, this.classLoader);
112116
}
113117
catch (ClassNotFoundException ex) {

0 commit comments

Comments
 (0)