History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: IDEA-16538
Type: Bug Bug
Status: Open Open
Assignee: Eugene Vigdorchik
Reporter: AlexL
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
IDEA: Feedback

Generify... removes needed type cast while generifying... List<Integer> returned from a raw Map<K,V>

Created: 27 Nov 07 23:07   Updated: 28 Nov 07 18:49
Component/s: Refactoring

Environment: Windows XP SP2

Build: 7,573
Severity: High


 Description  « Hide
In this example there is a raw Map<K,V>.
If I run Generify... on this code, IDEA creates a syntax error.
TestGenerifyProducesBadCode.java
package org.intellij;

import java.util.*;

/**
 * User: alex
 */
public class TestGenerifyProducesBadCode
{

    /*
       I think the fact that this method is unused is possible
       confusing IDEA? Still there is enough information there
        to determine the type of ratingMap as Map<String,List<Integer>>
     */
    protected static int unusedMethod(
        Map ratingMap
    ) {
        String[] nameA = new String[] {"Irida","Demetra","Selena"};

        int highestScore = 0;
        for (String name : nameA) {
            List ratings = (List) ratingMap.get(name);
            if (ratings == null) {
                ratings = new ArrayList();
            }
            if (ratings.size() != 0) {
                int score = getMax(ratings);
                if (score > highestScore) {
                    highestScore = score;
                }
            }
        }
        return highestScore;
    }

    public static int getMax(List<Integer> events) {
        int max = 0;
        for (Integer score : events) {
            if (score > max) {
                max = score;
            }
        }
        return max;
    }
}

If I run Generify.. on it, IDEA creates a compile error.

TestGenerifyProducesBadCode.java - After Generify with IDEA 7343
package org.intellij;

import java.util.*;

/**
 * User: alex
 */
public class TestGenerifyProducesBadCode
{

    /*
       I think the fact that this method is unused is possible
       confusing IDEA? Still there is enough information there
        to determine the type of ratingMap as Map<String,List<Integer>>
     */
    protected static int unusedMethod(
        Map ratingMap
    ) {
        String[] nameA = new String[] {"Irida","Demetra","Selena"};

        int highestScore = 0;
        for (String name : nameA) {
            List<Integer> ratings = ratingMap.get(name);
            if (ratings == null) {
                ratings = new ArrayList<Integer>();
            }
            if (ratings.size() != 0) {
                int score = getMax(ratings);
                if (score > highestScore) {
                    highestScore = score;
                }
            }
        }
        return highestScore;
    }

    public static int getMax(List<Integer> events) {
        int max = 0;
        for (Integer score : events) {
            if (score > max) {
                max = score;
            }
        }
        return max;
    }
}

If I use Eclipse 34m2, does almost the same as IDEA, except it puts a cast in where IDEA is creating
a compile error.

TestGenerifyProducesBadCode.java - Eclipse 34m2 Infer Generic Type Arguments
package org.intellij;

import java.util.*;

/**
 * User: alex
 */
public class TestGenerifyProducesBadCode
{

    /*
       I think the fact that this method is unused is possible
       confusing IDEA? Still there is enough information there
        to determine the type of ratingMap as Map<String,List<Integer>>
     */
    protected static int unusedMethod(
        Map ratingMap
    ) {
        String[] nameA = new String[] {"Irida","Demetra","Selena"};

        int highestScore = 0;
        for (String name : nameA) {
            List<Integer> ratings = (List<Integer>) ratingMap.get(name);
            if (ratings == null) {
                ratings = new ArrayList<Integer>();
            }
            if (ratings.size() != 0) {
                int score = getMax(ratings);
                if (score > highestScore) {
                    highestScore = score;
                }
            }
        }
        return highestScore;
    }

    public static int getMax(List<Integer> events) {
        int max = 0;
        for (Integer score : events) {
            if (score > max) {
                max = score;
            }
        }
        return max;
    }
}

Looking at the code, I thought IDEA could fully Generify.. the code. If IDEA had generified..
the ratingMap parameter to Map<String,List<Integer>>, then it wouldn't have needed the cast.
I think at some point IDEA thinks it has ratingMap parameter parametized, and so it drops the
cast, but then later for some reason it doesn't end up Generifying ratingMap, which causes the
problem.

TestGenerifyProducesBadCode.java - Expected Behavior
package org.intellij;

import java.util.*;

/**
 * User: alex
 */
public class TestGenerifyProducesBadCode
{

    /*
       I think the fact that this method is unused is possible
       confusing IDEA? Still there is enough information there
        to determine the type of ratingMap as Map<String,List<Integer>>
     */
    protected static int unusedMethod(
        Map<String,List<Integer>> ratingMap
    ) {
        String[] nameA = new String[] {"Irida","Demetra","Selena"};

        int highestScore = 0;
        for (String name : nameA) {
            List<Integer> ratings = ratingMap.get(name);
            if (ratings == null) {
                ratings = new ArrayList<Integer>();
            }
            if (ratings.size() != 0) {
                int score = getMax(ratings);
                if (score > highestScore) {
                    highestScore = score;
                }
            }
        }
        return highestScore;
    }

    public static int getMax(List<Integer> events) {
        int max = 0;
        for (Integer score : events) {
            if (score > max) {
                max = score;
            }
        }
        return max;
    }
}


 All   Comments   Work Log   Change History      Sort Order:
There are no comments yet on this issue.