4444import de .siegmar .fastcsv .reader .CsvReader ;
4545import de .siegmar .fastcsv .reader .CsvRow ;
4646import org .apache .commons .lang3 .tuple .Pair ;
47+ import org .gradle .api .logging .Logger ;
4748
4849public class McpNames {
4950 private static final String NEWLINE = System .getProperty ("line.separator" );
@@ -53,8 +54,9 @@ public class McpNames {
5354 private static final Pattern CLASS_JAVADOC_PATTERN = Pattern .compile ("^(?<indent>(?: )*|\\ t*)([\\ w|@]*\\ s)*(class|interface|@interface|enum) (?<name>[\\ w]+)" );
5455 private static final Pattern CLOSING_CURLY_BRACE = Pattern .compile ("^(?<indent>(?: )*|\\ t*)}" );
5556 private static final Pattern PACKAGE_DECL = Pattern .compile ("^[\\ s]*package(\\ s)*(?<name>[\\ w|.]+);$" );
57+ private int currentMethodIndent = -1 ;
5658
57- public static McpNames load (File data ) throws IOException {
59+ public static McpNames load (File data , Logger logger ) throws IOException {
5860 Map <String , String > names = new HashMap <>();
5961 Map <String , String > docs = new HashMap <>();
6062 try (ZipFile zip = new ZipFile (data )) {
@@ -75,17 +77,19 @@ public static McpNames load(File data) throws IOException {
7577 }
7678 }
7779
78- return new McpNames (HashFunction .SHA1 .hash (data ), names , docs );
80+ return new McpNames (HashFunction .SHA1 .hash (data ), names , docs , logger );
7981 }
8082
8183 private Map <String , String > names ;
8284 private Map <String , String > docs ;
8385 public final String hash ;
86+ private final Logger logger ;
8487
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 ) {
8689 this .hash = hash ;
8790 this .names = names ;
8891 this .docs = docs ;
92+ this .logger = logger ;
8993 }
9094
9195 public String rename (InputStream stream , boolean javadocs ) throws IOException {
@@ -123,7 +127,7 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
123127 String javadoc = docs .get (matcher .group ("name" ));
124128 if (!Strings .isNullOrEmpty (javadoc ))
125129 insertAboveAnnotations (lines , JavadocAdder .buildJavadoc (matcher .group ("indent" ), javadoc , true ));
126-
130+ currentMethodIndent = matcher . group ( "indent" ). length ();
127131 // worked, so return and don't try the fields.
128132 return ;
129133 }
@@ -143,9 +147,23 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
143147 if (matcher .find ()) {
144148 //we maintain a stack of the current (inner) class in com.example.ClassName$Inner format (along with indentation)
145149 //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+ }
146156 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 ));
148162 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 ;
149167 if (!Strings .isNullOrEmpty (javadoc )) {
150168 insertAboveAnnotations (lines , JavadocAdder .buildJavadoc (matcher .group ("indent" ), javadoc , true ));
151169 }
@@ -156,12 +174,15 @@ private void injectJavadoc(List<String> lines, String line, String _package, Deq
156174 //detect curly braces for inner class stacking/end identification
157175 matcher = CLOSING_CURLY_BRACE .matcher (line );
158176 if (matcher .find ()){
177+ if (matcher .group ("indent" ).length () == currentMethodIndent ) {
178+ currentMethodIndent = -1 ;
179+ }
159180 if (!innerClasses .isEmpty ()) {
160181 int len = matcher .group ("indent" ).length ();
161182 if (len == innerClasses .peek ().getRight ()) {
162183 innerClasses .pop ();
163184 } 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" );
165186 }
166187 }
167188 }
0 commit comments