Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>HI,</p> <p>After searching through i found that dynamic typecast at runtime can't be performed. So what i was trying to figure out seems to be absurd.</p> <p>I was trying to reduce the cyclomatic complexity of a method1. I was trying to create a method2 which contains the generalized pattern of the repetition pattern found in method1 and calling the method2 from method1 wherever necessary...</p> <p>the pattern was like this in the first method..</p> <pre><code>if (obj instanceof Cust) { Cust c = (Cust) obj; Set cxSet = c.getCustPhonSet(); CustPhon cx; if (cxSet != null &amp;&amp; cxSet.size() &gt; 0) { Iterator cxSetIterator = cxSet.iterator(); while (cxSetIterator.hasNext()) { cx = (CustPhon) cxSetIterator.next(); this.stringProp(cx); } } //....pattern continues here... CustPhon is replaced by various classes like CustNam etc... Also getCustPhonSet by getCustNamSet etc... } </code></pre> <p>so i thought of writing a generalized method for the above pattern like this::</p> <pre><code>public void dynamicIteration(Set xlSet, String clsName) { if (xSet != null &amp;&amp; xSet.size() &gt; 0) { try{ Class clsinstance = Class.forName(clsName); Iterator itr = generalSet.iterator(); while(itr.hasNext()) { this.stringProp(clsinstance.cast(itr.next()));// See this is wrong.. thats y i posted here by using a simple Horse example } }catch(ClassNotFoundException e) { e.printStackTrace(); } } } </code></pre> <p>Calling method2 from method 1</p> <pre><code>//process customer email address Set cxSet = c.getCustPhonSet(); className = "pkg.CustPhon"; dynamicIteration(cxSet,className); // Similarly for other patterns </code></pre> <p>By this way i must be able to reduce the cyclomatic complexity</p> <p>This is what i was trying to do..</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload