提交 400bbdaf 编写于 作者: K kohsuke

moved the code to locate the source jar file.


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@2291 71c3de6d-444a-0410-be80-ed276b4c234a
上级 23409727
package hudson.remoting;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
/**
* Locates where a given class is loaded from.
*
* @author Kohsuke Kawaguchi
*/
public class Which {
public static File jarFile(Class clazz) throws IOException {
String res = clazz.getClassLoader().getResource(clazz.getName().replace('.', '/') + ".class").toExternalForm();
if(res.startsWith("jar:")) {
res = res.substring(4,res.lastIndexOf('!')); // cut off jar: and the file name portion
return new File(decode(new URL(res).getPath()));
}
if(res.startsWith("file:")) {
// unpackaged classes
int n = clazz.getName().split("\\.").length; // how many slashes do wo need to cut?
for( ; n>0; n-- ) {
int idx = Math.max(res.lastIndexOf('/'), res.lastIndexOf('\\'));
res = res.substring(0,idx);
}
// won't work if res URL contains ' '
// return new File(new URI(null,new URL(res).toExternalForm(),null));
// won't work if res URL contains '%20'
// return new File(new URL(res).toURI());
return new File(decode(new URL(res).getPath()));
}
throw new IllegalArgumentException(res);
}
/**
* Decode '%HH'.
*/
private static String decode(String s) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for( int i=0; i<s.length();i++ ) {
char ch = s.charAt(i);
if(ch=='%') {
baos.write(hexToInt(s.charAt(i+1))*16 + hexToInt(s.charAt(i+2)));
i+=2;
continue;
}
baos.write(ch);
}
try {
return new String(baos.toByteArray(),"UTF-8");
} catch (UnsupportedEncodingException e) {
throw new Error(e); // impossible
}
}
private static int hexToInt(int ch) {
return Character.getNumericValue(ch);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册