Fix nullpointer in ast finder

This commit is contained in:
jeffcheasey88 2023-10-18 14:39:29 +02:00
parent aab4cd5fd8
commit 90007dc0d2
8 changed files with 43 additions and 24 deletions

View file

@ -55,12 +55,16 @@ public class Class extends JavaElement implements AnnotableBuffer, ClassContaine
@Override @Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){ public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){ for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation; if(finder.apply(annotation)) return (E) annotation;
} }
}
if(elements != null){
for(JavaElement element : this.elements){ for(JavaElement element : this.elements){
if(finder.apply(element)) return (E) element; if(finder.apply(element)) return (E) element;
} }
}
return null; return null;
} }

View file

@ -61,15 +61,21 @@ public class Function extends JavaElement implements VariableContainer, Operatio
@Override @Override
public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){ public <E extends JavaElement> E find(java.util.function.Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){ for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation; if(finder.apply(annotation)) return (E) annotation;
} }
}
if(parameters != null){
for(Variable param : this.parameters){ for(Variable param : this.parameters){
if(finder.apply(param)) return (E) param; if(finder.apply(param)) return (E) param;
} }
}
if(elements != null){
for(JavaElement content : this.elements){ for(JavaElement content : this.elements){
if(finder.apply(content)) return (E) content; if(finder.apply(content)) return (E) content;
} }
}
return null; return null;
} }

View file

@ -733,7 +733,9 @@ public class JavaParser extends Parser<JavaElement> {
BiFunction<JavaElement, Bag, JavaElement> function_builder = (parent, bag) -> { BiFunction<JavaElement, Bag, JavaElement> function_builder = (parent, bag) -> {
buildVariable(bag); buildVariable(bag);
Integer mod = bag.get("mod"); Integer mod = bag.get("mod");
Function function = new Function((((AnnotableBuffer)parent).getAnnotationBuffer()), mod == null ? 0 : mod, bag.get("generic"), bag.get("type"), bag.get("name"), bag.get("param"), bag.get("throws")); List<Annotation> annotations = null;
if(parent instanceof AnnotableBuffer) annotations = (((AnnotableBuffer)parent).getAnnotationBuffer());
Function function = new Function(annotations, mod == null ? 0 : mod, bag.get("generic"), bag.get("type"), bag.get("name"), bag.get("param"), bag.get("throws"));
if(parent instanceof FunctionContainer) ((FunctionContainer)parent).addFunction(function); if(parent instanceof FunctionContainer) ((FunctionContainer)parent).addFunction(function);
return function; return function;
}; };
@ -1039,7 +1041,7 @@ public class JavaParser extends Parser<JavaElement> {
} }
public static void main(String[] args) throws Exception{ public static void main(String[] args) throws Exception{
File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\src\\dev\\peerat\\backend\\bonus\\extract\\RouteExtracter.java"); File file = new File("C:\\Users\\jeffc\\eclipse-workspace\\peer-at-code-backend\\test\\dev\\peerat\\backend\\routes\\PlayerDetailsTests.java");
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));

View file

@ -46,9 +46,11 @@ public class Variable extends JavaElement{
@Override @Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){ public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(annotations != null){
for(Annotation annotation : this.annotations){ for(Annotation annotation : this.annotations){
if(finder.apply(annotation)) return (E) annotation; if(finder.apply(annotation)) return (E) annotation;
} }
}
return value != null ? finder.apply(value) ? (E)value : null : null; return value != null ? finder.apply(value) ? (E)value : null : null;
} }
} }

View file

@ -44,12 +44,16 @@ public class ForOperation extends OperationBag{
@Override @Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){ public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(init_vars != null){
for(Variable variable : this.init_vars){ for(Variable variable : this.init_vars){
if(finder.apply(variable)) return (E)variable; if(finder.apply(variable)) return (E)variable;
} }
}
if(init_values != null){
for(Value value : this.init_values){ for(Value value : this.init_values){
if(finder.apply(value)) return (E)value; if(finder.apply(value)) return (E)value;
} }
}
if(finder.apply(condition)) return (E)condition; if(finder.apply(condition)) return (E)condition;
for(Value value : this.updates){ for(Value value : this.updates){
if(finder.apply(value)) return (E)value; if(finder.apply(value)) return (E)value;

View file

@ -30,9 +30,11 @@ public class OperationBag extends Operation implements VariableContainer, Operat
@Override @Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){ public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(elements != null){
for(JavaElement element : this.elements){ for(JavaElement element : this.elements){
if(finder.apply(element)) return (E)element; if(finder.apply(element)) return (E)element;
} }
}
return null; return null;
} }

View file

@ -20,7 +20,7 @@ public class ReturnOperation extends Operation{
@Override @Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) { public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder) {
return finder.apply(value) ? (E)value : null; return value != null ? finder.apply(value) ? (E)value : null : null;
} }
} }

View file

@ -24,7 +24,6 @@ public class TryOperation extends OperationBag{
@Override @Override
public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){ public <E extends JavaElement> E find(Function<JavaElement, Boolean> finder){
if(finder.apply(resource)) return (E)resource; return resource != null ? finder.apply(resource) ? (E)resource : super.find(finder) : super.find(finder);
return super.find(finder);
} }
} }