How to use handleGenericType method of org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder class

Best EvoMaster code snippet using org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder.handleGenericType

Source:RPCEndpointsBuilder.java Github

copy

Full Screen

...358 private static NamedTypedValue build(InterfaceSchema schema, Class<?> clazz, Type genericType, String name, RPCType rpcType, List<String> depth,359 Map<Integer, CustomizedRequestValueDto> customizationDtos, Set<String> relatedCustomization, AccessibleSchema accessibleSchema,360 List<CustomizedNotNullAnnotationForRPCDto> notNullAnnotations, Class<?> originalType, Map<TypeVariable, Type> genericTypeMap) {361 handleGenericSuperclass(clazz, genericTypeMap);362 List<String> genericTypes = handleGenericType(clazz, genericType, genericTypeMap);363 String clazzWithGenericTypes = CodeJavaGenerator.handleClassNameWithGeneric(clazz.getName(), genericTypes);364 depth.add(getObjectTypeNameWithFlag(clazz, clazzWithGenericTypes));365 NamedTypedValue namedValue = null;366 try{367 if (PrimitiveOrWrapperType.isPrimitiveOrTypes(clazz)) {368 namedValue = PrimitiveOrWrapperParam.build(name, clazz, accessibleSchema);369 } else if (clazz == String.class) {370 StringType stringType = new StringType();371 namedValue = new StringParam(name, stringType, accessibleSchema);372 } else if (clazz == BigDecimal.class){373 BigDecimalType bigDecimalType = new BigDecimalType();374 namedValue = new BigDecimalParam(name, bigDecimalType, accessibleSchema);375 } else if (clazz == BigInteger.class){376 BigIntegerType bigIntegerType = new BigIntegerType();377 namedValue = new BigIntegerParam(name, bigIntegerType, accessibleSchema);378 } else if (clazz.isEnum()) {379 String [] items = Arrays.stream(clazz.getEnumConstants()).map(e-> getNameEnumConstant(e)).toArray(String[]::new);380 EnumType enumType = new EnumType(clazz.getSimpleName(), clazz.getName(), items, clazz);381 EnumParam param = new EnumParam(name, enumType, accessibleSchema);382 //register this type in the schema383 schema.registerType(enumType.copy(), param.copyStructureWithProperties());384 namedValue = param;385 } else if (clazz.isArray()){386 Type type = null;387 Class<?> templateClazz = null;388 if (genericType instanceof GenericArrayType){389 type = ((GenericArrayType)genericType).getGenericComponentType();390 templateClazz = getTemplateClass(type, genericTypeMap);391 }else {392 templateClazz = clazz.getComponentType();393 }394 NamedTypedValue template = build(schema, templateClazz, type,"template", rpcType, depth, customizationDtos, relatedCustomization, null, notNullAnnotations, null, genericTypeMap);395 template.setNullable(false);396 CollectionType ctype = new CollectionType(clazz.getSimpleName(),clazz.getName(), template, clazz);397 ctype.depth = getDepthLevel(clazz, depth, clazzWithGenericTypes);398 namedValue = new ArrayParam(name, ctype, accessibleSchema);399 } else if (clazz == ByteBuffer.class){400 // handle binary of thrift401 namedValue = new ByteBufferParam(name, accessibleSchema);402 } else if (List.class.isAssignableFrom(clazz) || Set.class.isAssignableFrom(clazz)){403 if (genericType == null)404 throw new RuntimeException("genericType should not be null for List and Set class");405 Type type = ((ParameterizedType) genericType).getActualTypeArguments()[0];406 Class<?> templateClazz = getTemplateClass(type, genericTypeMap);407 NamedTypedValue template = build(schema, templateClazz, type,"template", rpcType, depth, customizationDtos, relatedCustomization, null, notNullAnnotations, null, genericTypeMap);408 template.setNullable(false);409 CollectionType ctype = new CollectionType(clazz.getSimpleName(),clazz.getName(), template, clazz);410 ctype.depth = getDepthLevel(clazz, depth, clazzWithGenericTypes);411 if (List.class.isAssignableFrom(clazz))412 namedValue = new ListParam(name, ctype, accessibleSchema);413 else414 namedValue = new SetParam(name, ctype, accessibleSchema);415 } else if (Map.class.isAssignableFrom(clazz)){416 if (genericType == null)417 throw new RuntimeException("genericType should not be null for List and Set class");418 Type keyType = ((ParameterizedType) genericType).getActualTypeArguments()[0];419 Type valueType = ((ParameterizedType) genericType).getActualTypeArguments()[1];420 Class<?> keyTemplateClazz = getTemplateClass(keyType, genericTypeMap);421 NamedTypedValue keyTemplate = build(schema, keyTemplateClazz, keyType,"keyTemplate", rpcType, depth, customizationDtos, relatedCustomization, null, notNullAnnotations, null, genericTypeMap);422 keyTemplate.setNullable(false);423 Class<?> valueTemplateClazz = getTemplateClass(valueType, genericTypeMap);424 NamedTypedValue valueTemplate = build(schema, valueTemplateClazz, valueType,"valueTemplate", rpcType, depth, customizationDtos, relatedCustomization, null, notNullAnnotations, null, genericTypeMap);425 MapType mtype = new MapType(clazz.getSimpleName(), clazz.getName(), new PairParam(new PairType(keyTemplate, valueTemplate), null), clazz);426 mtype.depth = getDepthLevel(clazz, depth, clazzWithGenericTypes);427 namedValue = new MapParam(name, mtype, accessibleSchema);428 } else if (Date.class.isAssignableFrom(clazz)){429 if (clazz == Date.class)430 namedValue = new DateParam(name, accessibleSchema);431 else432 throw new RuntimeException("NOT support "+clazz.getName()+" date type in java yet");433 } else if (Exception.class.isAssignableFrom(clazz) && clazz.getName().startsWith("java")){434 // note that here we only extract class name and message435 StringParam msgField = new StringParam("message", new AccessibleSchema(false, null, "getMessage"));436 ObjectType exceptionType = new ObjectType(clazz.getSimpleName(), clazz.getName(), Collections.singletonList(msgField), clazz, genericTypes);437 namedValue = new ObjectParam(name, exceptionType, accessibleSchema);438 } else {439 if (clazz.getName().startsWith("java")){440 throw new RuntimeException("NOT handle "+clazz.getName()+" class in java yet");441 }442 long cycleSize = depth.stream().filter(s-> s.equals(getObjectTypeNameWithFlag(clazz, clazzWithGenericTypes))).count();443 if (cycleSize == 1){444 List<NamedTypedValue> fields = new ArrayList<>();445 Map<Integer, CustomizedRequestValueDto> objRelatedCustomizationDtos = getCustomizationBasedOnSpecifiedType(customizationDtos, clazz.getName());446 // field list447 List<Field> fieldList = new ArrayList<>();448 getAllFields(clazz, fieldList, rpcType);449 for(Field f: fieldList){450 // skip final field451 if (Modifier.isFinal(f.getModifiers()))452 continue;453 if (doSkipReflection(f.getName()))454 continue;455 AccessibleSchema faccessSchema = null;456 //check accessible457 if (Modifier.isPublic(f.getModifiers())){458 faccessSchema = new AccessibleSchema();459 } else{460 // find getter and setter461 faccessSchema = new AccessibleSchema(false, findGetterOrSetter(clazz, f, false), findGetterOrSetter(clazz, f, true));462 if (faccessSchema.getterMethodName == null || faccessSchema.setterMethodName == null){463 SimpleLogger.warn("Error: skip the field "+f.getName()+" since its setter/getter is not found");464 continue;465 }466 }467 Class<?> fType = f.getType();468 Class<?> foriginalType = null;469 Type fGType = f.getGenericType();470 if (f.getGenericType() instanceof TypeVariable){471 foriginalType = f.getType();472 Type actualType = getActualType(genericTypeMap, (TypeVariable) f.getGenericType());473 if (actualType instanceof Class){474 fType = (Class<?>) actualType;475 fGType = fType;476 }else if (actualType instanceof ParameterizedType){477 fGType = actualType;478 if (((ParameterizedType) actualType).getRawType() instanceof Class<?>)479 fType = (Class<?>) ((ParameterizedType) actualType).getRawType();480 else481 throw new RuntimeException("Error: Fail to handle actual type of a generic type");482 }483 }484 NamedTypedValue field = build(schema, fType, fGType,f.getName(), rpcType, depth, objRelatedCustomizationDtos, relatedCustomization, faccessSchema, notNullAnnotations, foriginalType, genericTypeMap);485 for (Annotation annotation : f.getAnnotations()){486 handleConstraint(field, annotation, notNullAnnotations);487 }488 fields.add(field);489 }490 handleNativeRPCConstraints(clazz, fields, rpcType);491 ObjectType otype = new ObjectType(clazz.getSimpleName(), clazz.getName(), fields, clazz, genericTypes);492 otype.setOriginalType(originalType);493 otype.depth = getDepthLevel(clazz, depth, clazzWithGenericTypes);494 ObjectParam oparam = new ObjectParam(name, otype, accessibleSchema);495 schema.registerType(otype.copy(), oparam);496 namedValue = oparam;497 }else {498 CycleObjectType otype = new CycleObjectType(clazz.getSimpleName(), clazz.getName(), clazz, genericTypes);499 otype.depth = getDepthLevel(clazz, depth, clazzWithGenericTypes);500 ObjectParam oparam = new ObjectParam(name, otype, accessibleSchema);501 schema.registerType(otype.copy(), oparam);502 namedValue = oparam;503 }504 }505 }catch (ClassCastException e){506 throw new RuntimeException(String.format("fail to perform reflection on param/field: %s; class: %s; genericType: %s; class of genericType: %s; depth: %s; error info:%s",507 name, clazz.getName(), genericType==null?"null":genericType.getTypeName(), genericType==null?"null":genericType.getClass().getName(), String.join(",", depth), e.getMessage()));508 }509 namedValue.getType().setOriginalType(originalType);510 if (customizationDtos!=null){511 handleNamedValueWithCustomizedDto(namedValue, customizationDtos, relatedCustomization);512 }513 return namedValue;514 }515 private static String getNameEnumConstant(Object object) {516 try {517 Method name = object.getClass().getMethod("name");518 name.setAccessible(true);519 return (String) name.invoke(object);520 } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {521 SimpleLogger.warn("Driver Error: fail to extract name for enum constant", e);522 return object.toString();523 }524 }525 private static void handleGenericSuperclass(Class clazz, Map<TypeVariable, Type> map){526 if (isNotCustomizedObject(clazz)) return;527 if (clazz.getGenericSuperclass() == null || !(clazz.getGenericSuperclass() instanceof ParameterizedType)) return;528 Type[] actualTypes = ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments();529 if (((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments().length == 0) return;530 TypeVariable[] typeVariables = clazz.getSuperclass().getTypeParameters();531 if (typeVariables.length != actualTypes.length){532 throw new RuntimeException("Error: fail to handle generic types in Dto");533 }534 for (int i = 0; i < typeVariables.length; i++){535 map.put(typeVariables[i], actualTypes[i]);536 }537 handleGenericSuperclass(clazz.getSuperclass(), map);538 }539 private static List<String> handleGenericType(Class<?> clazz, Type genericType, Map<TypeVariable, Type> map){540 if (isNotCustomizedObject(clazz)) return null;541 if (!(genericType instanceof ParameterizedType)) return null;542 List<String> genericTypes = new ArrayList<>();543 Type[] actualTypes = ((ParameterizedType) genericType).getActualTypeArguments();544 TypeVariable[] typeVariables = clazz.getTypeParameters();545 if (typeVariables.length != actualTypes.length){546 throw new RuntimeException("Error: fail to handle generic types in Dto");547 }548 for (int i = 0; i < typeVariables.length; i++){549 Type a = actualTypes[i];550 if (a instanceof TypeVariable)551 a = getActualType(map, (TypeVariable) a);552 if (a != null)553 genericTypes.add(a.getTypeName());...

Full Screen

Full Screen

handleGenericType

Using AI Code Generation

copy

Full Screen

1@EvoSuiteClasspath(value = { "target/classes" })2public class EvoMasterController {3 public void test() throws Throwable {4 final String packageName = "com.example.demo";5 final String className = "MyController";6 final String methodName = "get";7 final String body = null;8 final double probabilityOfChangingVariableValue = 0.5;9 final double probabilityOfChangingVariableType = 0.5;10 final double probabilityOfAddingNewVariable = 0.5;11 final double probabilityOfAddingNewAction = 0.5;12 final double probabilityOfEnablingActionOutput = 0.5;13 final double probabilityOfDisablingActionOutput = 0.5;14 final double probabilityOfAddingNullValue = 0.5;15 final double probabilityOfDisablingNullValue = 0.5;16 final double probabilityOfAddingNotNullValue = 0.5;17 final double probabilityOfDisablingNotNullValue = 0.5;18 final double probabilityOfAddingEmptyValue = 0.5;19 final double probabilityOfDisablingEmptyValue = 0.5;20 final double probabilityOfAddingNotEmptyValue = 0.5;21 final double probabilityOfDisablingNotEmptyValue = 0.5;22 final double probabilityOfAddingBlankValue = 0.5;23 final double probabilityOfDisablingBlankValue = 0.5;24 final double probabilityOfAddingNotBlankValue = 0.5;25 final double probabilityOfDisablingNotBlankValue = 0.5;26 final double probabilityOfAddingDefaultValue = 0.5;27 final double probabilityOfDisablingDefaultValue = 0.5;28 final double probabilityOfAddingZeroValue = 0.5;29 final double probabilityOfDisablingZeroValue = 0.5;

Full Screen

Full Screen

handleGenericType

Using AI Code Generation

copy

Full Screen

1public class RPCController {2 public RPCController(){3 }4 public RPCController(String host, int port){5 }6 public String handleGenericType(String input){7 }8}9RPCController rpcController = new RPCController();10rpcController.handleGenericType("input");11RPCController rpcController = new RPCController("host", 8080);12rpcController.handleGenericType("input");

Full Screen

Full Screen

handleGenericType

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.problem.rpc;2import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;3import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;4import org.evomaster.client.java.controller.api.dto.problem.ProblemInfoDto;5import org.evomaster.client.java.controller.api.dto.problem.RestProblemDto;6import org.evomaster.client.java.controller.problem.ProblemParser;7import org.evomaster.client.java.controller.problem.RestProblem;8import org.evomaster.client.java.controller.problem.RestProblemHandlingUtils;9import org.evomaster.client.java.controller.problem.RestResourceCalls;10import org.evomaster.client.java.controller.problem.rpc.RPCEndpointsBuilder;11import org.evomaster.client.java.controller.problem.rpc.RPCResourceCalls;12import org.evomaster.client.java.controller.problem.rpc.RPCResult;13import org.evomaster.client.java.controller.problem.rpc.RPCResultBuilder;14import org.evomaster.client.java.controller.problem.rpc.RPCTargetBuilder;15import org.evomaster.client.java.controller.problem.rpc.RPCTargetInfo;16import org.evomaster.client.java.controller.problem.rpc.RPCType;17import org.evomaster.client.java.controller.problem.rpc.RPCTypeFactory;18import org.evomaster.client.java.controller.problem.rpc.RPCTypeHelper;19import org.evomaster.client.java.controller.problem.rpc.RPCTypeHelperImpl;20import org.evomaster.client.java.controller.problem.rpc.RPCTypeReference;21import org.evomaster.client.java.controller.problem.rpc.RPCTypeReferenceImpl;22import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariable;23import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableImpl;24import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableReference;25import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableReferenceImpl;26import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableReferenceType;27import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableType;28import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableTypeImpl;29import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableTypeReference;30import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableTypeReferenceImpl;31import org.evomaster.client.java.controller.problem.rpc.RPCTypeVariableTypeReferenceType;32import org

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful