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

View file

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

View file

@ -733,7 +733,9 @@ public class JavaParser extends Parser<JavaElement> {
BiFunction<JavaElement, Bag, JavaElement> function_builder = (parent, bag) -> {
buildVariable(bag);
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);
return function;
};
@ -1039,7 +1041,7 @@ public class JavaParser extends Parser<JavaElement> {
}
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));

View file

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

View file

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

View file

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

View file

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