JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/utils/CodeGenerationUtils.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.utils;
23
24import java.io.File;
25import java.net.URISyntaxException;
26import java.nio.file.Path;
27import java.nio.file.Paths;
28
29import static com.github.javaparser.utils.Utils.capitalize;
30import static com.github.javaparser.utils.Utils.decapitalize;
31
32/**
33 * Utilities that can be useful when generating code.
34 */
35public final class CodeGenerationUtils {
36    private CodeGenerationUtils() {
37    }
38
39    public static String getterName(Class<?> typeString name) {
40        if (name.startsWith("is") && boolean.class.equals(type)) {
41            return name;
42        } else if (Boolean.TYPE.equals(type)) {
43            return "is" + capitalize(name);
44        }
45        return "get" + capitalize(name);
46    }
47
48    public static String getterToPropertyName(String getterName) {
49        if (getterName.startsWith("is")) {
50            return decapitalize(getterName.substring("is".length()));
51        } else if (getterName.startsWith("get")) {
52            return decapitalize(getterName.substring("get".length()));
53        } else if (getterName.startsWith("has")) {
54            return decapitalize(getterName.substring("has".length()));
55        }
56        throw new IllegalArgumentException("Unexpected getterName '" + getterName + "'");
57    }
58
59    public static String setterName(String fieldName) {
60        if (fieldName.startsWith("is")) {
61            return "set" + fieldName.substring(2);
62        }
63        return "set" + capitalize(fieldName);
64    }
65
66    public static String optionalOf(String textboolean isOptional) {
67        if (isOptional) {
68            return f("Optional.of(%s)"text);
69        } else {
70            return "Optional.empty()";
71        }
72    }
73
74    /**
75     * A shortcut to String.format.
76     */
77    public static String f(String formatObject... params) {
78        return String.format(formatparams);
79    }
80
81    /**
82     * Calculates the path to a file in a package.
83     *
84     * @param root the root directory in which the package resides
85     * @param pkg the package in which the file resides, like "com.laamella.parser"
86     * @param file the filename of the file in the package.
87     */
88    public static Path fileInPackageAbsolutePath(String rootString pkgString file) {
89        pkg = packageToPath(pkg);
90        return Paths.get(rootpkgfile).normalize();
91    }
92
93    public static Path fileInPackageAbsolutePath(Path rootString pkgString file) {
94        return fileInPackageAbsolutePath(root.toString(), pkgfile);
95    }
96
97    /**
98     * Turns a package and a file into a relative path. "com.laamella" and "Simple.java" will become
99     * "com/laamella/Simple.java"
100     */
101    public static Path fileInPackageRelativePath(String pkgString file) {
102        pkg = packageToPath(pkg);
103        return Paths.get(pkgfile).normalize();
104    }
105
106    /**
107     * Converts a package name like "com.laamella.parser" to a path like "com/laamella/parser"
108     */
109    public static String packageToPath(String pkg) {
110        return pkg.replace('.'File.separatorChar);
111    }
112
113    /**
114     * Calculates the path of a package.
115     *
116     * @param root the root directory in which the package resides
117     * @param pkg the package, like "com.laamella.parser"
118     */
119    public static Path packageAbsolutePath(String rootString pkg) {
120        pkg = packageToPath(pkg);
121        return Paths.get(rootpkg).normalize();
122    }
123
124    public static Path packageAbsolutePath(Path rootString pkg) {
125        return packageAbsolutePath(root.toString(), pkg);
126    }
127
128    /**
129     * @return the root directory of the classloader for class c.
130     */
131    public static Path classLoaderRoot(Class<?> c) {
132        try {
133            return Paths.get(c.getProtectionDomain().getCodeSource().getLocation().toURI());
134        } catch (URISyntaxException e) {
135            throw new AssertionError("Bug in JavaParser, please report."e);
136        }
137    }
138
139    /**
140     * Useful for locating source code in your Maven project. Finds the classpath for class c, then backs up out of
141     * "target/(test-)classes", giving the directory containing the pom.xml.
142     */
143    public static Path mavenModuleRoot(Class<?> c) {
144        return classLoaderRoot(c).resolve(Paths.get("..""..")).normalize();
145    }
146
147    /**
148     * Shortens path "full" by cutting "difference" off the end of it.
149     */
150    public static Path subtractPaths(Path fullPath difference) {
151        while (difference != null) {
152            if (difference.getFileName().equals(full.getFileName())) {
153                difference = difference.getParent();
154                full = full.getParent();
155            } else {
156                throw new RuntimeException(f("'%s' could not be subtracted from '%s'"differencefull));
157            }
158        }
159        return full;
160    }
161}
162
MembersX
CodeGenerationUtils:fileInPackageRelativePath
CodeGenerationUtils:CodeGenerationUtils
CodeGenerationUtils:classLoaderRoot
CodeGenerationUtils:packageAbsolutePath
CodeGenerationUtils:f
CodeGenerationUtils:subtractPaths
CodeGenerationUtils:mavenModuleRoot
CodeGenerationUtils:setterName
CodeGenerationUtils:fileInPackageAbsolutePath
CodeGenerationUtils:getterToPropertyName
CodeGenerationUtils:getterName
CodeGenerationUtils:packageToPath
CodeGenerationUtils:optionalOf
Members
X