Skip to content

Commit fa5fd69

Browse files
authored
Merge pull request #4 from javaparser/article_code_formatting
2 parents 969f16f + 07e3171 commit fa5fd69

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

_posts/2018-03-03-the-quick-and-the-full-api-of-javaparser.markdown

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,31 @@ Lost in a dark corner of the JavaParser site is the idea behind the two API's of
1515

1616
This API was built to make common use cases as painless as possible. In the end, this API is a collection of shortcuts to the full API.
1717

18+
```java
1819
import static com.github.javaparser.StaticJavaParser.*;
19-
...
20+
// ...
2021
CompilationUnit cu = parse("class X{}");
22+
```
2123

2224
* The quick API consists of all the static methods on the `StaticJavaParser` class.
2325
* All these methods throw an unchecked `ParseProblemException` if anything goes wrong. This exception contains a list of the problem(s) encountered.
2426
* The static methods called `parse` and `parseResource` offer various ways of reading Java code for parsing. They all expect a full Java file. So, even though the example parses a `String`, you could pass an `InputStream`, a `File` or various other inputs.
2527
* The remaining static methods parse a fragment of source code in a `String`. Here is one that parses just an expression:
26-
28+
```java
2729
Expression e = parseExpression("1+1");
30+
```
2831

2932
* `parseJavadoc` is a special case. Comments normally remaing unparsed, including Javadoc comments. This method is a separate parser for a `JavadocComment` node.
3033
* Changing configuration is done by modifying the `staticConfiguration` field.
31-
34+
```java
3235
StaticJavaParser.getConfiguration().setAttributeComments(false);
36+
```
3337

3438
### Full!
3539

3640
This API was built to give access to all flexibility there is, and to provide a little more performance.
3741

42+
```java
3843
import static com.github.javaparser.ParseStart.*;
3944
import static com.github.javaparser.Providers.provider;
4045
...
@@ -43,15 +48,18 @@ This API was built to give access to all flexibility there is, and to provide a
4348
result.ifSuccessful(cu ->
4449
// use cu
4550
);
51+
```
4652

4753
or, thanks to the fake builder pattern:
4854

55+
```java
4956
import static com.github.javaparser.ParseStart.*;
5057
import static com.github.javaparser.Providers.provider;
5158
...
5259
new JavaParser().parse(COMPILATION_UNIT, provider("class X{}")).ifSuccessful(cu ->
5360
System.out.println(cu)
5461
);
62+
```
5563

5664
* The full API consists of the `JavaParser` constructors, and the whole suite of parse methods, with one extra - the one that does the actual parsing work.
5765
* _Never_ does it throw an exception. `ParseResult` can tell you if parsing went fine, and if not what problems were encountered.
@@ -63,7 +71,7 @@ or, thanks to the fake builder pattern:
6371
* Parsing Javadoc is an exception again. You need the `JavadocParser` for that.
6472
* Configuration can be passed in the constructor.
6573

66-
74+
```java
6775
ParserConfiguration configuration = new ParserConfiguration();
6876
JavaParser parser = new JavaParser(configuration);
6977
ParseResult parseResult = parser.parse(EXPRESSION, provider("1+1"));
@@ -74,4 +82,5 @@ or, thanks to the fake builder pattern:
7482
parseResult.getResult().ifPresent(System.out::println);
7583
if (parseResult.getCommentsCollection().isPresent()) {
7684
// ...
77-
}
85+
}
86+
```

0 commit comments

Comments
 (0)