Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,21 @@ You must install the `jython-dev.jar` to your local maven repository.

Build and install to your local Maven repo: `mvn install`

## Code Quality Standards

This project enforces code quality standards during the build process:

### Print Statement Policy

Print statements (`System.out.println`, `System.err.println`) are only allowed in CLI driver classes. The build uses Maven Checkstyle plugin to enforce this policy and will fail if inappropriate print statements are detected in core library code or test classes.

- **To fix violations**: Replace print statements with appropriate logging using `java.util.logging.Logger`
- **To skip check during development**: Use `mvn install -Dskip.print.check=true`
- **For detailed policy**: See [Print Statement Policy](docs/PRINT_STATEMENT_POLICY.md)

### Code Formatting

- **Java**: Uses Spotless with Google Java Format - run `mvn spotless:apply` to auto-fix
- **Python**: Uses Black - run `black .` to auto-fix

[SO post]: https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project#answer-4955695
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ This is the top level repository for Ariadne code. More information on using the

Since it is built using [WALA], you need to have WALA on your system to use it. Instructions on building this project can be found in [CONTRIBUTING.md].

## Code Quality Standards

This project enforces several code quality standards:

- **Print Statement Policy**: Print statements (`System.out`, `System.err`) should only be used in CLI driver classes. See [Print Statement Policy](docs/PRINT_STATEMENT_POLICY.md) for details. Enforced via Maven Checkstyle plugin.
- **Code Formatting**: Java code is formatted using Spotless with Google Java Format.
- **Python Formatting**: Python code is formatted using Black.

The build will fail if these standards are not met.

To test, for example, run `TestCalls` in the `com.ibm.wala.cast.python.test` project.

[WALA]: https://github.com/wala/WALA
Expand Down
11 changes: 11 additions & 0 deletions checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">
<suppressions>
<!-- Allow print statements in CLI driver classes -->
<suppress checks="RegexpSinglelineJava" files=".*[/\\]driver[/\\].*\.java" />
<!-- Allow print statements in main methods of utility classes -->
<suppress checks="RegexpSinglelineJava" files=".*PythonFileParser\.java" />
<suppress checks="RegexpSinglelineJava" files=".*PythonModuleParser\.java" />
</suppressions>
21 changes: 21 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<property name="charset" value="UTF-8" />
<property name="severity" value="error" />
<property name="fileExtensions" value="java" />
<!-- Check for inappropriate print statement usage -->
<module name="TreeWalker">
<module name="RegexpSinglelineJava">
<property name="format" value="System\.(out|err)\.print" />
<property name="message" value="Print statements should only be used in CLI driver classes. Use java.util.logging.Logger instead." />
<property name="ignoreComments" value="true" />
</module>
</module>
<!-- Suppress checks for CLI driver classes -->
<module name="SuppressionFilter">
<property name="file" value="checkstyle-suppressions.xml" />
</module>
</module>
123 changes: 123 additions & 0 deletions docs/PRINT_STATEMENT_POLICY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Print Statement Usage Policy

This project enforces a policy that prevents inappropriate usage of `System.out.println()` and `System.err.println()` in non-CLI code. Print statements should only be used in CLI driver classes for user-facing output.

## Policy

- **Allowed**: Print statements in CLI driver classes (under `/driver/` packages)
- **Allowed**: Print statements in main methods of utility/demo classes (e.g., parser demos)
- **Not Allowed**: Print statements in core library code, analysis code, test classes, or other components

For all non-CLI code, including test classes, use `java.util.logging.Logger` instead of print statements.

## Build Integration

The build automatically checks for inappropriate print statement usage during the `validate` phase using Maven Checkstyle plugin:

```bash
# This will fail if inappropriate print statements are found
mvn validate

# This will succeed only if no violations are detected
mvn validate -Dskip.print.check=false
```

## Bypassing the Check

During development or when working on refactoring print statements (e.g., issue #331), you can skip the check:

```bash
# Skip the print statement check
mvn validate -Dskip.print.check=true

# Or set it permanently in your local settings
mvn clean install -Dskip.print.check=true
```

## Fixing Violations

If the build fails due to inappropriate print statements:

1. **For debug/info messages**: Replace with appropriate logging:
```java
// Bad
System.err.println("Debug info: " + value);

// Good
private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName());
LOGGER.fine("Debug info: " + value);
```

2. **For error messages**: Use logging with appropriate levels:
```java
// Bad
System.err.println("Error occurred: " + exception.getMessage());

// Good
LOGGER.severe("Error occurred: " + exception.getMessage());
```

3. **For CLI output**: Move the code to a driver class or ensure it's in an appropriate location

## Script Details

The check is implemented using Maven Checkstyle plugin with a custom configuration that:

- Scans all Java files (including test files) for `System.out` and `System.err` usage
- Excludes files in `/driver/` directories
- Allows print statements in main methods of specific utility classes
- Fails the build if violations are found

## Examples

### ✅ Allowed Usage

```java
// CLI driver class
public class Ariadne {
public static void main(String[] args) {
System.out.println("Analysis complete."); // OK - CLI output
}
}

// Test class
public class TestParser {
private static final Logger LOGGER = Logger.getLogger(TestParser.class.getName());

public void testMethod() {
LOGGER.info("Debug output"); // OK - using logging
}
}

// Demo main method
public class PythonFileParser {
public static void main(String[] args) {
System.err.println(script); // OK - demo/utility main method
}
}
```

### ❌ Prohibited Usage

```java
// Core library code
public class PythonParser {
public void parseCode() {
System.err.println("Parsing..."); // NOT OK - use LOGGER.fine() instead
}
}

// Analysis engine
public class AnalysisEngine {
public void analyze() {
System.out.println("Found result"); // NOT OK - use LOGGER.info() instead
}
}

// Test class
public class TestAnalysis {
public void testMethod() {
System.err.println("Debug info"); // NOT OK - use LOGGER.info() instead
}
}
```
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
<wala.version>1.6.13-SNAPSHOT</wala.version>
<spotless.version>2.46.1</spotless.version>
<maven.surefire.version>3.5.4</maven.surefire.version>
<checkstyle.version>10.20.1</checkstyle.version>
<parallel>both</parallel>
<logging.config.file>./logging.properties</logging.config.file>
<jacoco-version>0.8.13</jacoco-version>
<argLine/>
<skip.print.check>false</skip.print.check>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -252,8 +254,43 @@
</pom>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<skip>${skip.print.check}</skip>
</configuration>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>check-print-statements</id>
<goals>
<goal>check</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
Expand Down