crossminer / scava

https://eclipse.org/scava/
Eclipse Public License 2.0
18 stars 13 forks source link

Recommendations are related to the name of the variable and not to the type of the variable #397

Closed phkrief closed 4 years ago

phkrief commented 4 years ago

Hi, I try to get some recommendation on Hashtables by selecting in my code the following code: Hashtable<String, Object> table = (Hashtable<String, Object>)jsonObject;

I obtain 5 recommendation and non of them are talking about Hashtable but about ArrayList.

Now, if I rename the local variable 'table' to 'hastable" then I get some recommendations which could help me.

I would expect that the recommendations work on the type manipulated in the selected expression and not on the name of a variable.

Did I miss something? Thanks

phkrief commented 4 years ago

Btw, I would expect to get some helps on the Hashtable class APIs and I don't really get any. What should I do to make it happen? Thanks

jdirocco commented 4 years ago

Hi @phkrief , did you select the whole code (including the import section) from the compilation unit (i.e., the class) that you are developing? Can you share the query source code?

phkrief commented 4 years ago

Dear @md2manoppello , unfortunately, in this case it is worse.

Here is the whole code I have in my editor when I launch the API recommender:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;

import edu.grinnell.csc207.JSONUtils;

public class CurrentWeather {

    static String OpenWeatherAPI = "http://api.openweathermap.org/data/2.5/weather?q={city}&appid={app}";
    static String AppID = "abbcea2020f75409af198b98de40e3a6";

    public static void main(String[] args) {
        String city = "Toulouse";;
        if (args.length != 0) city = args[0];

        String api = OpenWeatherAPI.replace("{city}", city);
        api = OpenWeatherAPI.replace("{app}", AppID);

        String jsonString = getContent(api);
        try {
            Object jsonObject = JSONUtils.parse(jsonString);
            Hashtable<String, Object> table = (Hashtable<String, Object>)jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String getContent(String url) {
        InputStream in;
        StringBuilder input = new StringBuilder();
        try {
            in = new URL(url).openStream();
            Reader reader = new InputStreamReader(in);
            String line;
            BufferedReader read = new BufferedReader(reader);
            // get a line from the file
            while ((line = read.readLine()) != null)
              {
                input.append(line);
              } // parse until there are no more lines
            read.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return input.toString();        
    }
}

and here are the kind of recommendations I get if I select the code starting with ths import ad finishing with the Hashtable cast:

None of them are related to any Hastable. :-( Thanks

davidediruscio commented 4 years ago

I think this can be closed. @md2manoppello @phkrief isn't?