Skip to content

Commit ef17e5c

Browse files
committed
Error checker now adds 'public' to all default access methods
Fixes processing#4583
1 parent a2e8954 commit ef17e5c

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

java/src/processing/mode/java/pdex/PreprocessingService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,8 +402,7 @@ private PreprocessedSketch preprocessSketch(PreprocessedSketch prevResult) {
402402

403403
// Prepare advanced transforms which operate on AST
404404
TextTransform toCompilable = new TextTransform(parsableStage);
405-
toCompilable.addAll(SourceUtils.addPublicToTopLevelMethods(parsableCU));
406-
toCompilable.addAll(SourceUtils.replaceColorAndFixFloats(parsableCU));
405+
toCompilable.addAll(SourceUtils.preprocessAST(parsableCU));
407406

408407
// Transform code to compilable state
409408
String compilableStage = toCompilable.apply();

java/src/processing/mode/java/pdex/SourceUtils.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import org.eclipse.jdt.core.dom.MethodDeclaration;
66
import org.eclipse.jdt.core.dom.NumberLiteral;
77
import org.eclipse.jdt.core.dom.SimpleType;
8-
import org.eclipse.jdt.core.dom.TypeDeclaration;
98

9+
import java.lang.reflect.Modifier;
1010
import java.util.ArrayList;
1111
import java.util.List;
1212
import 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

Comments
 (0)