| 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 | |
| 22 | package com.github.javaparser; |
| 23 | |
| 24 | import com.github.javaparser.ast.CompilationUnit; |
| 25 | import com.github.javaparser.ast.ImportDeclaration; |
| 26 | import com.github.javaparser.ast.Node; |
| 27 | import com.github.javaparser.ast.PackageDeclaration; |
| 28 | import com.github.javaparser.ast.body.BodyDeclaration; |
| 29 | import com.github.javaparser.ast.body.MethodDeclaration; |
| 30 | import com.github.javaparser.ast.body.Parameter; |
| 31 | import com.github.javaparser.ast.body.TypeDeclaration; |
| 32 | import com.github.javaparser.ast.expr.*; |
| 33 | import com.github.javaparser.ast.modules.ModuleDeclaration; |
| 34 | import com.github.javaparser.ast.modules.ModuleDirective; |
| 35 | import com.github.javaparser.ast.stmt.BlockStmt; |
| 36 | import com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt; |
| 37 | import com.github.javaparser.ast.stmt.Statement; |
| 38 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
| 39 | import com.github.javaparser.ast.type.Type; |
| 40 | import com.github.javaparser.ast.type.TypeParameter; |
| 41 | import com.github.javaparser.javadoc.Javadoc; |
| 42 | |
| 43 | import java.io.*; |
| 44 | import java.nio.charset.Charset; |
| 45 | import java.nio.file.Path; |
| 46 | |
| 47 | /** |
| 48 | * A simpler, static API than {@link JavaParser}. |
| 49 | */ |
| 50 | public final class StaticJavaParser { |
| 51 | private static ParserConfiguration configuration = new ParserConfiguration(); |
| 52 | |
| 53 | private StaticJavaParser() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the configuration for the parse... methods. |
| 58 | */ |
| 59 | public static ParserConfiguration getConfiguration() { |
| 60 | return configuration; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Set the configuration for the static parse... methods. |
| 65 | * This is a STATIC field, so modifying it will directly change how all static parse... methods work! |
| 66 | */ |
| 67 | public static void setConfiguration(ParserConfiguration configuration) { |
| 68 | StaticJavaParser.configuration = configuration; |
| 69 | } |
| 70 | |
| 71 | private static JavaParser newParser() { |
| 72 | return new JavaParser(configuration); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Parses the Java code contained in the {@link InputStream} and returns a |
| 77 | * {@link CompilationUnit} that represents it. |
| 78 | * |
| 79 | * @param in {@link InputStream} containing Java source code. It will be closed after parsing. |
| 80 | * @param encoding encoding of the source code |
| 81 | * @return CompilationUnit representing the Java source code |
| 82 | * @throws ParseProblemException if the source code has parser errors |
| 83 | * @deprecated set the encoding in the {@link ParserConfiguration} |
| 84 | */ |
| 85 | @Deprecated |
| 86 | public static CompilationUnit parse(final InputStream in, Charset encoding) { |
| 87 | return handleResult(newParser().parse(in, encoding)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Parses the Java code contained in the {@link InputStream} and returns a |
| 92 | * {@link CompilationUnit} that represents it.<br> |
| 93 | * |
| 94 | * @param in {@link InputStream} containing Java source code. It will be closed after parsing. |
| 95 | * @return CompilationUnit representing the Java source code |
| 96 | * @throws ParseProblemException if the source code has parser errors |
| 97 | */ |
| 98 | public static CompilationUnit parse(final InputStream in) { |
| 99 | return handleResult(newParser().parse(in)); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Parses the Java code contained in a {@link File} and returns a |
| 104 | * {@link CompilationUnit} that represents it. |
| 105 | * |
| 106 | * @param file {@link File} containing Java source code. It will be closed after parsing. |
| 107 | * @param encoding encoding of the source code |
| 108 | * @return CompilationUnit representing the Java source code |
| 109 | * @throws ParseProblemException if the source code has parser errors |
| 110 | * @throws FileNotFoundException the file was not found |
| 111 | * @deprecated set the encoding in the {@link ParserConfiguration} |
| 112 | */ |
| 113 | @Deprecated |
| 114 | public static CompilationUnit parse(final File file, final Charset encoding) throws FileNotFoundException { |
| 115 | return handleResult(newParser().parse(file, encoding)); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Parses the Java code contained in a {@link File} and returns a |
| 120 | * {@link CompilationUnit} that represents it.<br> |
| 121 | * |
| 122 | * @param file {@link File} containing Java source code. It will be closed after parsing. |
| 123 | * @return CompilationUnit representing the Java source code |
| 124 | * @throws ParseProblemException if the source code has parser errors |
| 125 | * @throws FileNotFoundException the file was not found |
| 126 | */ |
| 127 | public static CompilationUnit parse(final File file) throws FileNotFoundException { |
| 128 | return handleResult(newParser().parse(file)); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Parses the Java code contained in a file and returns a |
| 133 | * {@link CompilationUnit} that represents it. |
| 134 | * |
| 135 | * @param path path to a file containing Java source code |
| 136 | * @param encoding encoding of the source code |
| 137 | * @return CompilationUnit representing the Java source code |
| 138 | * @throws IOException the path could not be accessed |
| 139 | * @throws ParseProblemException if the source code has parser errors |
| 140 | * @deprecated set the encoding in the {@link ParserConfiguration} |
| 141 | */ |
| 142 | @Deprecated |
| 143 | public static CompilationUnit parse(final Path path, final Charset encoding) throws IOException { |
| 144 | return handleResult(newParser().parse(path, encoding)); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Parses the Java code contained in a file and returns a |
| 149 | * {@link CompilationUnit} that represents it.<br> |
| 150 | * |
| 151 | * @param path path to a file containing Java source code |
| 152 | * @return CompilationUnit representing the Java source code |
| 153 | * @throws ParseProblemException if the source code has parser errors |
| 154 | * @throws IOException the path could not be accessed |
| 155 | */ |
| 156 | public static CompilationUnit parse(final Path path) throws IOException { |
| 157 | return handleResult(newParser().parse(path)); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Parses the Java code contained in a resource and returns a |
| 162 | * {@link CompilationUnit} that represents it.<br> |
| 163 | * |
| 164 | * @param path path to a resource containing Java source code. As resource is accessed through a class loader, a |
| 165 | * leading "/" is not allowed in pathToResource |
| 166 | * @return CompilationUnit representing the Java source code |
| 167 | * @throws ParseProblemException if the source code has parser errors |
| 168 | * @throws IOException the path could not be accessed |
| 169 | */ |
| 170 | public static CompilationUnit parseResource(final String path) throws IOException { |
| 171 | return handleResult(newParser().parseResource(path)); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Parses the Java code contained in a resource and returns a |
| 176 | * {@link CompilationUnit} that represents it.<br> |
| 177 | * |
| 178 | * @param path path to a resource containing Java source code. As resource is accessed through a class loader, a |
| 179 | * leading "/" is not allowed in pathToResource |
| 180 | * @param encoding encoding of the source code |
| 181 | * @return CompilationUnit representing the Java source code |
| 182 | * @throws ParseProblemException if the source code has parser errors |
| 183 | * @throws IOException the path could not be accessed |
| 184 | * @deprecated set the encoding in the {@link ParserConfiguration} |
| 185 | */ |
| 186 | @Deprecated |
| 187 | public static CompilationUnit parseResource(final String path, Charset encoding) throws IOException { |
| 188 | return handleResult(newParser().parseResource(path, encoding)); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Parses the Java code contained in a resource and returns a |
| 193 | * {@link CompilationUnit} that represents it.<br> |
| 194 | * |
| 195 | * @param classLoader the classLoader that is asked to load the resource |
| 196 | * @param path path to a resource containing Java source code. As resource is accessed through a class loader, a |
| 197 | * leading "/" is not allowed in pathToResource |
| 198 | * @return CompilationUnit representing the Java source code |
| 199 | * @throws ParseProblemException if the source code has parser errors |
| 200 | * @throws IOException the path could not be accessed |
| 201 | * @deprecated set the encoding in the {@link ParserConfiguration} |
| 202 | */ |
| 203 | @Deprecated |
| 204 | public static CompilationUnit parseResource(final ClassLoader classLoader, final String path, Charset encoding) throws IOException { |
| 205 | return handleResult(newParser().parseResource(classLoader, path, encoding)); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Parses Java code from a Reader and returns a |
| 210 | * {@link CompilationUnit} that represents it.<br> |
| 211 | * |
| 212 | * @param reader the reader containing Java source code. It will be closed after parsing. |
| 213 | * @return CompilationUnit representing the Java source code |
| 214 | * @throws ParseProblemException if the source code has parser errors |
| 215 | */ |
| 216 | public static CompilationUnit parse(final Reader reader) { |
| 217 | return handleResult(newParser().parse(reader)); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Parses the Java code contained in code and returns a |
| 222 | * {@link CompilationUnit} that represents it. |
| 223 | * |
| 224 | * @param code Java source code |
| 225 | * @return CompilationUnit representing the Java source code |
| 226 | * @throws ParseProblemException if the source code has parser errors |
| 227 | */ |
| 228 | public static CompilationUnit parse(String code) { |
| 229 | return handleResult(newParser().parse(code)); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Parses the Java block contained in a {@link String} and returns a |
| 234 | * {@link BlockStmt} that represents it. |
| 235 | * |
| 236 | * @param blockStatement {@link String} containing Java block code |
| 237 | * @return BlockStmt representing the Java block |
| 238 | * @throws ParseProblemException if the source code has parser errors |
| 239 | */ |
| 240 | public static BlockStmt parseBlock(final String blockStatement) { |
| 241 | return handleResult(newParser().parseBlock(blockStatement)); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Parses the Java statement contained in a {@link String} and returns a |
| 246 | * {@link Statement} that represents it. |
| 247 | * |
| 248 | * @param statement {@link String} containing Java statement code |
| 249 | * @return Statement representing the Java statement |
| 250 | * @throws ParseProblemException if the source code has parser errors |
| 251 | */ |
| 252 | public static Statement parseStatement(final String statement) { |
| 253 | return handleResult(newParser().parseStatement(statement)); |
| 254 | } |
| 255 | |
| 256 | private static <T extends Node> T handleResult(ParseResult<T> result) { |
| 257 | if (result.isSuccessful()) { |
| 258 | return result.getResult().get(); |
| 259 | } |
| 260 | throw new ParseProblemException(result.getProblems()); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Parses the Java import contained in a {@link String} and returns a |
| 265 | * {@link ImportDeclaration} that represents it. |
| 266 | * |
| 267 | * @param importDeclaration {@link String} containing Java import code |
| 268 | * @return ImportDeclaration representing the Java import declaration |
| 269 | * @throws ParseProblemException if the source code has parser errors |
| 270 | */ |
| 271 | public static ImportDeclaration parseImport(final String importDeclaration) { |
| 272 | return handleResult(newParser().parseImport(importDeclaration)); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Parses the Java expression contained in a {@link String} and returns a |
| 277 | * {@link Expression} that represents it. |
| 278 | * |
| 279 | * @param expression {@link String} containing Java expression |
| 280 | * @return Expression representing the Java expression |
| 281 | * @throws ParseProblemException if the source code has parser errors |
| 282 | */ |
| 283 | public static <T extends Expression> T parseExpression(final String expression) { |
| 284 | return handleResult(newParser().parseExpression(expression)); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Parses the Java annotation contained in a {@link String} and returns a |
| 289 | * {@link AnnotationExpr} that represents it. |
| 290 | * |
| 291 | * @param annotation {@link String} containing Java annotation |
| 292 | * @return AnnotationExpr representing the Java annotation |
| 293 | * @throws ParseProblemException if the source code has parser errors |
| 294 | */ |
| 295 | public static AnnotationExpr parseAnnotation(final String annotation) { |
| 296 | return handleResult(newParser().parseAnnotation(annotation)); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Parses the Java annotation body declaration(e.g fields or methods) contained in a |
| 301 | * {@link String} and returns a {@link BodyDeclaration} that represents it. |
| 302 | * |
| 303 | * @param body {@link String} containing Java body declaration |
| 304 | * @return BodyDeclaration representing the Java annotation |
| 305 | * @throws ParseProblemException if the source code has parser errors |
| 306 | */ |
| 307 | public static BodyDeclaration<?> parseAnnotationBodyDeclaration(final String body) { |
| 308 | return handleResult(newParser().parseAnnotationBodyDeclaration(body)); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Parses a Java class or interface body declaration(e.g fields or methods) and returns a |
| 313 | * {@link BodyDeclaration} that represents it. |
| 314 | * |
| 315 | * @param body the body of a class or interface |
| 316 | * @return BodyDeclaration representing the Java interface body |
| 317 | * @throws ParseProblemException if the source code has parser errors |
| 318 | */ |
| 319 | public static BodyDeclaration<?> parseBodyDeclaration(String body) { |
| 320 | return handleResult(newParser().parseBodyDeclaration(body)); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Parses a Java class or interface type name and returns a {@link ClassOrInterfaceType} that represents it. |
| 325 | * |
| 326 | * @param type the type name like a.b.c.X or Y |
| 327 | * @return ClassOrInterfaceType representing the type |
| 328 | * @throws ParseProblemException if the source code has parser errors |
| 329 | */ |
| 330 | public static ClassOrInterfaceType parseClassOrInterfaceType(String type) { |
| 331 | return handleResult(newParser().parseClassOrInterfaceType(type)); |
| 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Parses a Java type name and returns a {@link Type} that represents it. |
| 336 | * |
| 337 | * @param type the type name like a.b.c.X, Y, or int |
| 338 | * @return ClassOrInterfaceType representing the type |
| 339 | * @throws ParseProblemException if the source code has parser errors |
| 340 | */ |
| 341 | public static Type parseType(String type) { |
| 342 | return handleResult(newParser().parseType(type)); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Parses a variable declaration expression and returns a {@link VariableDeclarationExpr} |
| 347 | * that represents it. |
| 348 | * |
| 349 | * @param declaration a variable declaration like {@code int x=2;} |
| 350 | * @return VariableDeclarationExpr representing the type |
| 351 | * @throws ParseProblemException if the source code has parser errors |
| 352 | */ |
| 353 | public static VariableDeclarationExpr parseVariableDeclarationExpr(String declaration) { |
| 354 | return handleResult(newParser().parseVariableDeclarationExpr(declaration)); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Parses the content of a JavadocComment and returns a {@link Javadoc} that |
| 359 | * represents it. |
| 360 | * |
| 361 | * @param content a variable declaration like {@code content of my javadoc\n * second line\n * third line} |
| 362 | * @return Javadoc representing the content of the comment |
| 363 | * @throws ParseProblemException if the source code has parser errors |
| 364 | */ |
| 365 | public static Javadoc parseJavadoc(String content) { |
| 366 | return JavadocParser.parse(content); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Parses the this(...) and super(...) statements that may occur at the start of a constructor. |
| 371 | * |
| 372 | * @param statement a statement like super("hello"); |
| 373 | * @return the AST for the statement. |
| 374 | * @throws ParseProblemException if the source code has parser errors |
| 375 | */ |
| 376 | public static ExplicitConstructorInvocationStmt parseExplicitConstructorInvocationStmt(String statement) { |
| 377 | return handleResult(newParser().parseExplicitConstructorInvocationStmt(statement)); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Parses a qualified name (one that can have "."s in it) and returns it as a Name. |
| 382 | * |
| 383 | * @param qualifiedName a name like "com.laamella.parameter_source" |
| 384 | * @return the AST for the name |
| 385 | * @throws ParseProblemException if the source code has parser errors |
| 386 | */ |
| 387 | public static Name parseName(String qualifiedName) { |
| 388 | return handleResult(newParser().parseName(qualifiedName)); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Parses a simple name (one that can NOT have "."s in it) and returns it as a SimpleName. |
| 393 | * |
| 394 | * @param name a name like "parameter_source" |
| 395 | * @return the AST for the name |
| 396 | * @throws ParseProblemException if the source code has parser errors |
| 397 | */ |
| 398 | public static SimpleName parseSimpleName(String name) { |
| 399 | return handleResult(newParser().parseSimpleName(name)); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Parses a single parameter (a type and a name) and returns it as a Parameter. |
| 404 | * |
| 405 | * @param parameter a parameter like "int[] x" |
| 406 | * @return the AST for the parameter |
| 407 | * @throws ParseProblemException if the source code has parser errors |
| 408 | */ |
| 409 | public static Parameter parseParameter(String parameter) { |
| 410 | return handleResult(newParser().parseParameter(parameter)); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Parses a package declaration and returns it as a PackageDeclaration. |
| 415 | * |
| 416 | * @param packageDeclaration a declaration like "package com.microsoft.java;" |
| 417 | * @return the AST for the parameter |
| 418 | * @throws ParseProblemException if the source code has parser errors |
| 419 | */ |
| 420 | public static PackageDeclaration parsePackageDeclaration(String packageDeclaration) { |
| 421 | return handleResult(newParser().parsePackageDeclaration(packageDeclaration)); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Parses a type declaration and returns it as a TypeDeclaration. |
| 426 | * |
| 427 | * @param typeDeclaration a declaration like "class X {}" |
| 428 | * @return the AST for the type declaration |
| 429 | * @throws ParseProblemException if the source code has parser errors |
| 430 | */ |
| 431 | public static TypeDeclaration<?> parseTypeDeclaration(String typeDeclaration) { |
| 432 | return handleResult(newParser().parseTypeDeclaration(typeDeclaration)); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Parses a module declaration and returns it as a ModuleDeclaration. |
| 437 | * |
| 438 | * @param moduleDeclaration a declaration like "module X {}" |
| 439 | * @return the AST for the module declaration |
| 440 | * @throws ParseProblemException if the source code has parser errors |
| 441 | * @see ModuleDeclaration |
| 442 | */ |
| 443 | public static ModuleDeclaration parseModuleDeclaration(String moduleDeclaration) { |
| 444 | return handleResult(newParser().parseModuleDeclaration(moduleDeclaration)); |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Parses a module directive and returns it as a ModuleDirective. |
| 449 | * |
| 450 | * @param moduleDirective a directive like "opens C;" |
| 451 | * @return the AST for the module directive |
| 452 | * @throws ParseProblemException if the source code has parser errors |
| 453 | * @see ModuleDirective |
| 454 | */ |
| 455 | public static ModuleDirective parseModuleDirective(String moduleDirective) { |
| 456 | return handleResult(newParser().parseModuleDirective(moduleDirective)); |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /** |
| 461 | * Parses a type parameter and returns it as a TypeParameter |
| 462 | * |
| 463 | * @param typeParameter a parameter like "T extends Serializable" |
| 464 | * @return the AST for the type parameter |
| 465 | * @throws ParseProblemException if the source code has parser errors |
| 466 | */ |
| 467 | public static TypeParameter parseTypeParameter(String typeParameter) { |
| 468 | return handleResult(newParser().parseTypeParameter(typeParameter)); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Parses a method declaration and returns it as a MethodDeclaration. |
| 473 | * |
| 474 | * @param methodDeclaration a method declaration like "void foo() {}" |
| 475 | * @return the AST for the method declaration |
| 476 | * @throws ParseProblemException if the source code has parser errors |
| 477 | * @see MethodDeclaration |
| 478 | */ |
| 479 | public static MethodDeclaration parseMethodDeclaration(String methodDeclaration) { |
| 480 | return handleResult(newParser().parseMethodDeclaration(methodDeclaration)); |
| 481 | } |
| 482 | |
| 483 | } |
| 484 |
Members