< prev index next >
src/java.base/share/classes/jdk/internal/loader/URLClassPath.java
Print this page
8198481: Coding style cleanups for src/java.base/share/classes/jdk/internal/loader/ClassLoaders.java
Reviewed-by: mchung, alanb
@@ -41,10 +41,11 @@
import java.security.AccessControlContext;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.CodeSigner;
import java.security.Permission;
+import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -97,31 +98,34 @@
p = props.getProperty("jdk.net.URLClassPath.disableRestrictedPermissions");
DISABLE_ACC_CHECKING = p != null ? p.equals("true") || p.equals("") : false;
}
- /* The original search path of URLs. */
+ /** The original search path of URLs. */
private final List<URL> path;
- /* The stack of unopened URLs */
+ /** The stack of unopened URLs. */
private final Stack<URL> urls = new Stack<>();
- /* The resulting search path of Loaders */
+ /** The resulting search path of Loaders. */
private final ArrayList<Loader> loaders = new ArrayList<>();
- /* Map of each URL opened to its corresponding Loader */
+ /** Map of each URL opened to its corresponding Loader. */
private final HashMap<String, Loader> lmap = new HashMap<>();
- /* The jar protocol handler to use when creating new URLs */
+ /** The jar protocol handler to use when creating new URLs. */
private final URLStreamHandler jarHandler;
- /* Whether this URLClassLoader has been closed yet */
+ /** Whether this URLClassLoader has been closed yet. */
private boolean closed = false;
- /* The context to be used when loading classes and resources. If non-null
- * this is the context that was captured during the creation of the
- * URLClassLoader. null implies no additional security restrictions. */
+ /**
+ * The context to be used when loading classes and resources. If
+ * non-null this is the context that was captured during the creation
+ * of the URLClassLoader. null implies no additional security
+ * restrictions.
+ */
private final AccessControlContext acc;
/**
* Creates a new URLClassPath for the given URLs. The URLs will be
* searched in the order specified for classes and resources. A URL
@@ -207,11 +211,11 @@
List<IOException> result = new LinkedList<>();
for (Loader loader : loaders) {
try {
loader.close();
} catch (IOException e) {
- result.add (e);
+ result.add(e);
}
}
closed = true;
return result;
}
@@ -270,11 +274,11 @@
* Finds the resource with the specified name on the URL search path
* or null if not found or security check fails.
*
* @param name the name of the resource
* @param check whether to perform a security check
- * @return a <code>URL</code> for the resource, or <code>null</code>
+ * @return a {@code URL} for the resource, or {@code null}
* if the resource could not be found.
*/
public URL findResource(String name, boolean check) {
Loader loader;
for (int i = 0; (loader = getLoader(i)) != null; i++) {
@@ -401,11 +405,11 @@
public Enumeration<Resource> getResources(final String name) {
return getResources(name, true);
}
- /*
+ /**
* Returns the Loader at the specified position in the URL search
* path. The URLs are opened and expanded as needed. Returns null
* if the specified index is out of range.
*/
private synchronized Loader getLoader(int index) {
@@ -458,17 +462,17 @@
lmap.put(urlNoFragString, loader);
}
return loaders.get(index);
}
- /*
+ /**
* Returns the Loader for the specified base URL.
*/
private Loader getLoader(final URL url) throws IOException {
try {
- return java.security.AccessController.doPrivileged(
- new java.security.PrivilegedExceptionAction<>() {
+ return AccessController.doPrivileged(
+ new PrivilegedExceptionAction<>() {
public Loader run() throws IOException {
String protocol = url.getProtocol(); // lower cased in URL
String file = url.getFile();
if (file != null && file.endsWith("/")) {
if ("file".equals(protocol)) {
@@ -485,11 +489,11 @@
} else {
return new JarLoader(url, jarHandler, lmap, acc);
}
}
}, acc);
- } catch (java.security.PrivilegedActionException pae) {
+ } catch (PrivilegedActionException pae) {
throw (IOException)pae.getException();
}
}
private static final JavaNetURLAccess JNUA
@@ -498,24 +502,24 @@
private static boolean isDefaultJarHandler(URL u) {
URLStreamHandler h = JNUA.getHandler(u);
return h instanceof sun.net.www.protocol.jar.Handler;
}
- /*
+ /**
* Pushes the specified URLs onto the list of unopened URLs.
*/
private void push(URL[] us) {
synchronized (urls) {
for (int i = us.length - 1; i >= 0; --i) {
urls.push(us[i]);
}
}
}
- /*
- * Check whether the resource URL should be returned.
- * Return null on security check failure.
+ /**
+ * Checks whether the resource URL should be returned.
+ * Returns null on security check failure.
* Called by java.net.URLClassLoader.
*/
public static URL checkURL(URL url) {
if (url != null) {
try {
@@ -525,13 +529,13 @@
}
}
return url;
}
- /*
- * Check whether the resource URL should be returned.
- * Throw exception on failure.
+ /**
+ * Checks whether the resource URL should be returned.
+ * Throws exception on failure.
* Called internally within this file.
*/
public static void check(URL url) throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) {
@@ -569,18 +573,18 @@
*/
private static class Loader implements Closeable {
private final URL base;
private JarFile jarfile; // if this points to a jar file
- /*
+ /**
* Creates a new Loader for the specified URL.
*/
Loader(URL url) {
base = url;
}
- /*
+ /**
* Returns the base URL for this Loader.
*/
URL getBaseURL() {
return base;
}
@@ -656,39 +660,39 @@
return uc.getContentLength();
}
};
}
- /*
+ /**
* Returns the Resource for the specified name, or null if not
* found or the caller does not have the permission to get the
* resource.
*/
Resource getResource(final String name) {
return getResource(name, true);
}
- /*
- * close this loader and release all resources
- * method overridden in sub-classes
+ /**
+ * Closes this loader and release all resources.
+ * Method overridden in sub-classes.
*/
@Override
public void close() throws IOException {
if (jarfile != null) {
jarfile.close();
}
}
- /*
+ /**
* Returns the local class path for this loader, or null if none.
*/
URL[] getClassPath() throws IOException {
return null;
}
}
- /*
+ /**
* Nested class used to represent a Loader of resources from a JAR URL.
*/
static class JarLoader extends Loader {
private JarFile jar;
private final URL csu;
@@ -698,11 +702,11 @@
private final AccessControlContext acc;
private boolean closed = false;
private static final JavaUtilZipFileAccess zipAccess =
SharedSecrets.getJavaUtilZipFileAccess();
- /*
+ /**
* Creates a new JarLoader for the specified URL referring to
* a JAR file.
*/
JarLoader(URL url, URLStreamHandler jarHandler,
HashMap<String, Loader> loaderMap,
@@ -738,12 +742,12 @@
}
private void ensureOpen() throws IOException {
if (jar == null) {
try {
- java.security.AccessController.doPrivileged(
- new java.security.PrivilegedExceptionAction<>() {
+ AccessController.doPrivileged(
+ new PrivilegedExceptionAction<>() {
public Void run() throws IOException {
if (DEBUG) {
System.err.println("Opening " + csu);
Thread.dumpStack();
}
@@ -755,11 +759,11 @@
// Add all the dependent URLs to the lmap so that loaders
// will not be created for them by URLClassPath.getLoader(int)
// if the same URL occurs later on the main class path. We set
// Loader to null here to avoid creating a Loader for each
// URL until we actually need to try to load something from them.
- for(int i = 0; i < jarfiles.length; i++) {
+ for (int i = 0; i < jarfiles.length; i++) {
try {
URL jarURL = new URL(csu, jarfiles[i]);
// If a non-null loader already exists, leave it alone.
String urlNoFragString = URLUtil.urlNoFragString(jarURL);
if (!lmap.containsKey(urlNoFragString)) {
@@ -771,17 +775,17 @@
}
}
return null;
}
}, acc);
- } catch (java.security.PrivilegedActionException pae) {
+ } catch (PrivilegedActionException pae) {
throw (IOException)pae.getException();
}
}
}
- /* Throws if the given jar file is does not start with the correct LOC */
+ /** Throws if the given jar file is does not start with the correct LOC */
static JarFile checkJar(JarFile jar) throws IOException {
if (System.getSecurityManager() != null && !DISABLE_JAR_CHECKING
&& !zipAccess.startsWithLocHeader(jar)) {
IOException x = new IOException("Invalid Jar file");
try {
@@ -809,11 +813,11 @@
uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION);
JarFile jarFile = ((JarURLConnection)uc).getJarFile();
return checkJar(jarFile);
}
- /*
+ /**
* Returns the index of this JarLoader if it exists.
*/
JarIndex getIndex() {
try {
ensureOpen();
@@ -821,11 +825,11 @@
throw new InternalError(e);
}
return index;
}
- /*
+ /**
* Creates the resource and if the check flag is set to true, checks if
* is its okay to return the resource.
*/
Resource checkResource(final String name, boolean check,
final JarEntry entry) {
@@ -867,37 +871,37 @@
{ return entry.getCodeSigners(); };
};
}
- /*
+ /**
* Returns true iff at least one resource in the jar file has the same
* package name as that of the specified resource name.
*/
boolean validIndex(final String name) {
String packageName = name;
int pos;
- if((pos = name.lastIndexOf('/')) != -1) {
+ if ((pos = name.lastIndexOf('/')) != -1) {
packageName = name.substring(0, pos);
}
String entryName;
ZipEntry entry;
Enumeration<JarEntry> enum_ = jar.entries();
while (enum_.hasMoreElements()) {
entry = enum_.nextElement();
entryName = entry.getName();
- if((pos = entryName.lastIndexOf('/')) != -1)
+ if ((pos = entryName.lastIndexOf('/')) != -1)
entryName = entryName.substring(0, pos);
if (entryName.equals(packageName)) {
return true;
}
}
return false;
}
- /*
+ /**
* Returns the URL for a resource with the specified name
*/
@Override
URL findResource(final String name, boolean check) {
Resource rsc = getResource(name, check);
@@ -905,11 +909,11 @@
return rsc.getURL();
}
return null;
}
- /*
+ /**
* Returns the JAR Resource for the specified name.
*/
@Override
Resource getResource(final String name, boolean check) {
try {
@@ -926,16 +930,16 @@
HashSet<String> visited = new HashSet<>();
return getResource(name, check, visited);
}
- /*
+ /**
* Version of getResource() that tracks the jar files that have been
* visited by linking through the index files. This helper method uses
* a HashSet to store the URLs of jar files that have been searched and
* uses it to avoid going into an infinite loop, looking for a
- * non-existent resource
+ * non-existent resource.
*/
Resource getResource(final String name, boolean check,
Set<String> visited) {
Resource res;
String[] jarFiles;
@@ -943,18 +947,18 @@
LinkedList<String> jarFilesList = null;
/* If there no jar files in the index that can potential contain
* this resource then return immediately.
*/
- if((jarFilesList = index.get(name)) == null)
+ if ((jarFilesList = index.get(name)) == null)
return null;
do {
int size = jarFilesList.size();
jarFiles = jarFilesList.toArray(new String[size]);
/* loop through the mapped jar file list */
- while(count < size) {
+ while (count < size) {
String jarName = jarFiles[count++];
JarLoader newLoader;
final URL url;
try{
@@ -975,20 +979,20 @@
/* this newly opened jar file has its own index,
* merge it into the parent's index, taking into
* account the relative path.
*/
JarIndex newIndex = newLoader.getIndex();
- if(newIndex != null) {
+ if (newIndex != null) {
int pos = jarName.lastIndexOf('/');
newIndex.merge(this.index, (pos == -1 ?
null : jarName.substring(0, pos + 1)));
}
/* put it in the global hashtable */
lmap.put(urlNoFragString, newLoader);
}
- } catch (java.security.PrivilegedActionException pae) {
+ } catch (PrivilegedActionException pae) {
continue;
} catch (MalformedURLException e) {
continue;
}
@@ -1027,26 +1031,26 @@
continue;
}
/* Process the index of the new loader
*/
- if((res = newLoader.getResource(name, check, visited))
+ if ((res = newLoader.getResource(name, check, visited))
!= null) {
return res;
}
}
// Get the list of jar files again as the list could have grown
// due to merging of index files.
jarFilesList = index.get(name);
// If the count is unchanged, we are done.
- } while(count < jarFilesList.size());
+ } while (count < jarFilesList.size());
return null;
}
- /*
+ /**
* Returns the JAR file local class path, or null if none.
*/
@Override
URL[] getClassPath() throws IOException {
if (index != null) {
@@ -1069,11 +1073,11 @@
}
}
return null;
}
- /*
+ /**
* Parses value of the Class-Path manifest attribute and returns
* an array of URLs relative to the specified base URL.
*/
private static URL[] parseClassPath(URL base, String value)
throws MalformedURLException
@@ -1088,16 +1092,16 @@
}
return urls;
}
}
- /*
+ /**
* Nested class used to represent a loader of classes and resources
* from a file URL that refers to a directory.
*/
private static class FileLoader extends Loader {
- /* Canonicalized File */
+ /** Canonicalized File */
private File dir;
FileLoader(URL url) throws IOException {
super(url);
if (!"file".equals(url.getProtocol())) {
@@ -1106,11 +1110,11 @@
String path = url.getFile().replace('/', File.separatorChar);
path = ParseUtil.decode(path);
dir = (new File(path)).getCanonicalFile();
}
- /*
+ /**
* Returns the URL for a resource with the specified name
*/
@Override
URL findResource(final String name, boolean check) {
Resource rsc = getResource(name, check);
< prev index next >