Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6124451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:08:04+00:00 2026-05-23T16:08:04+00:00

I have written code to cast the object to the required type. And if

  • 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);          
   }

}
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:08:05+00:00Added an answer on May 23, 2026 at 4:08 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code: public class NewClass2 implements Comparator<Point> { public int
I have written code that automatically creates CSS sprites based on the IMG tags
I have written code that opens 16 figures at once. Currently, they all open
I have written code in Java to access web cam,and to save image... I
I have written some code in my VB.NET application to send an HTML e-mail
I have written jQuery code, in files Main.html and ajax.php . The ajax.php file
I have written some code for playing a .wav through my application. Now I
I have written this code to join ArrayList elements: Can it be optimized more?
I have written some code to ensure that items on an order are all
I have written some code to look at properties using reflection. I have retrieved

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.