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) {
if(currentMethod == null || currentClass == null || name == null) {
throw new RuntimeException("Method, Class or Name is null");
}
if (currentMethod.isLocalVariableInMethod(name)) {
return currentMethod.getLocalVariableType(name);
} else if (currentClass.isThereField(name)) {
@ -80,6 +84,11 @@ public class TypedAssignment implements TypedStatement {
}
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)) {
return currentConstructor.getLocalVariableType(name);
} else if (currentClass.isThereField(name)) {

View File

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

View File

@ -46,6 +46,10 @@ public class TypedConstructor implements TypedNode {
}
private void convertToTypedParameter(List<Parameter> params) {
if(params.isEmpty()) {
return;
}
for (Parameter param : params) {
TypedParameter typedParameter = new TypedParameter(param);
checkIfParameterExists(typedParameter.getParaName());
@ -70,28 +74,50 @@ public class TypedConstructor implements TypedNode {
}
public boolean isLocalVariablePresent(String localVarName) {
if(localVarName == null) {
return false;
}
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
}
public boolean isParameterPresent(String parameterName) {
if(parameterName == null) {
return false;
}
return typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName));
}
public boolean isLocalVariableInConstructor(String localVarName) {
if(localVarName == null) {
return false;
}
return isLocalVariablePresent(localVarName) || isParameterPresent(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();
}
private void checkIfParameterExists(String paraName) {
if(paraName == null) {
throw new RuntimeException("Parameter name is null");
}
if (typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(paraName))) {
throw new RuntimeException("Parameter " + paraName + " already exists");
}
}
public void deleteLocalVariableInConstructor(String localVarName) {
if(localVarName == null) {
throw new RuntimeException("Local variable name is null");
}
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) {
if(ownerChain == null || methodName == null) {
return Optional.empty();
}
List<TypedMethod> methods = ownerChain.getTypedMethods().stream()
.filter(method -> method.getName().equals(methodName))
.toList();

View File

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