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