0

I have written code to cast the object to the required type. And if the required type is a custom class object and it has another object we need to cast it recursively. BTW: I will know that I need to construct an object if the input is a hashMap. If in side a HashMap if another hashMap is there then I need to understand that its object inside an object. And inside object I need to build from the inner hashMap. To build it I will call the method recursively. Code I have depicted here. But these 2 classes Castor and MyBuilder both have become in cycles. I am not getting how to break it. If the method is not cyclic we can break dependecy. But with cycles can any one help? Any pattern can I introduce or how can I refactor this?

Thanks in advance.

Code is something like this : For faster access to cycle pl refer to : returnValue castPrimitive( .... and void setParameterToEntity.....

MyBuilder myBuilder = new MyBuilder();

class Castor {
public Object castToRequiredType( Type type, Object object) {
    String typeString = type.toString();
    Object returnValue = null;
    if (myUtils.isTypePrimitive(typeString)) {
        returnValue = castPrimitive(object.toString(), typeString);
    }else if {
 // some conditions and some casting logic.
}
else {
        returnValue = myBuilder.buildCustomObject(this,typeString, object);
    }
    return returnValue;
   }
  // other methods which call castToRequiredType() recursively.
}


 class MyBuilder{
 buildCustomObject(Castor castor,
          String classOfType, Object object){
     Class<?> loadedClass = myUtils.loadClass(classOfType);

        instance = loadedClass.newInstance();
        HashMap<?, ?> myMap;
        List<Method> declaredMethods = myUtils.getMethodsForClass(loadedClass);
        for (Method method : declaredMethods) {
            if (object instanceof HashMap<?, ?>) {
                myMap = (HashMap<?, ?>) object;
        // ITERATE THROUGH MAP AND CALL THE SET PARAMETER TO ENTITY METHOD.
                    }
                }
            }
     return instance;
}

 void setParameterToEntity(Castor caster,
Object instance,  Method method,  Object value) {
    ype[] parameterTypes = method.getGenericParameterTypes();
    Object castedValue = caster.castToRequiredType(
    parameterTypes[0], value);          
   }

} }

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
java_enthu
  • 2,279
  • 7
  • 44
  • 74

1 Answers1

0

This section seems suspect to me - you are iterating over each method and for each one you are invoking setParameterToEntity

for (Method method : declaredMethods) {
        if (object instanceof HashMap<?, ?>) {
            myMap = (HashMap<?, ?>) object;
    // ITERATE THROUGH MAP AND CALL THE SET PARAMETER TO ENTITY METHOD.
  1. You are ignoring the key in the hash map and just passing in the value (see setParameterToEntity sig)

  2. You pass the same object in to each and every method in the class you are building .

I assume your hashmap is a list of setter names and the associated value, so shouldn't you be iterating over the hashmap keys and for each one identify the Method, and then invoke the Method with the value from the hashmap as the parameter. To support multiple parameters you would need another hashmap.

Since this code iterates over an object graph in hashmaps, and produces another object graph, cycles should appear only if the hashmaps are clever enough to store references to other elements within the hashmap, or you have a mistake in the code like a) passing in the same object instead of elements from the object (causing it to spin), or b) you have a bug where you are iterating over the object being built rather the hashmap graph.

Given the code snippet above, I think the problem is both :), so iterate over the hashmap not the declaredMethods as I said above, and make sure you pass the Method (determined from the hashmap key), and value from the hashmap to the setParameterToEntity, not the hashmap itself.

memetech
  • 1,039
  • 7
  • 9