String (@Nullable Path basePath, @NotNull Class<?> clazz) { final ClassLoader loader = clazz.getClassLoader(); if (loader == null) return null; if (loader == ClassLoader.getSystemClassLoader()) return null; String resourceName = getClassResourcePath(clazz); URL url = loader.getResource(resourceName); if (url == null) return null; String path = url.toString(); if (path.startsWith("jar:")) { int sep = path.lastIndexOf("!/"); path = path.substring(4, sep); } if (path.startsWith("jrt:")) { //platform classes goes without source: attribute return null; } if (!path.startsWith("file:")) { log("Class " + clazz.getName() + " has unexpected source: " + url); return path; } if (basePath != null) { try { Path resolvedPath = Paths.get(URI.create(path)); try { path = basePath.relativize(resolvedPath).toString(); } catch (Throwable t) { //can fail because of a file-system, e.g. java.lang.IllegalArgumentException: 'other' has different root path = resolvedPath.toString(); } } catch (Throwable t) { //MOP } } return path; }