1package com.github.javaparser.utils;
2
3import java.io.File;
4import java.net.URISyntaxException;
5import java.nio.file.Path;
6import java.nio.file.Paths;
7
8import static com.github.javaparser.utils.Utils.capitalize;
9import static com.github.javaparser.utils.Utils.decapitalize;
10
11public final class CodeGenerationUtils {
12    private CodeGenerationUtils() {
13    }
14
15    public static String getterName(Class<?> type, String name) {
16        if (name.startsWith("is")) {
17            return name;
18        } else if (type.equals(Boolean.class)) {
19            return "is" + capitalize(name);
20        }
21        return "get" + capitalize(name);
22    }
23
24    public static String getterToPropertyName(String getterName) {
25        if (getterName.startsWith("is")) {
26            return decapitalize(getterName.substring("is".length()));
27        } else if (getterName.startsWith("get")) {
28            return decapitalize(getterName.substring("get".length()));
29        } else if (getterName.startsWith("has")) {
30            return decapitalize(getterName.substring("has".length()));
31        }
32        throw new IllegalArgumentException("Unexpected getterName '" + getterName + "'");
33    }
34
35    public static String setterName(String fieldName) {
36        if (fieldName.startsWith("is")) {
37            return "set" + fieldName.substring(2);
38        }
39        return "set" + capitalize(fieldName);
40    }
41
42    public static String optionalOf(String text, boolean isOptional) {
43        if (isOptional) {
44            return f("Optional.of(%s)", text);
45        } else {
46            return "Optional.empty()";
47        }
48    }
49
50    /**
51     * A shortcut to String.format.
52     */
53    public static String f(String format, Object... params) {
54        return String.format(format, params);
55    }
56
57    /**
58     * Calculates the path to a file in a package.
59     *
60     * @param root the root directory in which the package resides
61     * @param pkg the package in which the file resides, like "com.laamella.parser"
62     * @param file the filename of the file in the package.
63     */
64    public static Path fileInPackageAbsolutePath(String root, String pkg, String file) {
65        pkg = packageToPath(pkg);
66        return Paths.get(root, pkg, file).normalize();
67    }
68
69    public static Path fileInPackageAbsolutePath(Path root, String pkg, String file) {
70        return fileInPackageAbsolutePath(root.toString(), pkg, file);
71    }
72
73    /**
74     * Turns a package and a file into a relative path. "com.laamella" and "Simple.java" will become
75     * "com/laamella/Simple.java"
76     */
77    public static Path fileInPackageRelativePath(String pkg, String file) {
78        pkg = packageToPath(pkg);
79        return Paths.get(pkg, file).normalize();
80    }
81
82    /**
83     * Converts a package name like "com.laamella.parser" to a path like "com/laamella/parser"
84     */
85    public static String packageToPath(String pkg) {
86        return pkg.replace(".", File.separator);
87    }
88
89    /**
90     * Calculates the path of a package.
91     *
92     * @param root the root directory in which the package resides
93     * @param pkg the package, like "com.laamella.parser"
94     */
95    public static Path packageAbsolutePath(String root, String pkg) {
96        pkg = packageToPath(pkg);
97        return Paths.get(root, pkg).normalize();
98    }
99
100    public static Path packageAbsolutePath(Path root, String pkg) {
101        return packageAbsolutePath(root.toString(), pkg);
102    }
103
104    /**
105     * @return the root directory of the classloader for class c.
106     */
107    public static Path classLoaderRoot(Class<?> c) {
108        try {
109            return Paths.get(c.getProtectionDomain().getCodeSource().getLocation().toURI());
110        } catch (URISyntaxException e) {
111            throw new AssertionError("Bug in JavaParser, please report.", e);
112        }
113    }
114
115    /**
116     * Useful for locating source code in your Maven project. Finds the classpath for class c, then backs up out of
117     * "target/(test-)classes", giving the directory containing the pom.xml.
118     */
119    public static Path mavenModuleRoot(Class<?> c) {
120        return classLoaderRoot(c).resolve(Paths.get("..", "..")).normalize();
121    }
122
123    /**
124     * Shortens path "full" by cutting "difference" off the end of it.
125     */
126    public static Path subtractPaths(Path full, Path difference) {
127        while (difference != null) {
128            if (difference.getFileName().equals(full.getFileName())) {
129                difference = difference.getParent();
130                full = full.getParent();
131            } else {
132                throw new RuntimeException(f("'%s' could not be subtracted from '%s'", difference, full));
133            }
134        }
135        return full;
136    }
137}
138