Skip to content

Commit c1134e7

Browse files
committed
Implement module requires removal
Fixes: #37
1 parent ae3daa7 commit c1134e7

20 files changed

Lines changed: 662 additions & 15 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ $(call Executable_file,jurand_test): $(call Object_file,jurand_test.cpp)
1717

1818
manpages: \
1919
$(call Manpage,jurand.1)\
20+
$(call Manpage,java_remove.7)\
2021
$(call Manpage,java_remove_annotations.7)\
2122
$(call Manpage,java_remove_imports.7)\
2223

README.adoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A tool for manipulating symbols present in `.java` source files.
55
The tool can be used for patching `.java` sources in cases where using `sed` is insufficient due to Java language syntax.
66
The tool follows Java language rules rather than applying simple regular expressions on the source code.
77

8-
Currently the tool is able to remove `import` statements and annotations.
8+
Currently the tool is able to remove `import` statements, annotations and modular `requires` statements.
99

1010
== Usage
1111
----
@@ -16,6 +16,7 @@ Matcher::
1616
[horizontal]
1717
`-n <name>`::: Simple (not fully-qualified) class name.
1818
`-p <pattern>`::: Regex pattern to match names used in code.
19+
`-m <pattern>`::: Regex pattern to match module name `requires` fields used in `module-info.java` files.
1920
[horizontal!]
2021

2122
Optional flags::
@@ -36,10 +37,12 @@ File path arguments are handled the following way:
3637
* Symlinks are ignored
3738
* Regular files are handled regardless of the file name
3839
* Directories are traversed recursively and all `.java` files are handled
40+
* Files named `module-info.java` are handled specifically
3941

4042
If no file path is provided then the tool reads from the standard input.
4143

4244
Class name matching can be done in two ways: `-n` to match simple class names or `-p` to match the provided regex pattern against fully qualified names as present in the sources.
45+
Modular `requires` statements are matched using the `-m` flag provding a regex pattern.
4346
These options can be specified multiple times.
4447

4548
The specific implementation of the regex search engine is subject to change.

macros/macros.jurand

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# java_remove - remove dependency declarations or annotations from Java source files
2+
#
3+
# Usage: *%java_remove* [-n <simple class name>]... [-p|-m <pattern>]... [file path]...
4+
#
5+
# Removes dependency declarations or annotations from Java source files by matching
6+
# them against the lists of simple class names and patterns.
7+
%java_remove %{_bindir}/jurand -i
8+
19
# java_remove_imports - remove import statements from Java source files
210
#
311
# Usage: *%java_remove_imports* [-n <simple class name>]... [-p <pattern>]... [file path]...

manpages/java_remove.7.adoc

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
= java_remove(7)
2+
:doctype: manpage
3+
:mansource: JAVA_REMOVE
4+
:manmanual: Java packaging support
5+
6+
== NAME
7+
java_remove - remove dependency declarations or annotations from Java source files
8+
9+
== SYNOPSIS
10+
*%java_remove* [optional flags] <matcher>... [file path]...
11+
12+
== DESCRIPTION
13+
This macro removes annotations, corresponding *import* statements and modular *requires* statements from Java source files.
14+
15+
Annotations and import statements are matched using *-p* and *-n* flags.
16+
Modular requires statements are matched using the *-m* flag.
17+
18+
The tool matches all non-whitespace content following the 'import [static]' statement against all patterns provided via the *-p* flag and all simple class names against names provided by the *-n* flag.
19+
20+
In *module-info.java* files, all content following 'requires [static] [transitive]' is matched against all patterns provided via the *-m* flag.
21+
22+
File path arguments are handled the following way:
23+
24+
* Symlinks are ignored.
25+
* Regular files are handled regardless of the file name.
26+
* Directories are traversed recursively and all `.java` files are handled.
27+
28+
Arguments can be specified in arbitrary order.
29+
30+
Optional flags:
31+
32+
*-s*, *--strict*::
33+
Fail if any of the user provided arguments were redundant.
34+
35+
*-a*::
36+
Also remove annotations used in code.
37+
Annotations will be matched by the non-modular matchers.
38+
39+
Matcher is one of:
40+
41+
*-n <name>*::
42+
Simple class name to be matched against the simple names of imported symbols.
43+
Names are matched for exact equality.
44+
Can be specified multiple times.
45+
46+
*-p <pattern>*::
47+
Regex patterns to be matched against imported symbols.
48+
Each imported symbol found in the code is matched against each pattern.
49+
Can be specified multiple times.
50+
51+
*-m <pattern>*::
52+
Regex pattern to match module name `requires` fields used in `module-info.java` files.
53+
Can be specified multiple times.
54+
55+
== EXAMPLES
56+
Examples of usage in a *.spec* file:
57+
58+
- *%java_remove module-api module-impl module-tests -s -a -n Nullable*
59+
- *%java_remove src -s -a -p org[.]jspecify[.]annotations -m org[.]jspecify*
60+
61+
=== Import patterns:
62+
- Import statements present in Java source file:
63+
64+
1) import java.lang.Runnable;
65+
2) import java.util.List;
66+
3) import static java.util.*;
67+
4) import static java.lang.String.valueOf;
68+
5) import com.google.common.util.concurrent.Service;
69+
70+
- Names used to match:
71+
72+
Name 'Runnable' matches 1)
73+
Name 'String' matches 4)
74+
75+
Name 'util' does not match anything.
76+
Name '*' does not match anything.
77+
Name 'valueOf' does not match anything.
78+
79+
- Patterns used to match:
80+
81+
Pattern 'Runnable' matches 1).
82+
Pattern '[*]' matches 3).
83+
Pattern 'java[.]util' matches 2), 3).
84+
Pattern 'util' matches 2), 3), 5).
85+
Patterns 'java', 'java.*' match 1) - 4).
86+
87+
Pattern 'static' does not match anything.
88+
89+
=== Annotations
90+
- Annotations present in Java source file:
91+
92+
1) @SuppressWarnings
93+
2) @SuppressFBWarnings(value = {"EI_EXPOSE_REP", "EI_EXPOSE_REP2"})
94+
3) @org.junit.Test
95+
4) @org.junit.jupiter.api.Test
96+
97+
- Names used to match:
98+
99+
Name 'SuppressWarnings' matches 1)
100+
Name 'Test' matches 3), 4).
101+
102+
Name 'junit' does not match anything.
103+
104+
- Patterns used to match:
105+
106+
Pattern 'SuppressWarnings' matches 1).
107+
Pattern 'Suppress' matches 1), 2).
108+
Pattern 'org[.]junit[.]Test' matches 3).
109+
Pattern 'junit' matches 3), 4).
110+
111+
Pattern '@SuppressWarnings' does not match anything.
112+
Pattern 'EI_EXPOSE_REP' does not match anything.
113+
114+
== REPORTING BUGS
115+
Bugs should be reported through the issue tracker at GitHub: https://github.com/fedora-java/jurand/issues.
116+
117+
== AUTHOR
118+
Written by Marián Konček.
119+
120+
== SEE ALSO
121+
*jurand*(1).

manpages/jurand.1.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
jurand - Java removal of annotations
88

99
== SYNOPSIS
10-
*jurand* [*-a*] [*-i*] [*-s*] [*-n*=_<name>_] [*-p*=<pattern>] [_<paths>_...]
10+
*jurand* [*-a*] [*-i*] [*-s*] [*-n*=_<name>_] [*-p|-m*=_<pattern>_] [_<paths>_...]
1111

1212
== DESCRIPTION
1313
A tool for manipulating symbols present in `.java` source files.
1414

1515
The tool can be used for patching `.java` sources in cases where using sed is insufficient due to Java language syntax.
1616
The tool follows Java language rules rather than applying simple regular expressions on the source code.
1717

18-
Currently the tool is able to remove `import` statements and annotations.
18+
Currently the tool is able to remove `import` statements, annotations and module `requires` statements.
1919

2020
== OPTIONS
2121
*-n _<name>_::
@@ -24,6 +24,9 @@ Simple (not fully-qualified) class name.
2424
*-p _<pattern>_::
2525
Regex pattern to match names used in code.
2626

27+
*-m _<pattern>_::
28+
Regex pattern to match module name `requires` fields used in `module-info.java` files.
29+
2730
*-a*::
2831
Also remove annotations used in code.
2932

@@ -41,5 +44,6 @@ Bugs should be reported through the Jurand issue tracker at Github: https://gith
4144
Written by Marián Konček.
4245

4346
== SEE ALSO
47+
*java_remove*(7),
4448
*java_remove_annotations*(7),
4549
*java_remove_imports*(7).

0 commit comments

Comments
 (0)