|
| 1 | +/* |
| 2 | + * Copyright 2021 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
1 | 16 | package com.diffplug.spotless.pom;
|
2 | 17 |
|
3 | 18 | import java.io.IOException;
|
4 | 19 | import java.io.InputStream;
|
5 | 20 | import java.net.URL;
|
6 | 21 | import java.nio.Buffer;
|
7 | 22 | import java.nio.ByteBuffer;
|
8 |
| -import java.security.SecureClassLoader; |
9 | 23 | import java.util.Enumeration;
|
10 | 24 | import java.util.Vector;
|
11 | 25 |
|
|
16 | 30 | */
|
17 | 31 | public class JoinClassLoader extends ClassLoader {
|
18 | 32 |
|
19 |
| -private ClassLoader[] delegateClassLoaders; |
| 33 | + private ClassLoader[] delegateClassLoaders; |
20 | 34 |
|
21 |
| -public JoinClassLoader (ClassLoader parent, ClassLoader... delegateClassLoaders) { |
22 |
| - super(parent); |
23 |
| - this.delegateClassLoaders = delegateClassLoaders; } |
| 35 | + public JoinClassLoader(ClassLoader parent, ClassLoader... delegateClassLoaders) { |
| 36 | + super(parent); |
| 37 | + this.delegateClassLoaders = delegateClassLoaders; |
| 38 | + } |
24 | 39 |
|
25 |
| -protected Class<?> findClass (String name) throws ClassNotFoundException { |
26 |
| - // It would be easier to call the loadClass() methods of the delegateClassLoaders |
27 |
| - // here, but we have to load the class from the byte code ourselves, because we |
28 |
| - // need it to be associated with our class loader. |
29 |
| - String path = name.replace('.', '/') + ".class"; |
30 |
| - URL url = findResource(path); |
31 |
| - if (url == null) { |
32 |
| - throw new ClassNotFoundException(name); } |
33 |
| - ByteBuffer byteCode; |
34 |
| - try { |
35 |
| - byteCode = loadResource(url); } |
36 |
| - catch (IOException e) { |
37 |
| - throw new ClassNotFoundException(name, e); } |
38 |
| - return defineClass(name, byteCode, null); } |
| 40 | + protected Class<?> findClass(String name) throws ClassNotFoundException { |
| 41 | + // It would be easier to call the loadClass() methods of the delegateClassLoaders |
| 42 | + // here, but we have to load the class from the byte code ourselves, because we |
| 43 | + // need it to be associated with our class loader. |
| 44 | + String path = name.replace('.', '/') + ".class"; |
| 45 | + URL url = findResource(path); |
| 46 | + if (url == null) { |
| 47 | + throw new ClassNotFoundException(name); |
| 48 | + } |
| 49 | + ByteBuffer byteCode; |
| 50 | + try { |
| 51 | + byteCode = loadResource(url); |
| 52 | + } catch (IOException e) { |
| 53 | + throw new ClassNotFoundException(name, e); |
| 54 | + } |
| 55 | + return defineClass(name, byteCode, null); |
| 56 | + } |
39 | 57 |
|
40 |
| -private ByteBuffer loadResource (URL url) throws IOException { |
41 |
| - InputStream stream = null; |
42 |
| - try { |
43 |
| - stream = url.openStream(); |
44 |
| - int initialBufferCapacity = Math.min(0x40000, stream.available() + 1); |
45 |
| - if (initialBufferCapacity <= 2) { |
46 |
| - initialBufferCapacity = 0x10000; } |
47 |
| - else { |
48 |
| - initialBufferCapacity = Math.max(initialBufferCapacity, 0x200); } |
49 |
| - ByteBuffer buf = ByteBuffer.allocate(initialBufferCapacity); |
50 |
| - while (true) { |
51 |
| - if (!buf.hasRemaining()) { |
52 |
| - ByteBuffer newBuf = ByteBuffer.allocate(2*buf.capacity()); |
53 |
| - buf.flip(); |
54 |
| - newBuf.put(buf); |
55 |
| - buf = newBuf; } |
56 |
| - int len = stream.read(buf.array(), buf.position(), buf.remaining()); |
57 |
| - if (len <= 0) { |
58 |
| - break; } |
59 |
| - ((Buffer)buf).position(buf.position()+len); } |
60 |
| - ((Buffer)buf).flip(); |
61 |
| - return buf; } |
62 |
| - finally { |
63 |
| - if (stream != null) { |
64 |
| - stream.close(); }}} |
| 58 | + private ByteBuffer loadResource(URL url) throws IOException { |
| 59 | + InputStream stream = null; |
| 60 | + try { |
| 61 | + stream = url.openStream(); |
| 62 | + int initialBufferCapacity = Math.min(0x40000, stream.available() + 1); |
| 63 | + if (initialBufferCapacity <= 2) { |
| 64 | + initialBufferCapacity = 0x10000; |
| 65 | + } else { |
| 66 | + initialBufferCapacity = Math.max(initialBufferCapacity, 0x200); |
| 67 | + } |
| 68 | + ByteBuffer buf = ByteBuffer.allocate(initialBufferCapacity); |
| 69 | + while (true) { |
| 70 | + if (!buf.hasRemaining()) { |
| 71 | + ByteBuffer newBuf = ByteBuffer.allocate(2 * buf.capacity()); |
| 72 | + buf.flip(); |
| 73 | + newBuf.put(buf); |
| 74 | + buf = newBuf; |
| 75 | + } |
| 76 | + int len = stream.read(buf.array(), buf.position(), buf.remaining()); |
| 77 | + if (len <= 0) { |
| 78 | + break; |
| 79 | + } |
| 80 | + ((Buffer) buf).position(buf.position() + len); |
| 81 | + } |
| 82 | + ((Buffer) buf).flip(); |
| 83 | + return buf; |
| 84 | + } finally { |
| 85 | + if (stream != null) { |
| 86 | + stream.close(); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
65 | 90 |
|
66 |
| -protected URL findResource (String name) { |
67 |
| - for (ClassLoader delegate : delegateClassLoaders) { |
68 |
| - URL resource = delegate.getResource(name); |
69 |
| - if (resource != null) { |
70 |
| - return resource; }} |
71 |
| - return null; } |
| 91 | + protected URL findResource(String name) { |
| 92 | + for (ClassLoader delegate : delegateClassLoaders) { |
| 93 | + URL resource = delegate.getResource(name); |
| 94 | + if (resource != null) { |
| 95 | + return resource; |
| 96 | + } |
| 97 | + } |
| 98 | + return null; |
| 99 | + } |
72 | 100 |
|
73 |
| -protected Enumeration<URL> findResources (String name) throws IOException { |
74 |
| - Vector<URL> vector = new Vector<URL>(); |
75 |
| - for (ClassLoader delegate : delegateClassLoaders) { |
76 |
| - Enumeration<URL> enumeration = delegate.getResources(name); |
77 |
| - while (enumeration.hasMoreElements()) { |
78 |
| - vector.add(enumeration.nextElement()); }} |
79 |
| - return vector.elements(); } |
| 101 | + protected Enumeration<URL> findResources(String name) throws IOException { |
| 102 | + Vector<URL> vector = new Vector<URL>(); |
| 103 | + for (ClassLoader delegate : delegateClassLoaders) { |
| 104 | + Enumeration<URL> enumeration = delegate.getResources(name); |
| 105 | + while (enumeration.hasMoreElements()) { |
| 106 | + vector.add(enumeration.nextElement()); |
| 107 | + } |
| 108 | + } |
| 109 | + return vector.elements(); |
| 110 | + } |
80 | 111 |
|
81 | 112 | }
|
0 commit comments