Skip to content

Commit dbac5c0

Browse files
committed
first commit
0 parents  commit dbac5c0

13 files changed

+411
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
lib
2+
bin
3+
.manager
4+
target
5+
.settings
6+
*~
7+
/target
8+
.project
9+
.classpath
10+
.idea/
11+
*.iml
12+
release.properties

pom.xml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.github.javaparser</groupId>
8+
<artifactId>javaparser-book</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>1.8</source>
17+
<target>1.8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>com.github.javaparser</groupId>
26+
<artifactId>javaparser-core</artifactId>
27+
<version>3.0.0-RC.3</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>junit</groupId>
31+
<artifactId>junit</artifactId>
32+
<version>4.12</version>
33+
</dependency>
34+
</dependencies>
35+
36+
</project>

src/.DS_Store

8 KB
Binary file not shown.

src/main/.DS_Store

8 KB
Binary file not shown.

src/main/java/.DS_Store

8 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.javaparser.examples;
2+
3+
import com.github.javaparser.JavaParser;
4+
import com.github.javaparser.ast.CompilationUnit;
5+
import com.github.javaparser.ast.body.MethodDeclaration;
6+
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
7+
8+
import java.io.FileInputStream;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.regex.Pattern;
12+
13+
public class CommentGenerator {
14+
15+
private static final String FILE_PATH = "src/main/java/org/javaparser/examples/ReversePolishNotation.java";
16+
17+
private static final Pattern FIND_UPPERCASE = Pattern.compile("(.)(\\p{Upper})");
18+
19+
public static void main(String[] args) throws Exception {
20+
21+
CompilationUnit cu = JavaParser.parse(new FileInputStream(FILE_PATH));
22+
23+
List<MethodDeclaration> methodDeclarations = new ArrayList<>();
24+
VoidVisitorAdapter<List<MethodDeclaration>> unDocumentedMethodCollector = new UnDocumentedMethodCollector();
25+
unDocumentedMethodCollector.visit(cu, methodDeclarations);
26+
27+
methodDeclarations.forEach(md -> md.setJavaDocComment(generateJavaDoc(md)));
28+
29+
System.out.println(cu.toString());
30+
}
31+
32+
private static class UnDocumentedMethodCollector extends VoidVisitorAdapter<List<MethodDeclaration>> {
33+
34+
@Override
35+
public void visit(MethodDeclaration md, List<MethodDeclaration> collector) {
36+
super.visit(md, collector);
37+
if(md.getJavaDoc() != null) {
38+
collector.add(md);
39+
}
40+
}
41+
}
42+
43+
private static String generateJavaDoc(MethodDeclaration md){
44+
return " " + camelCaseToTitleFormat(md.getNameAsString()) + " ";
45+
}
46+
47+
private static String camelCaseToTitleFormat(String text){
48+
String split = FIND_UPPERCASE.matcher(text).replaceAll("$1 $2");
49+
return split.substring(0,1).toUpperCase() + split.substring(1);
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.javaparser.examples;
2+
3+
import com.github.javaparser.JavaParser;
4+
import com.github.javaparser.ast.CompilationUnit;
5+
import com.github.javaparser.ast.Node;
6+
import com.github.javaparser.ast.comments.Comment;
7+
import com.github.javaparser.ast.comments.LineComment;
8+
9+
import java.io.FileInputStream;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
13+
public class CommentRemover {
14+
15+
private static final String FILE_PATH = "src/main/java/org/javaparser/examples/ReversePolishNotation.java";
16+
17+
public static void main(String[] args) throws Exception {
18+
19+
CompilationUnit cu = JavaParser.parse(new FileInputStream(FILE_PATH));
20+
21+
List<Comment> comments = cu.getAllContainedComments();
22+
List<Comment> unwantedComments = comments
23+
.stream()
24+
.filter(p -> !p.getCommentedNode().isPresent() || p instanceof LineComment)
25+
.collect(Collectors.toList());
26+
unwantedComments.forEach(Node::remove);
27+
28+
System.out.println(cu.toString());
29+
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.javaparser.examples;
2+
3+
import com.github.javaparser.JavaParser;
4+
import com.github.javaparser.ast.CompilationUnit;
5+
6+
import java.io.FileInputStream;
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
public class CommentReporter {
11+
12+
private static final String FILE_PATH = "src/main/java/org/javaparser/examples/ReversePolishNotation.java";
13+
14+
public static void main(String[] args) throws Exception {
15+
16+
CompilationUnit cu = JavaParser.parse(new FileInputStream(FILE_PATH));
17+
18+
List<CommentReportEntry> comments = cu.getAllContainedComments()
19+
.stream()
20+
.map(p -> new CommentReportEntry(p.getClass().getSimpleName(),
21+
p.getContent(),
22+
p.getRange().get().begin.line,
23+
!p.getCommentedNode().isPresent()))
24+
.collect(Collectors.toList());
25+
26+
comments.forEach(System.out::println);
27+
}
28+
29+
30+
private static class CommentReportEntry {
31+
private String type;
32+
private String text;
33+
private int lineNumber;
34+
private boolean isOrphan;
35+
36+
CommentReportEntry(String type, String text, int lineNumber, boolean isOrphan) {
37+
this.type = type;
38+
this.text = text;
39+
this.lineNumber = lineNumber;
40+
this.isOrphan = isOrphan;
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return lineNumber + "|" + type + "|" + isOrphan + "|" + text.replaceAll("\\n","").trim();
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.javaparser.examples;
2+
3+
import com.github.javaparser.JavaParser;
4+
import com.github.javaparser.ast.CompilationUnit;
5+
import com.github.javaparser.ast.body.FieldDeclaration;
6+
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
7+
import com.github.javaparser.ast.visitor.ModifierVisitor;
8+
9+
import java.io.FileInputStream;
10+
import java.util.regex.Pattern;
11+
12+
public class ModifyingVisitorExample {
13+
14+
private static final String FILE_PATH = "src/main/java/org/javaparser/examples/ReversePolishNotation.java";
15+
16+
private static final Pattern LOOK_AHEAD_THREE = Pattern.compile("(\\d)(?=(\\d{3})+$)");
17+
18+
public static void main(String[] args) throws Exception {
19+
20+
CompilationUnit cu = JavaParser.parse(new FileInputStream(FILE_PATH));
21+
22+
ModifierVisitor<?> numericLiteralVisitor = new IntegerLiteralModifier();
23+
numericLiteralVisitor.visit(cu, null);
24+
25+
System.out.println(cu.toString());
26+
}
27+
28+
private static class IntegerLiteralModifier extends ModifierVisitor<Void> {
29+
30+
@Override
31+
public FieldDeclaration visit(FieldDeclaration fd, Void arg) {
32+
super.visit(fd, arg);
33+
fd.getVariables().forEach(v ->
34+
v.getInitializer().ifPresent(i -> {
35+
if (i instanceof IntegerLiteralExpr) {
36+
v.setInitializer(formatWithUnderscores(((IntegerLiteralExpr) i).getValue()));
37+
}
38+
}));
39+
return fd;
40+
}
41+
}
42+
43+
static String formatWithUnderscores(String value) {
44+
String withoutUnderscores = value.replaceAll("_", "");
45+
return LOOK_AHEAD_THREE.matcher(withoutUnderscores).replaceAll("$1_");
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.javaparser.examples;
2+
3+
import java.util.Stack;
4+
import java.util.stream.Stream;
5+
6+
7+
/**
8+
* A Simple Reverse Polish Notation calculator with memory function.
9+
*/
10+
public class ReversePolishNotation {
11+
12+
// What does this do?
13+
public static int ONE_BILLION = 1000000000;
14+
15+
private double memory = 0;
16+
17+
/**
18+
* Takes reverse polish notation style string and returns the resulting calculation.
19+
*
20+
* @param input mathematical expression in the reverse Polish notation format
21+
* @return the calculation result
22+
*/
23+
public Double calc(String input) {
24+
25+
String[] tokens = input.split(" ");
26+
Stack<Double> numbers = new Stack<>();
27+
28+
Stream.of(tokens).forEach(t -> {
29+
double a;
30+
double b;
31+
switch(t){
32+
case "+":
33+
b = numbers.pop();
34+
a = numbers.pop();
35+
numbers.push(a + b);
36+
break;
37+
case "/":
38+
b = numbers.pop();
39+
a = numbers.pop();
40+
numbers.push(a / b);
41+
break;
42+
case "-":
43+
b = numbers.pop();
44+
a = numbers.pop();
45+
numbers.push(a - b);
46+
break;
47+
case "*":
48+
b = numbers.pop();
49+
a = numbers.pop();
50+
numbers.push(a * b);
51+
break;
52+
default:
53+
numbers.push(Double.valueOf(t));
54+
}
55+
});
56+
return numbers.pop();
57+
}
58+
59+
/**
60+
* Memory Recall uses the number in stored memory, defaulting to 0.
61+
*
62+
* @return the double
63+
*/
64+
public double memoryRecall(){
65+
return memory;
66+
}
67+
68+
/**
69+
* Memory Clear sets the memory to 0.
70+
*/
71+
public void memoryClear(){
72+
memory = 0;
73+
}
74+
75+
76+
public void memoryStore(double value){
77+
memory = value;
78+
}
79+
80+
}
81+
/* EOF */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.javaparser.examples;
2+
3+
import java.time.LocalDateTime;
4+
5+
public class TimePrinter {
6+
7+
public static void main(String args[]){
8+
System.out.print(LocalDateTime.now());
9+
}
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.javaparser.examples;
2+
3+
import com.github.javaparser.JavaParser;
4+
import com.github.javaparser.ast.CompilationUnit;
5+
import com.github.javaparser.ast.body.MethodDeclaration;
6+
import com.github.javaparser.ast.visitor.VoidVisitor;
7+
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
8+
9+
import java.io.FileInputStream;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class VoidVisitorExample {
14+
15+
private static final String FILE_PATH = "src/main/java/org/javaparser/examples/ReversePolishNotation.java";
16+
17+
public static void main(String[] args) throws Exception {
18+
19+
CompilationUnit cu = JavaParser.parse(new FileInputStream(FILE_PATH));
20+
21+
VoidVisitor<?> methodNameVisitor = new MethodNamePrinter();
22+
methodNameVisitor.visit(cu, null);
23+
List<String> methodNames = new ArrayList<>();
24+
VoidVisitor<List<String>> methodNameCollector = new MethodNameCollector();
25+
methodNameCollector.visit(cu, methodNames);
26+
methodNames.forEach(n -> System.out.println("Method Name Collected: " + n));
27+
28+
}
29+
30+
private static class MethodNamePrinter extends VoidVisitorAdapter<Void> {
31+
32+
@Override
33+
public void visit(MethodDeclaration md, Void arg) {
34+
super.visit(md, arg);
35+
System.out.println("Method Name Printed: " + md.getName());
36+
}
37+
}
38+
39+
private static class MethodNameCollector extends VoidVisitorAdapter<List<String>> {
40+
41+
@Override
42+
public void visit(MethodDeclaration md, List<String> collector) {
43+
super.visit(md, collector);
44+
collector.add(md.getNameAsString());
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)