55import org .eclipse .jdt .core .dom .MethodDeclaration ;
66import org .eclipse .jdt .core .dom .NumberLiteral ;
77import org .eclipse .jdt .core .dom .SimpleType ;
8- import org .eclipse .jdt .core .dom .TypeDeclaration ;
98
9+ import java .lang .reflect .Modifier ;
1010import java .util .ArrayList ;
1111import java .util .List ;
1212import java .util .regex .Matcher ;
@@ -141,25 +141,6 @@ public static List<Edit> wrapSketch(PdePreprocessor.Mode mode, String className,
141141 }
142142
143143
144- public static List <Edit > addPublicToTopLevelMethods (CompilationUnit cu ) {
145- List <Edit > edits = new ArrayList <>();
146-
147- // Add public modifier to top level methods
148- for (Object node : cu .types ()) {
149- if (node instanceof TypeDeclaration ) {
150- TypeDeclaration type = (TypeDeclaration ) node ;
151- for (MethodDeclaration method : type .getMethods ()) {
152- if (method .modifiers ().isEmpty () && !method .isConstructor ()) {
153- edits .add (Edit .insert (method .getStartPosition (), "public " ));
154- }
155- }
156- }
157- }
158-
159- return edits ;
160- }
161-
162-
163144 // Verifies that whole input String is floating point literal. Can't be used for searching.
164145 // https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-DecimalFloatingPointLiteral
165146 public static final Pattern FLOATING_POINT_LITERAL_VERIFIER ;
@@ -173,13 +154,18 @@ public static List<Edit> addPublicToTopLevelMethods(CompilationUnit cu) {
173154 "(?:^" + DIGITS + EXPONENT_PART + "?[fFdD]$)" );
174155 }
175156
176- public static List <Edit > replaceColorAndFixFloats (CompilationUnit cu ) {
157+ // Mask to quickly resolve whether there are any access modifiers present
158+ private static final int ACCESS_MODIFIERS_MASK =
159+ Modifier .PUBLIC | Modifier .PRIVATE | Modifier .PROTECTED ;
160+
161+ public static List <Edit > preprocessAST (CompilationUnit cu ) {
177162 final List <Edit > edits = new ArrayList <>();
178163
179- // Walk the tree, replace "color" with "int" and add 'f' to floats
164+ // Walk the tree
180165 cu .accept (new ASTVisitor () {
181166 @ Override
182167 public boolean visit (SimpleType node ) {
168+ // replace "color" with "int"
183169 if ("color" .equals (node .getName ().toString ())) {
184170 edits .add (Edit .replace (node .getStartPosition (), node .getLength (), "int" ));
185171 }
@@ -188,12 +174,23 @@ public boolean visit(SimpleType node) {
188174
189175 @ Override
190176 public boolean visit (NumberLiteral node ) {
177+ // add 'f' to floats
191178 String s = node .getToken ().toLowerCase ();
192179 if (FLOATING_POINT_LITERAL_VERIFIER .matcher (s ).matches () && !s .endsWith ("f" ) && !s .endsWith ("d" )) {
193180 edits .add (Edit .insert (node .getStartPosition () + node .getLength (), "f" ));
194181 }
195182 return super .visit (node );
196183 }
184+
185+ @ Override
186+ public boolean visit (MethodDeclaration node ) {
187+ // add 'public' to methods with default visibility
188+ int accessModifiers = node .getModifiers () & ACCESS_MODIFIERS_MASK ;
189+ if (accessModifiers == 0 ) {
190+ edits .add (Edit .insert (node .getStartPosition (), "public " ));
191+ }
192+ return super .visit (node );
193+ }
197194 });
198195
199196 return edits ;
@@ -322,4 +319,4 @@ static public void scrubCommentsAndStrings(StringBuilder p) {
322319
323320 }
324321
325- }
322+ }
0 commit comments