@author SeanDragon
| 6 | * @author SeanDragon |
| 7 | */ |
| 8 | public final class ToolPosition { |
| 9 | |
| 10 | private static String webRootPath; |
| 11 | private static String rootClassPath; |
| 12 | |
| 13 | @SuppressWarnings("rawtypes") |
| 14 | public static String getPath(Class clazz) { |
| 15 | String path = clazz.getResource("").getPath(); |
| 16 | return new File(path).getAbsolutePath(); |
| 17 | } |
| 18 | |
| 19 | public static String getPath(Object object) { |
| 20 | String path = object.getClass().getResource("").getPath(); |
| 21 | return new File(path).getAbsolutePath(); |
| 22 | } |
| 23 | |
| 24 | public static String getRootClassPath() { |
| 25 | if (rootClassPath == null) { |
| 26 | try { |
| 27 | String path = ToolPosition.class.getClassLoader().getResource("").toURI().getPath(); |
| 28 | rootClassPath = new File(path).getAbsolutePath(); |
| 29 | } catch (Exception e) { |
| 30 | String path = ToolPosition.class.getClassLoader().getResource("").getPath(); |
| 31 | rootClassPath = new File(path).getAbsolutePath(); |
| 32 | } |
| 33 | } |
| 34 | return rootClassPath; |
| 35 | } |
| 36 | |
| 37 | public static String getPackagePath(Object object) { |
| 38 | Package p = object.getClass().getPackage(); |
| 39 | return p != null ? p.getName().replaceAll("\\.", "/") : ""; |
| 40 | } |
| 41 | |
| 42 | public static File getFileFromJar(String file) { |
| 43 | throw new RuntimeException("Not finish. Do not use this method."); |
| 44 | } |
| 45 | |
| 46 | public static String getWebRootPath() { |
| 47 | if (webRootPath == null) { |
| 48 | webRootPath = detectWebRootPath(); |
| 49 | } |
| 50 | return webRootPath; |
| 51 | } |
| 52 | |
| 53 | private static String detectWebRootPath() { |
| 54 | try { |
| 55 | String path = ToolPosition.class.getResource("/").toURI().getPath(); |
| 56 | return new File(path).getParentFile().getParentFile().getCanonicalPath(); |
| 57 | } catch (Exception e) { |
| 58 | throw new RuntimeException(e); |
| 59 | } |
| 60 | } |
| 61 | } |
nothing calls this directly
no outgoing calls
no test coverage detected