Skip to content

Commit 81c5519

Browse files
docs: add JavaDocs for parsing-related classes
1 parent b278870 commit 81c5519

7 files changed

Lines changed: 168 additions & 6 deletions

File tree

src/main/java/de/hub/mse/variantsync/boosting/ecco/Association.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public int hashCode() {
8181
/**
8282
* Returns the set of AST nodes contained in this EccoSet.
8383
*
84-
* @return EccoSet<EccoNode> - a set of EccoNode objects representing the AST
84+
* @return EccoSet<ASTNode> - a set of ASTNode objects representing the AST
8585
* nodes
8686
*/
8787
public EccoSet<ASTNode> getAstNodes() {

src/main/java/de/hub/mse/variantsync/boosting/parsing/AbstractAST.java

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,39 @@
1212
import java.util.HashSet;
1313
import java.util.Set;
1414

15+
/**
16+
* AbstractAST is an abstract class representing an Abstract Syntax Tree (AST)
17+
* with a root node and a set of AST nodes.
18+
* This class provides constructors to create an AST with optional filtering
19+
* based on file types.
20+
*
21+
* @param root the root node of the AST
22+
* @param astNodes the set of AST nodes in the tree
23+
* @param fileTypes a set of file types to filter the AST nodes by
24+
*/
1525
public abstract class AbstractAST implements Serializable {
1626
protected final ASTNode root;
1727
protected final EccoSet<ASTNode> astNodes;
18-
// Used for filtering; no filtering is applied if the set is empty
1928
protected final Set<String> fileTypes;
2029

30+
/**
31+
* Constructs an AbstractAST with no filtering based on file types.
32+
*
33+
* @param fileTypes an array of file types to filter the AST nodes by
34+
*/
2135
public AbstractAST(final String... fileTypes) {
2236
this.fileTypes = new HashSet<>();
2337
Collections.addAll(this.fileTypes, fileTypes);
2438
root = new ASTNode(null, null, RootPosition.INSTANCE, ASTNode.NODE_TYPE.ROOT, null);
2539
astNodes = new EccoSet<>();
2640
}
2741

42+
/**
43+
* Constructs an AbstractAST with filtering based on file types.
44+
*
45+
* @param rootFile the root file to build the AST from
46+
* @param fileTypes an array of file types to filter the AST nodes by
47+
*/
2848
public AbstractAST(final File rootFile, final String... fileTypes) {
2949
this.fileTypes = new HashSet<>();
3050
Collections.addAll(this.fileTypes, fileTypes);
@@ -33,6 +53,14 @@ public AbstractAST(final File rootFile, final String... fileTypes) {
3353
astNodes = collectAstNodes();
3454
}
3555

56+
/**
57+
* Constructs an AbstractAST with a specified root node, set of AST nodes, and
58+
* optional filtering based on file types.
59+
*
60+
* @param root the root node of the AST
61+
* @param astNodes the set of AST nodes in the tree
62+
* @param fileTypes an array of file types to filter the AST nodes by
63+
*/
3664
public AbstractAST(final ASTNode root, final EccoSet<ASTNode> astNodes,
3765
final String... fileTypes) {
3866
this.fileTypes = new HashSet<>();
@@ -41,8 +69,12 @@ public AbstractAST(final ASTNode root, final EccoSet<ASTNode> astNodes,
4169
this.astNodes = astNodes;
4270
}
4371

44-
// collects all nodes (except the root node) of the AST in one set to simplify
45-
// their access
72+
/**
73+
* Collects all nodes (except the root node) of the Abstract Syntax Tree (AST)
74+
* in one set to simplify their access.
75+
*
76+
* @return EccoSet<ASTNode> - a set containing all nodes of the AST
77+
*/
4678
public EccoSet<ASTNode> collectAstNodes() {
4779
final EccoSet<ASTNode> result = new EccoSet<>();
4880
final ArrayList<ASTNode> nodesToVisit = new ArrayList<>();
@@ -55,7 +87,12 @@ public EccoSet<ASTNode> collectAstNodes() {
5587
return result;
5688
}
5789

58-
// create the AST from a file
90+
/**
91+
* Creates the AST from a file.
92+
*
93+
* @param parent - the parent ASTNode to which the file belongs
94+
* @param parentFile - the File object representing the parent directory
95+
*/
5996
private void visitFile(final ASTNode parent, final File parentFile) {
6097
final File[] children = parentFile.listFiles();
6198
if (children == null)
@@ -83,13 +120,38 @@ private void visitFile(final ASTNode parent, final File parentFile) {
83120
}
84121
}
85122

123+
/**
124+
* This method is responsible for visiting the content of a file represented by
125+
* the given ASTNode and File object.
126+
*
127+
* @param fileNode The ASTNode representing the content of the file to be
128+
* visited.
129+
* @param fileToVisit The File object representing the file to be visited.
130+
*
131+
* @throws NullPointerException if either fileNode or fileToVisit is null.
132+
* @throws IllegalArgumentException if the fileToVisit does not exist or is not
133+
* a valid file.
134+
* @throws IOException if an I/O error occurs while reading the
135+
* file content.
136+
*/
86137
protected abstract void visitFileContent(final ASTNode fileNode, final File fileToVisit);
87138

139+
/**
140+
* Returns the root node of the Abstract Syntax Tree (AST).
141+
*
142+
* @return the root node of the AST
143+
*/
88144
public ASTNode getRoot() {
89145
return root;
90146
}
91147

148+
/**
149+
* Returns a set of all AST nodes in the tree.
150+
*
151+
* @return a set of all AST nodes
152+
*/
92153
public EccoSet<ASTNode> getAstNodes() {
93154
return astNodes;
94155
}
156+
95157
}

src/main/java/de/hub/mse/variantsync/boosting/parsing/CAST.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,41 @@
55
import de.hub.mse.variantsync.boosting.ecco.ASTNode;
66
import de.hub.mse.variantsync.boosting.ecco.EccoSet;
77

8+
/**
9+
* Represents a Syntax Tree (CAST) for C source code files.
10+
*
11+
* This class extends LineAST and provides functionality specific to C source
12+
* code files.
13+
*
14+
* The supported file types for this CAST are ".c" and ".h".
15+
*/
816
public class CAST extends LineAST {
917
private static final String[] fileTypes = new String[] { ".c", ".h" };
1018

19+
/**
20+
* Constructs a new CAST object with default file types ".c" and ".h".
21+
*/
1122
public CAST() {
1223
super(fileTypes);
1324
}
1425

26+
/**
27+
* Constructs a new CAST object with the specified root file and default file
28+
* types ".c" and ".h".
29+
*
30+
* @param rootFile The root file for the CAST.
31+
*/
1532
public CAST(final File rootFile) {
1633
super(rootFile, fileTypes);
1734
}
1835

36+
/**
37+
* Constructs a new CAST object with the specified root node and AST nodes, and
38+
* default file types ".c" and ".h".
39+
*
40+
* @param root The root node for the CAST.
41+
* @param astNodes The set of AST nodes for the CAST.
42+
*/
1943
public CAST(final ASTNode root, final EccoSet<ASTNode> astNodes) {
2044
super(root, astNodes, fileTypes);
2145
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package de.hub.mse.variantsync.boosting.parsing;
22

3+
/**
4+
* Enum representing supported programming languages.
5+
*
6+
* This enum defines the supported programming languages for a specific
7+
* application.
8+
* The supported languages are C, Java, and Lines.
9+
*/
310
public enum ESupportedLanguages {
411
C, JAVA, LINES,
512
}

src/main/java/de/hub/mse/variantsync/boosting/parsing/JavaAST.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
import java.io.File;
1111

12+
/**
13+
* Represents an Abstract Syntax Tree (AST) for Java files.
14+
* This class extends the AbstractAST class and provides specific functionality
15+
* for Java files.
16+
*
17+
*/
1218
public class JavaAST extends AbstractAST {
1319
public JavaAST() {
1420
super(".java");

src/main/java/de/hub/mse/variantsync/boosting/parsing/JavaVisitor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,29 @@
1414

1515
import org.tinylog.Logger;
1616

17+
/**
18+
* This class represents a Java visitor that is used to visit ASTNodes in a Java
19+
* file.
20+
*/
1721
public class JavaVisitor extends VoidVisitorWithDefaults<ASTNode> {
1822
private final String visitedFile;
1923

24+
/**
25+
* Constructs a new JavaVisitor object with the specified visited file.
26+
*
27+
* @param visitedFile The file being visited by the JavaVisitor.
28+
*/
2029
public JavaVisitor(final String visitedFile) {
2130
this.visitedFile = visitedFile;
2231
}
2332

33+
/**
34+
* Parses a javaparser Position object into our custom Position
35+
* object.
36+
*
37+
* @param parserPosition The com.github.javaparser.Position object to be parsed.
38+
* @return A custom Position object representing the parsed position.
39+
*/
2440
public Position parsePosition(final com.github.javaparser.Position parserPosition) {
2541
return new LinePosition(visitedFile, parserPosition.line, parserPosition.column);
2642
}

src/main/java/de/hub/mse/variantsync/boosting/parsing/LineAST.java

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,84 @@
1313
import java.nio.file.Files;
1414
import java.util.List;
1515

16+
/**
17+
* Represents a node in the Abstract Syntax Tree (AST) that represents a line of
18+
* code in a file.
19+
* This class extends AbstractAST and provides functionality to visit the
20+
* content of a file and create
21+
* AST nodes for each line in the file.
22+
*
23+
* The visitFileContent method is overridden to visit the content of a file and
24+
* create AST nodes for each line in the file.
25+
* Each line in the file is represented by an ASTNode with the NODE_TYPE set to
26+
* LINE.
27+
*
28+
* @param fileNode The ASTNode representing the file being visited.
29+
* @param fileToVisit The file to visit and create AST nodes for each line.
30+
* @throws UncheckedIOException If an IOException occurs while reading the file.
31+
*/
1632
public class LineAST extends AbstractAST {
1733
private static final int LINE_BASE_INDEX = 0;
1834

35+
/**
36+
* Constructs a LineAST object with the specified file types.
37+
*
38+
* @param fileTypes the file types to be used for parsing
39+
*/
1940
public LineAST(final String... fileTypes) {
2041
super(fileTypes);
2142
}
2243

44+
/**
45+
* Constructs a LineAST object with the specified root file and file types.
46+
*
47+
* @param rootFile the root file to start parsing from
48+
* @param fileTypes the file types to be used for parsing
49+
*/
2350
public LineAST(final File rootFile, final String... fileTypes) {
2451
super(rootFile, fileTypes);
2552
}
2653

54+
/**
55+
* Constructs a LineAST object with the specified root node, AST nodes, and file
56+
* types.
57+
*
58+
* @param root the root node of the AST
59+
* @param astNodes the set of AST nodes
60+
* @param fileTypes the file types to be used for parsing
61+
*/
2762
public LineAST(final ASTNode root, final EccoSet<ASTNode> astNodes, final String... fileTypes) {
2863
super(root, astNodes, fileTypes);
2964
}
3065

66+
/**
67+
* This method is used to visit the content of a file and create an AST node for
68+
* each line in the file.
69+
*
70+
* @param fileNode The ASTNode representing the file being visited
71+
* @param fileToVisit The File object representing the file to be visited
72+
* @throws UncheckedIOException If an IOException occurs while reading the file
73+
*/
3174
@Override
3275
protected void visitFileContent(final ASTNode fileNode, final File fileToVisit) {
33-
// Create an EccoNode for each line in the file
3476
try {
77+
// Read all lines from the file
3578
final List<String> lines = Files.readAllLines(fileToVisit.toPath());
3679
int lineIndex = LINE_BASE_INDEX;
80+
81+
// Create a node for each line in the file
3782
for (final String line : lines) {
3883
final Position position = new LinePosition(fileToVisit.toString(), lineIndex, 0);
3984
final ASTNode lineNode = new ASTNode(fileNode, line, position, ASTNode.NODE_TYPE.LINE, null);
4085
fileNode.addChild(lineNode);
4186
lineIndex++;
4287
}
4388
} catch (final IOException e) {
89+
// Handle any IOException that occurs
4490
e.printStackTrace();
4591
Logger.error("Was not able to read file " + fileToVisit, e);
4692
throw new UncheckedIOException(e);
4793
}
4894
}
95+
4996
}

0 commit comments

Comments
 (0)