44
44
import de .siegmar .fastcsv .reader .CsvReader ;
45
45
import de .siegmar .fastcsv .reader .CsvRow ;
46
46
import org .apache .commons .lang3 .tuple .Pair ;
47
+ import org .gradle .api .logging .Logger ;
47
48
48
49
public class McpNames {
49
50
private static final String NEWLINE = System .getProperty ("line.separator" );
@@ -53,8 +54,9 @@ public class McpNames {
53
54
private static final Pattern CLASS_JAVADOC_PATTERN = Pattern .compile ("^(?<indent>(?: )*|\\ t*)([\\ w|@]*\\ s)*(class|interface|@interface|enum) (?<name>[\\ w]+)" );
54
55
private static final Pattern CLOSING_CURLY_BRACE = Pattern .compile ("^(?<indent>(?: )*|\\ t*)}" );
55
56
private static final Pattern PACKAGE_DECL = Pattern .compile ("^[\\ s]*package(\\ s)*(?<name>[\\ w|.]+);$" );
57
+ private int currentMethodIndent = -1 ;
56
58
57
- public static McpNames load (File data ) throws IOException {
59
+ public static McpNames load (File data , Logger logger ) throws IOException {
58
60
Map <String , String > names = new HashMap <>();
59
61
Map <String , String > docs = new HashMap <>();
60
62
try (ZipFile zip = new ZipFile (data )) {
@@ -75,17 +77,19 @@ public static McpNames load(File data) throws IOException {
75
77
}
76
78
}
77
79
78
- return new McpNames (HashFunction .SHA1 .hash (data ), names , docs );
80
+ return new McpNames (HashFunction .SHA1 .hash (data ), names , docs , logger );
79
81
}
80
82
81
83
private Map <String , String > names ;
82
84
private Map <String , String > docs ;
83
85
public final String hash ;
86
+ private final Logger logger ;
84
87
85
- private McpNames (String hash , Map <String , String > names , Map <String , String > docs ) {
88
+ private McpNames (String hash , Map <String , String > names , Map <String , String > docs , Logger logger ) {
86
89
this .hash = hash ;
87
90
this .names = names ;
88
91
this .docs = docs ;
92
+ this .logger = logger ;
89
93
}
90
94
91
95
public String rename (InputStream stream , boolean javadocs ) throws IOException {
@@ -123,7 +127,7 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
123
127
String javadoc = docs .get (matcher .group ("name" ));
124
128
if (!Strings .isNullOrEmpty (javadoc ))
125
129
insertAboveAnnotations (lines , JavadocAdder .buildJavadoc (matcher .group ("indent" ), javadoc , true ));
126
-
130
+ currentMethodIndent = matcher . group ( "indent" ). length ();
127
131
// worked, so return and don't try the fields.
128
132
return ;
129
133
}
@@ -143,9 +147,23 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
143
147
if (matcher .find ()) {
144
148
//we maintain a stack of the current (inner) class in com.example.ClassName$Inner format (along with indentation)
145
149
//if the stack is not empty we are entering a new inner class
150
+ int parentBlockIndentation = innerClasses .isEmpty () ? 0 : innerClasses .peek ().getRight ();
151
+ int currentIndentation = matcher .group ("indent" ).length ();
152
+ if (parentBlockIndentation >= currentIndentation ) {
153
+ //We haven't closed a previous class correctly, so we backtrack now.
154
+ innerClasses .removeIf (p -> p .getRight () >= currentIndentation );
155
+ }
146
156
String currentClass = (innerClasses .isEmpty () ? _package : innerClasses .peek ().getLeft () + "$" ) + matcher .group ("name" );
147
- innerClasses .push (Pair .of (currentClass , matcher .group ("indent" ).length ()));
157
+ if (parentBlockIndentation >= currentIndentation ) {
158
+ logger .warn ("Trying to recover inner class tracking, assuming " + currentClass + " is defined in line " + (lines .size () + 1 ));
159
+ }
160
+
161
+ innerClasses .push (Pair .of (currentClass , currentIndentation ));
148
162
String javadoc = docs .get (currentClass );
163
+ if (currentMethodIndent != 0 ) {
164
+ logger .warn ("In " + currentClass + ":" + (lines .size () + 1 ) + " there likely is a method inner class, which we can't handle properly" );
165
+ }
166
+ currentMethodIndent = -1 ;
149
167
if (!Strings .isNullOrEmpty (javadoc )) {
150
168
insertAboveAnnotations (lines , JavadocAdder .buildJavadoc (matcher .group ("indent" ), javadoc , true ));
151
169
}
@@ -156,12 +174,15 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
156
174
//detect curly braces for inner class stacking/end identification
157
175
matcher = CLOSING_CURLY_BRACE .matcher (line );
158
176
if (matcher .find ()){
177
+ if (matcher .group ("indent" ).length () == currentMethodIndent ) {
178
+ currentMethodIndent = -1 ;
179
+ }
159
180
if (!innerClasses .isEmpty ()) {
160
181
int len = matcher .group ("indent" ).length ();
161
182
if (len == innerClasses .peek ().getRight ()) {
162
183
innerClasses .pop ();
163
184
} else if (len < innerClasses .peek ().getRight ()) {
164
- throw new IllegalArgumentException ( "Failed to properly track class blocks around class " + innerClasses .peek ().getLeft () + ":" + (lines .size () + 1 ));
185
+ logger . warn ( "Tracking inner classes failed around " + innerClasses .peek ().getLeft () + ":" + (lines .size () + 1 ) + ", classes may be annotated incorrectly. Is your indentation off?g" );
165
186
}
166
187
}
167
188
}
0 commit comments