Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation adding and editing #5490

Closed
wants to merge 10 commits into from
35 changes: 35 additions & 0 deletions src/main/java/spoon/reflect/visitor/AccessibleVariablesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,38 @@ public AccessibleVariablesFinder(CtElement expression) {
this.expression = expression;
}

/**
* Finds and returns a list of CtVariable objects associated with the parent of the expression.
* If the parent of the expression is not initialized, it returns an empty list.
* @return List of CtVariable objects if the parent of the expression is initialized, otherwise an empty list.
*/
public List<CtVariable> find() {
if (expression.isParentInitialized()) {
return getVariable(expression.getParent());
}
return Collections.emptyList();
}

/**
* This method retrieves a list of variables from a given parent CtElement instance.
* @param parent The parent element from which to retrieve variables.
* @return A list of variables found within the parent element.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* This method retrieves a list of variables from a given parent CtElement instance.
* @param parent The parent element from which to retrieve variables.
* @return A list of variables found within the parent element.
*/

We can skip documentation for private methods.

private List<CtVariable> getVariable(final CtElement parent) {
final List<CtVariable> variables = new ArrayList<>();
if (parent == null) {
return variables;
}

/*
* This class scans for variables within a given element.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*
* This class scans for variables within a given element.
*/

We can skip for package-private class as well.

class VariableScanner extends CtInheritanceScanner {

/**
* This method visits a list of statements and adds any variables it finds to the list.
* @param e The list of statements to visit.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* This method visits a list of statements and adds any variables it finds to the list.
* @param e The list of statements to visit.
*/

I think we can remove all the docstring in the overridden methods in this class as they are not part of the public API. They are just public.

Copy link
Author

@TheMarvelFan TheMarvelFan Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. Thanks for pointing out these corrections. I will rectify these and add new javadocs based your suggestions.

@Override
public void visitCtStatementList(CtStatementList e) {
for (int i = 0; i < e.getStatements().size(); i++) {
Expand All @@ -72,6 +91,10 @@ public void visitCtStatementList(CtStatementList e) {
super.visitCtStatementList(e);
}

/**
* This method scans CtType instance and adds any fields it finds to the list.
* @param type The type to scan.
*/
@Override
public <T> void scanCtType(CtType<T> type) {
List<CtField<?>> fields = type.getFields();
Expand All @@ -98,6 +121,10 @@ public <T> void scanCtType(CtType<T> type) {
super.scanCtType(type);
}

/**
* This method visits a try-with-resource statement and adds any resources it finds to the list.
* @param e The try-with-resource statement to visit.
*/
@Override
public void visitCtTryWithResource(CtTryWithResource e) {
for (CtResource<?> resource: e.getResources()) {
Expand All @@ -108,12 +135,20 @@ public void visitCtTryWithResource(CtTryWithResource e) {
super.visitCtTryWithResource(e);
}

/**
* This method scans an executable and adds any parameters it finds to the list.
* @param e The executable to scan.
*/
@Override
public void scanCtExecutable(CtExecutable e) {
variables.addAll(e.getParameters());
super.scanCtExecutable(e);
}

/**
* This method visits a for loop and scans its initialization statements for variables.
* @param e The for loop to visit.
*/
@Override
public void visitCtFor(CtFor e) {
for (CtStatement ctStatement : e.getForInit()) {
Expand Down
60 changes: 57 additions & 3 deletions src/main/java/spoon/reflect/visitor/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
public class PrinterHelper {
/**
* Line separator which is used by the printer helper.
* By default the system line separator is used
* By default, the system line separator is used
*/
private String lineSeparator = System.getProperty("line.separator");

Expand Down Expand Up @@ -85,6 +85,8 @@ public void reset() {

/**
* Outputs a string.
* @param s String to printed in output
* @return current object of PrintHelper class
*/
public PrinterHelper write(String s) {
if (s != null) {
Expand All @@ -98,6 +100,8 @@ public PrinterHelper write(String s) {

/**
* Outputs a char.
* @param c Character to be printed in output
* @return current object of PrinterHelper class
*/
public PrinterHelper write(char c) {
if (c == '\r') {
Expand Down Expand Up @@ -129,12 +133,16 @@ public PrinterHelper write(char c) {

/**
* Generates a new line.
* @return current object of PrinterHelper class
*/
public PrinterHelper writeln() {
write(lineSeparator);
return this;
}

/**
* Appends tabs or spaces to a StringBuilder based on the environment settings.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Appends tabs or spaces to a StringBuilder based on the environment settings.
*/

private void writeTabsInternal() {
for (int i = 0; i < nbTabs; i++) {
if (env != null && env.isUsingTabulations()) {
Expand All @@ -149,6 +157,10 @@ private void writeTabsInternal() {
}
}

/**
* Writes tabs if the condition is met.
* This method checks if tabs should be written, and if so, it writes them and resets the flag.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Writes tabs if the condition is met.
* This method checks if tabs should be written, and if so, it writes them and resets the flag.
*/

protected void autoWriteTabs() {
if (shouldWriteTabs) {
writeTabsInternal();
Expand All @@ -158,6 +170,7 @@ protected void autoWriteTabs() {

/**
* Increments the current number of tabs.
* @return current object of PrinterHelper class
*/
public PrinterHelper incTab() {
nbTabs++;
Expand All @@ -166,6 +179,7 @@ public PrinterHelper incTab() {

/**
* Decrements the current number of tabs.
* @return current object of PrinterHelper class
*/
public PrinterHelper decTab() {
nbTabs--;
Expand All @@ -181,12 +195,17 @@ public int getTabCount() {

/**
* Sets the current number of tabs.
* @return current object of PrinterHelper class
*/
public PrinterHelper setTabCount(int tabCount) {
nbTabs = tabCount;
return this;
}

/**
* Removes the last line from the buffer if it matches the line separator.
* @return true if the line was removed, false otherwise.
*/
public boolean removeLine() {
String ls = lineSeparator;
int i = sbf.length() - ls.length();
Expand All @@ -207,11 +226,18 @@ public boolean removeLine() {
return true;
}

/**
* @param c Character that needs to be checked for being space/line-break character
* @return true if character is space/line-break character
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @param c Character that needs to be checked for being space/line-break character
* @return true if character is space/line-break character
*/

private boolean isWhite(char c) {
return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
}

/** writes as many newlines as needed to align the line number again between the element position and the current line number */
/** writes as many newlines as needed to align the line number again between the element position and the current line number
* @param e The CtElement whose start position is to be adjusted. It should not be implicit and should have a valid position.
* @return The current instance of PrinterHelper with the adjusted start position.
*/
public PrinterHelper adjustStartPosition(CtElement e) {
if (!e.isImplicit() && e.getPosition().isValidPosition()) {
// we should add some lines
Expand Down Expand Up @@ -243,12 +269,24 @@ public PrinterHelper adjustEndPosition(CtElement e) {
return this;
}

/**
* Undefines the current line number mapping.
* If the current line number mapping is null, it sets the line number mapping to 0.
*/
public void undefineLine() {
if (lineNumberMapping.get(line) == null) {
putLineNumberMapping(0);
}
}

/**
* Maps the line number of a given CtElement to the current line number.
* If the position of the CtElement is not valid, or it does not belong to the expected compilation unit, or it is a partial source position,
* it undefines the current line number.
*
* @param e The CtElement whose line number is to be mapped.
* @param unitExpected The expected compilation unit of the CtElement.
*/
public void mapLine(CtElement e, CtCompilationUnit unitExpected) {
SourcePosition sp = e.getPosition();
if ((sp.isValidPosition())
Expand All @@ -261,14 +299,26 @@ public void mapLine(CtElement e, CtCompilationUnit unitExpected) {
}
}

/**
* Maps a given line number to the current line number.
* @param valueLine The line number to be mapped to the current line.
*/
public void putLineNumberMapping(int valueLine) {
lineNumberMapping.put(this.line, valueLine);
}

/**
* Retrieves the current line number mapping as an unmodifiable map.
* @return An unmodifiable map of the current line number mapping.
*/
public Map<Integer, Integer> getLineNumberMapping() {
return Collections.unmodifiableMap(lineNumberMapping);
}

/**
* Converts the content of sbf StringBuilder in current object to a string representation.
* @return String that contains content stored in sbf for current object of PrinterHelper class
*/
@Override
public String toString() {
return sbf.toString();
Expand All @@ -284,7 +334,7 @@ public String getLineSeparator() {

/**
* @param lineSeparator characters which will be printed as End of line.
* By default there is System.getProperty("line.separator")
* By default, there is System.getProperty("line.separator")
*/
public void setLineSeparator(String lineSeparator) {
this.lineSeparator = lineSeparator;
Expand All @@ -295,6 +345,10 @@ public void writeSpace() {
this.write(' ');
}

/**
* Sets the flag indicating whether to write tabs.
* @param b The boolean value to set. If true, tabs will be written; if false, they won't be.
*/
public void setShouldWriteTabs(boolean b) {
this.shouldWriteTabs = b;
}
Expand Down