|
1 | | -# java-preprocessing-lib |
2 | | -Trying to create a library to somehow preprocess the java code using directives. |
| 1 | +# Java Preprocessing Library |
| 2 | + |
| 3 | +A lightweight Java library and Maven plugin that brings C++ style preprocessor directives (`#if`, `#ifdef`, etc.) to Java. This allows for conditional compilation based on build-time symbols, enabling feature toggles, multi-version builds, and platform-specific code generation. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +* **Standard Directives**: Support for `#if`, `#ifdef`, `#ifndef`, `#else`, `#endif`. |
| 8 | +* **Expression Evaluation**: Support for complex boolean expressions in `#if` directives (e.g., `#if DEBUG && VERSION > 1`). |
| 9 | +* **Maven Plugin**: Seamless integration into the Maven build lifecycle (`generate-sources`). |
| 10 | +* **Zero Runtime Dependencies**: The preprocessor runs at build time; the generated code is pure Java with no extra runtime requirements. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Currently, you need to build and install the library locally: |
| 15 | + |
| 16 | +```bash |
| 17 | +git clone https://github.com/your-repo/java-preprocessing-lib.git |
| 18 | +cd java-preprocessing-lib |
| 19 | +mvn install |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Maven Configuration |
| 25 | + |
| 26 | +Add the `preprocessor-maven-plugin` to your `pom.xml`. It should be configured to run during the `generate-sources` phase. |
| 27 | + |
| 28 | +**Note**: To use the preprocessor effectively, it is recommended to keep your source files with directives in a separate directory (e.g., `src/main/preprocess`) to prevent the standard Java compiler from trying to compile the unprocessed files. |
| 29 | + |
| 30 | +```xml |
| 31 | +<build> |
| 32 | + <plugins> |
| 33 | + <plugin> |
| 34 | + <groupId>com.github.javapreprocessor</groupId> |
| 35 | + <artifactId>preprocessor-maven-plugin</artifactId> |
| 36 | + <version>1.0.0-SNAPSHOT</version> |
| 37 | + <executions> |
| 38 | + <execution> |
| 39 | + <id>preprocess-sources</id> |
| 40 | + <phase>generate-sources</phase> |
| 41 | + <goals> |
| 42 | + <goal>process</goal> |
| 43 | + </goals> |
| 44 | + <configuration> |
| 45 | + <!-- Directory containing source files with directives --> |
| 46 | + <sourceDirectory>${project.basedir}/src/main/preprocess</sourceDirectory> |
| 47 | + |
| 48 | + <!-- Define symbols for conditional compilation --> |
| 49 | + <symbols> |
| 50 | + <DEBUG>true</DEBUG> |
| 51 | + <VERSION>2</VERSION> |
| 52 | + <FEATURE_X>false</FEATURE_X> |
| 53 | + </symbols> |
| 54 | + </configuration> |
| 55 | + </execution> |
| 56 | + </executions> |
| 57 | + </plugin> |
| 58 | + </plugins> |
| 59 | +</build> |
| 60 | +``` |
| 61 | + |
| 62 | +### Supported Directives |
| 63 | + |
| 64 | +#### `#ifdef` / `#ifndef` |
| 65 | +Checks if a symbol is defined (or not defined). |
| 66 | + |
| 67 | +```java |
| 68 | +#ifdef DEBUG |
| 69 | + System.out.println("Debug logging enabled"); |
| 70 | +#endif |
| 71 | + |
| 72 | +#ifndef PRODUCTION |
| 73 | + // Test helper code |
| 74 | +#endif |
| 75 | +``` |
| 76 | + |
| 77 | +#### `#if` / `#else` / `#endif` |
| 78 | +Evaluates a boolean expression. Supports `&&`, `||`, `!`, `>`, `<`, `>=`, `<=`, `==`, `!=`, and `defined()`. |
| 79 | + |
| 80 | +```java |
| 81 | +#if VERSION > 1 && defined(FEATURE_NEW_UI) |
| 82 | + renderNewUI(); |
| 83 | +#else |
| 84 | + renderClassicUI(); |
| 85 | +#endif |
| 86 | +``` |
| 87 | + |
| 88 | +#### `#else` |
| 89 | +Alternative branch for `#if`, `#ifdef`, or `#ifndef`. |
| 90 | + |
| 91 | +## Example |
| 92 | + |
| 93 | +**Input (`src/main/preprocess/com/example/App.java`):** |
| 94 | +```java |
| 95 | +package com.example; |
| 96 | + |
| 97 | +public class App { |
| 98 | + public static void main(String[] args) { |
| 99 | + #if DEBUG |
| 100 | + System.out.println("Debugging..."); |
| 101 | + #endif |
| 102 | + |
| 103 | + System.out.println("Running App"); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +**Configuration (pom.xml):** |
| 109 | +```xml |
| 110 | +<symbols> |
| 111 | + <DEBUG>true</DEBUG> |
| 112 | +</symbols> |
| 113 | +``` |
| 114 | + |
| 115 | +**Generated Output (`target/generated-sources/preprocessed/com/example/App.java`):** |
| 116 | +```java |
| 117 | +package com.example; |
| 118 | + |
| 119 | +public class App { |
| 120 | + public static void main(String[] args) { |
| 121 | + |
| 122 | + System.out.println("Debugging..."); |
| 123 | + |
| 124 | + |
| 125 | + System.out.println("Running App"); |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## License |
| 131 | + |
| 132 | +[MIT License](LICENSE) |
0 commit comments