Polish [Standard]TypeLocator
This commit is contained in:
parent
40f1cf67bd
commit
311c58ea2d
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -18,8 +18,9 @@ package org.springframework.expression;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementers of this interface are expected to be able to locate types.
|
* Implementers of this interface are expected to be able to locate types.
|
||||||
* They may use a custom {@link ClassLoader} and/or deal with common
|
*
|
||||||
* package prefixes (e.g. {@code java.lang}) however they wish.
|
* <p>They may use a custom {@link ClassLoader} and/or deal with common package
|
||||||
|
* prefixes (for example, {@code java.lang}) however they wish.
|
||||||
*
|
*
|
||||||
* <p>See {@link org.springframework.expression.spel.support.StandardTypeLocator}
|
* <p>See {@link org.springframework.expression.spel.support.StandardTypeLocator}
|
||||||
* for an example implementation.
|
* for an example implementation.
|
||||||
|
|
@ -31,8 +32,9 @@ package org.springframework.expression;
|
||||||
public interface TypeLocator {
|
public interface TypeLocator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a type by name. The name may or may not be fully qualified
|
* Find a type by name.
|
||||||
* (e.g. {@code String} or {@code java.lang.String}).
|
* <p>The name may or may not be fully qualified — for example,
|
||||||
|
* {@code String} or {@code java.lang.String}.
|
||||||
* @param typeName the type to be located
|
* @param typeName the type to be located
|
||||||
* @return the {@code Class} object representing that type
|
* @return the {@code Class} object representing that type
|
||||||
* @throws EvaluationException if there is a problem finding the type
|
* @throws EvaluationException if there is a problem finding the type
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2020 the original author or authors.
|
* Copyright 2002-2023 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -28,12 +28,16 @@ import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple implementation of {@link TypeLocator} that uses the context ClassLoader
|
* A simple implementation of {@link TypeLocator} that uses the default
|
||||||
* (or any ClassLoader set upon it). It supports 'well-known' packages: So if a
|
* {@link ClassLoader} or a supplied {@link ClassLoader} to locate types.
|
||||||
* type cannot be found, it will try the registered imports to locate it.
|
*
|
||||||
|
* <p>Supports <em>well-known</em> packages, registered as
|
||||||
|
* {@linkplain #registerImport(String) import prefixes}. If a type cannot be found,
|
||||||
|
* this class will attempt to locate it using the registered import prefixes.
|
||||||
*
|
*
|
||||||
* @author Andy Clement
|
* @author Andy Clement
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class StandardTypeLocator implements TypeLocator {
|
public class StandardTypeLocator implements TypeLocator {
|
||||||
|
|
@ -41,20 +45,21 @@ public class StandardTypeLocator implements TypeLocator {
|
||||||
@Nullable
|
@Nullable
|
||||||
private final ClassLoader classLoader;
|
private final ClassLoader classLoader;
|
||||||
|
|
||||||
private final List<String> knownPackagePrefixes = new ArrayList<>(1);
|
private final List<String> importPrefixes = new ArrayList<>(1);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a StandardTypeLocator for the default ClassLoader
|
* Create a {@code StandardTypeLocator} for the default {@link ClassLoader}
|
||||||
* (typically, the thread context ClassLoader).
|
* (typically, the thread context {@code ClassLoader}).
|
||||||
|
* @see ClassUtils#getDefaultClassLoader()
|
||||||
*/
|
*/
|
||||||
public StandardTypeLocator() {
|
public StandardTypeLocator() {
|
||||||
this(ClassUtils.getDefaultClassLoader());
|
this(ClassUtils.getDefaultClassLoader());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a StandardTypeLocator for the given ClassLoader.
|
* Create a {@code StandardTypeLocator} for the given {@link ClassLoader}.
|
||||||
* @param classLoader the ClassLoader to delegate to
|
* @param classLoader the {@code ClassLoader} to delegate to
|
||||||
*/
|
*/
|
||||||
public StandardTypeLocator(@Nullable ClassLoader classLoader) {
|
public StandardTypeLocator(@Nullable ClassLoader classLoader) {
|
||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
|
|
@ -65,49 +70,48 @@ public class StandardTypeLocator implements TypeLocator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a new import prefix that will be used when searching for unqualified types.
|
* Register a new import prefix that will be used when searching for unqualified types.
|
||||||
* Expected format is something like "java.lang".
|
* <p>Expected format is something like {@code "java.lang"}.
|
||||||
* @param prefix the prefix to register
|
* @param prefix the import prefix to register
|
||||||
*/
|
*/
|
||||||
public void registerImport(String prefix) {
|
public void registerImport(String prefix) {
|
||||||
this.knownPackagePrefixes.add(prefix);
|
this.importPrefixes.add(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove that specified prefix from this locator's list of imports.
|
* Remove the specified prefix from this locator's list of imports.
|
||||||
* @param prefix the prefix to remove
|
* @param prefix the import prefix to remove
|
||||||
*/
|
*/
|
||||||
public void removeImport(String prefix) {
|
public void removeImport(String prefix) {
|
||||||
this.knownPackagePrefixes.remove(prefix);
|
this.importPrefixes.remove(prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a list of all the import prefixes registered with this StandardTypeLocator.
|
* Get the list of import prefixes registered with this {@code StandardTypeLocator}.
|
||||||
* @return a list of registered import prefixes
|
* @return the list of registered import prefixes
|
||||||
*/
|
*/
|
||||||
public List<String> getImportPrefixes() {
|
public List<String> getImportPrefixes() {
|
||||||
return Collections.unmodifiableList(this.knownPackagePrefixes);
|
return Collections.unmodifiableList(this.importPrefixes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a (possibly unqualified) type reference - first using the type name as-is,
|
* Find a (possibly unqualified) type reference, first using the type name as-is,
|
||||||
* then trying any registered prefixes if the type name cannot be found.
|
* and then trying any registered import prefixes if the type name cannot be found.
|
||||||
* @param typeName the type to locate
|
* @param typeName the type to locate
|
||||||
* @return the class object for the type
|
* @return the class object for the type
|
||||||
* @throws EvaluationException if the type cannot be found
|
* @throws EvaluationException if the type cannot be found
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Class<?> findType(String typeName) throws EvaluationException {
|
public Class<?> findType(String typeName) throws EvaluationException {
|
||||||
String nameToLookup = typeName;
|
|
||||||
try {
|
try {
|
||||||
return ClassUtils.forName(nameToLookup, this.classLoader);
|
return ClassUtils.forName(typeName, this.classLoader);
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ey) {
|
catch (ClassNotFoundException ex) {
|
||||||
// try any registered prefixes before giving up
|
// try any registered prefixes before giving up
|
||||||
}
|
}
|
||||||
for (String prefix : this.knownPackagePrefixes) {
|
for (String prefix : this.importPrefixes) {
|
||||||
try {
|
try {
|
||||||
nameToLookup = prefix + '.' + typeName;
|
String nameToLookup = prefix + '.' + typeName;
|
||||||
return ClassUtils.forName(nameToLookup, this.classLoader);
|
return ClassUtils.forName(nameToLookup, this.classLoader);
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ex) {
|
catch (ClassNotFoundException ex) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue