|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.github.javaparser.utils; |
|
|
|
import java.io.File; |
|
import java.net.URISyntaxException; |
|
import java.nio.file.Path; |
|
import java.nio.file.Paths; |
|
|
|
import static com.github.javaparser.utils.Utils.capitalize; |
|
import static com.github.javaparser.utils.Utils.decapitalize; |
|
|
|
|
|
|
|
*/ |
|
public final class CodeGenerationUtils { |
|
private CodeGenerationUtils() { |
|
} |
|
|
|
public static String getterName(Class<?> type, String name) { |
|
if (name.startsWith("is") && boolean.class.equals(type)) { |
|
return name; |
|
} else if (Boolean.TYPE.equals(type)) { |
|
return "is" + capitalize(name); |
|
} |
|
return "get" + capitalize(name); |
|
} |
|
|
|
public static String getterToPropertyName(String getterName) { |
|
if (getterName.startsWith("is")) { |
|
return decapitalize(getterName.substring("is".length())); |
|
} else if (getterName.startsWith("get")) { |
|
return decapitalize(getterName.substring("get".length())); |
|
} else if (getterName.startsWith("has")) { |
|
return decapitalize(getterName.substring("has".length())); |
|
} |
|
throw new IllegalArgumentException("Unexpected getterName '" + getterName + "'"); |
|
} |
|
|
|
public static String setterName(String fieldName) { |
|
if (fieldName.startsWith("is")) { |
|
return "set" + fieldName.substring(2); |
|
} |
|
return "set" + capitalize(fieldName); |
|
} |
|
|
|
public static String optionalOf(String text, boolean isOptional) { |
|
if (isOptional) { |
|
return f("Optional.of(%s)", text); |
|
} else { |
|
return "Optional.empty()"; |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static String f(String format, Object... params) { |
|
return String.format(format, params); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static Path fileInPackageAbsolutePath(String root, String pkg, String file) { |
|
pkg = packageToPath(pkg); |
|
return Paths.get(root, pkg, file).normalize(); |
|
} |
|
|
|
public static Path fileInPackageAbsolutePath(Path root, String pkg, String file) { |
|
return fileInPackageAbsolutePath(root.toString(), pkg, file); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public static Path fileInPackageRelativePath(String pkg, String file) { |
|
pkg = packageToPath(pkg); |
|
return Paths.get(pkg, file).normalize(); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static String packageToPath(String pkg) { |
|
return pkg.replace('.', File.separatorChar); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static Path packageAbsolutePath(String root, String pkg) { |
|
pkg = packageToPath(pkg); |
|
return Paths.get(root, pkg).normalize(); |
|
} |
|
|
|
public static Path packageAbsolutePath(Path root, String pkg) { |
|
return packageAbsolutePath(root.toString(), pkg); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static Path classLoaderRoot(Class<?> c) { |
|
try { |
|
return Paths.get(c.getProtectionDomain().getCodeSource().getLocation().toURI()); |
|
} catch (URISyntaxException e) { |
|
throw new AssertionError("Bug in JavaParser, please report.", e); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public static Path mavenModuleRoot(Class<?> c) { |
|
return classLoaderRoot(c).resolve(Paths.get("..", "..")).normalize(); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static Path subtractPaths(Path full, Path difference) { |
|
while (difference != null) { |
|
if (difference.getFileName().equals(full.getFileName())) { |
|
difference = difference.getParent(); |
|
full = full.getParent(); |
|
} else { |
|
throw new RuntimeException(f("'%s' could not be subtracted from '%s'", difference, full)); |
|
} |
|
} |
|
return full; |
|
} |
|
} |