Checked parameters on null

This commit is contained in:
ahmad 2024-07-05 01:03:58 +02:00
parent 8199bc9b10
commit 6af67326f1
5 changed files with 80 additions and 0 deletions

View File

@ -70,6 +70,10 @@ public class TypedAssignment implements TypedStatement {
} }
private Type getTypeFromMethodOrField(TypedMethod currentMethod, String name, TypedClass currentClass) { private Type getTypeFromMethodOrField(TypedMethod currentMethod, String name, TypedClass currentClass) {
if(currentMethod == null || currentClass == null || name == null) {
throw new RuntimeException("Method, Class or Name is null");
}
if (currentMethod.isLocalVariableInMethod(name)) { if (currentMethod.isLocalVariableInMethod(name)) {
return currentMethod.getLocalVariableType(name); return currentMethod.getLocalVariableType(name);
} else if (currentClass.isThereField(name)) { } else if (currentClass.isThereField(name)) {
@ -80,6 +84,11 @@ public class TypedAssignment implements TypedStatement {
} }
private Type getTypeFromConstructorOrField(TypedConstructor currentConstructor, String name, TypedClass currentClass) { private Type getTypeFromConstructorOrField(TypedConstructor currentConstructor, String name, TypedClass currentClass) {
if(currentConstructor == null || currentClass == null || name == null) {
throw new RuntimeException("Constructor, Class or Name is null");
}
if (currentConstructor.isLocalVariableInConstructor(name)) { if (currentConstructor.isLocalVariableInConstructor(name)) {
return currentConstructor.getLocalVariableType(name); return currentConstructor.getLocalVariableType(name);
} else if (currentClass.isThereField(name)) { } else if (currentClass.isThereField(name)) {

View File

@ -124,22 +124,38 @@ public class TypedClass implements TypedNode {
} }
public boolean isParameterWitNameInCurrentMethod(String parameterName) { public boolean isParameterWitNameInCurrentMethod(String parameterName) {
if (parameterName == null) {
return false;
}
return this.getCurrentMethod().getTypedParameters().stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName)); return this.getCurrentMethod().getTypedParameters().stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
} }
public boolean isParameterWitNameInCurrentConstructor(String parameterName) { public boolean isParameterWitNameInCurrentConstructor(String parameterName) {
if (parameterName == null) {
return false;
}
return this.getCurrentConstructor().getTypedParameters().stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName)); return this.getCurrentConstructor().getTypedParameters().stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
} }
public Type getParameterTypeInCurrentMethod(String parameterName) { public Type getParameterTypeInCurrentMethod(String parameterName) {
if(parameterName == null) {
throw new RuntimeException("Parameter name is null");
}
return getCurrentMethod().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType(); return getCurrentMethod().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType();
} }
public Type getParameterTypeInCurrentConstructor(String parameterName) { public Type getParameterTypeInCurrentConstructor(String parameterName) {
if(parameterName == null) {
throw new RuntimeException("Parameter name is null");
}
return getCurrentConstructor().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType(); return getCurrentConstructor().getTypedParameters().stream().filter(parameter -> parameter.getParaName().equals(parameterName)).findFirst().get().getType();
} }
public boolean isMethodOfCurrentClass(String methodName) { public boolean isMethodOfCurrentClass(String methodName) {
if(methodName == null) {
return false;
}
for (TypedMethod m : typedMethods) { for (TypedMethod m : typedMethods) {
if (m.getName().equals(methodName)) { if (m.getName().equals(methodName)) {
return true; return true;
@ -149,6 +165,10 @@ public class TypedClass implements TypedNode {
} }
public Type getMethodType(String methodName) { public Type getMethodType(String methodName) {
if(methodName == null) {
throw new RuntimeException("Method name is null");
}
for (TypedMethod m : typedMethods) { for (TypedMethod m : typedMethods) {
if (m.getName().equals(methodName)) { if (m.getName().equals(methodName)) {
return m.getReturnType(); return m.getReturnType();
@ -158,6 +178,10 @@ public class TypedClass implements TypedNode {
} }
public boolean isThereField(String fieldName) { public boolean isThereField(String fieldName) {
if(fieldName == null) {
return false;
}
for (TypedDeclaration f : typedDeclarations) { for (TypedDeclaration f : typedDeclarations) {
if (f.getName().equals(fieldName)) { if (f.getName().equals(fieldName)) {
return true; return true;
@ -167,6 +191,9 @@ public class TypedClass implements TypedNode {
} }
public Type getFieldType(String fieldName) { public Type getFieldType(String fieldName) {
if(fieldName == null) {
throw new RuntimeException("Field name is null");
}
for (TypedDeclaration f : typedDeclarations) { for (TypedDeclaration f : typedDeclarations) {
if (f.getName().equals(fieldName)) { if (f.getName().equals(fieldName)) {
return f.getType(); return f.getType();

View File

@ -46,6 +46,10 @@ public class TypedConstructor implements TypedNode {
} }
private void convertToTypedParameter(List<Parameter> params) { private void convertToTypedParameter(List<Parameter> params) {
if(params.isEmpty()) {
return;
}
for (Parameter param : params) { for (Parameter param : params) {
TypedParameter typedParameter = new TypedParameter(param); TypedParameter typedParameter = new TypedParameter(param);
checkIfParameterExists(typedParameter.getParaName()); checkIfParameterExists(typedParameter.getParaName());
@ -70,28 +74,50 @@ public class TypedConstructor implements TypedNode {
} }
public boolean isLocalVariablePresent(String localVarName) { public boolean isLocalVariablePresent(String localVarName) {
if(localVarName == null) {
return false;
}
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName)); return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
} }
public boolean isParameterPresent(String parameterName) { public boolean isParameterPresent(String parameterName) {
if(parameterName == null) {
return false;
}
return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName)); return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
} }
public boolean isLocalVariableInConstructor(String localVarName) { public boolean isLocalVariableInConstructor(String localVarName) {
if(localVarName == null) {
return false;
}
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName); return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
} }
public Type getLocalVariableType(String localVarName) { public Type getLocalVariableType(String localVarName) {
if(localVarName == null) {
throw new RuntimeException("Local variable name is null");
}
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType(); return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
} }
private void checkIfParameterExists(String paraName) { private void checkIfParameterExists(String paraName) {
if(paraName == null) {
throw new RuntimeException("Parameter name is null");
}
if (typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(paraName))) { if (typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(paraName))) {
throw new RuntimeException("Parameter " + paraName + " already exists"); throw new RuntimeException("Parameter " + paraName + " already exists");
} }
} }
public void deleteLocalVariableInConstructor(String localVarName) { public void deleteLocalVariableInConstructor(String localVarName) {
if(localVarName == null) {
throw new RuntimeException("Local variable name is null");
}
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName)); localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));
} }

View File

@ -68,6 +68,11 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
} }
public Optional<Type> findMatchingMethod(TypedClass ownerChain, String methodName) { public Optional<Type> findMatchingMethod(TypedClass ownerChain, String methodName) {
if(ownerChain == null || methodName == null) {
return Optional.empty();
}
List<TypedMethod> methods = ownerChain.getTypedMethods().stream() List<TypedMethod> methods = ownerChain.getTypedMethods().stream()
.filter(method -> method.getName().equals(methodName)) .filter(method -> method.getName().equals(methodName))
.toList(); .toList();

View File

@ -45,10 +45,17 @@ public class TypedProgram {
} }
public boolean isClassWithNamePresent(String className) { public boolean isClassWithNamePresent(String className) {
if(className == null){
return false;
}
return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className)); return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className));
} }
public Type getTypeOfFieldNameInClass(String className, String fieldName) { public Type getTypeOfFieldNameInClass(String className, String fieldName) {
if(className == null || fieldName == null){
return null;
}
return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get().getFieldType(fieldName); return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get().getFieldType(fieldName);
} }
public Type getTypeOfFieldOrMethodNameInClass(String className, String fieldName) { public Type getTypeOfFieldOrMethodNameInClass(String className, String fieldName) {
@ -66,10 +73,16 @@ public class TypedProgram {
} }
public TypedClass getTypedClass(String className) { public TypedClass getTypedClass(String className) {
if(className == null){
return null;
}
return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get(); return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get();
} }
public boolean isTypedClassPresent(String className) { public boolean isTypedClassPresent(String className) {
if(className == null){
return false;
}
return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className)); return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className));
} }