0

My java compiler is complaining that my code uses "unchecked or unsafe operations". Any idea which lines are causing that in the two code snippets?

    @SuppressWarnings("unchecked")
private final void prepareChannelAccountStringsParserImplementedClassConstructor() {
    try {
        String channelAccountStringsParserImplementedClassName = channelIdentifier + "AccountStringsParser";
        Class channelAccountStringsParserImplementedClass = Class.forName(channelAccountStringsParserImplementedClassName);
        Class[] channelAccountStringsParserImplementedClassArguments = new Class[1];
        channelAccountStringsParserImplementedClassArguments[0] = String.class;
        channelAccountStringsParserImplementedClassConstructor = channelAccountStringsParserImplementedClass.getConstructor(channelAccountStringsParserImplementedClassArguments);
        channelAccountStringsParserImplementedParseMethod = channelAccountStringsParserImplementedClass.getMethod("parse", String.class);
        channelAccountStringsParserGetRequestIDMethod = channelAccountStringsParserImplementedClass.getMethod("getRequestID");
    } catch (ClassNotFoundException classNotFoundException) {
        NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB();
        notifyMeListenersLogsDB.writeDeveloperException(classNotFoundException);
    } catch (NoSuchMethodException noSuchMethodException) {
        NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB();
        notifyMeListenersLogsDB.writeDeveloperException(noSuchMethodException);
    }
}

    @SuppressWarnings("unchecked")
public synchronized final void requestChannelAccountsCompletionSuccess(String responseContent) {
    Object channelAccountStringsParserImplementedInstance;
    ArrayList<ChannelAccount> channelAccounts = null;
    String requestID = null;
    try {
        channelAccountStringsParserImplementedInstance = channelAccountStringsParserImplementedClassConstructor.newInstance(responseContent);
        channelAccounts = (ArrayList<ChannelAccount>) channelAccountStringsParserImplementedParseMethod.invoke(channelAccountStringsParserImplementedInstance, responseContent);
        requestID = (String) channelAccountStringsParserGetRequestIDMethod.invoke(channelAccountStringsParserImplementedInstance);
    } catch (InstantiationException instantiationException) {
        NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB();
        notifyMeListenersLogsDB.writeDeveloperException(instantiationException);
    } catch (IllegalAccessException illegalAccessException) {
        NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB();
        notifyMeListenersLogsDB.writeDeveloperException(illegalAccessException);
    } catch (InvocationTargetException invocationTargetException) {
        NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB();
        notifyMeListenersLogsDB.writeDeveloperException(invocationTargetException);
    }
    channelAccountStringsParserImplementedInstance = null;
    try {
        startChannelConnections(channelAccounts);
        isStarted = true;
        LoadBalancer.getInstance().sendSuccessAcknowledgement(requestID);
    } catch (NotifyMeListenersApplicationInitializationException notifyMeListenersApplicationInitializationException) {
        NotifyMeListenersLogsDB notifyMeListenersLogsDB = DatabaseController.getInstance().getNotifyMeListenersLogsDB();
        notifyMeListenersLogsDB.writeNotifyMeListenersException(notifyMeListenersApplicationInitializationException);
        System.exit(1);
    }
}

Of course, I put the @SuppressWarnings to suppress it, but I'd like to know the cause. Anyone can help?

ikevin8me
  • 4,253
  • 5
  • 44
  • 84
  • Recompile with `-Xlint:unchecked` for more detail. – obataku Aug 31 '12 at 19:39
  • 1
    Remove @SuppressWarnings("unchecked") that tells you which lines causing the issue. – kosa Aug 31 '12 at 19:39
  • 3
    Wow.. those are some long names! :) – Jonatan Aug 31 '12 at 19:42
  • I think it's that Class.forName line personally – Jason Sperske Aug 31 '12 at 19:44
  • [unchecked] unchecked call to getConstructor(Class>...) as a member of the raw type Class where T is a type-variable: T extends Object declared in class Class – ikevin8me Aug 31 '12 at 19:45
  • [unchecked] unchecked call to getMethod(String,Class>...) as a member of the raw type Class [unchecked] unchecked call to getMethod(String,Class>...) as a member of the raw type Class [unchecked] unchecked cast required: ArrayList found: Object – ikevin8me Aug 31 '12 at 19:46
  • I did put in the "-Xlint:unchecked" and got the details. So, now the question, so why doesn't solve the problem by catching a ClassCastException - I tried it does not work? – ikevin8me Aug 31 '12 at 19:47
  • One of the reasons IDEs were created was to answer those types of questions automatically. – Thomas Aug 31 '12 at 19:48
  • IMHO shorter variable names would be easier to read. – Peter Lawrey Aug 31 '12 at 19:48
  • 1
    Does it help if you replace `Class` with `Class>`? – halex Aug 31 '12 at 19:52
  • Yes, I put in Class> and it helped. However, this is still causing a problem: channelAccounts = (ArrayList) channelAccountStringsParserImplementedParseMethod.invoke(channelAccountStringsParserImplementedInstance, responseContent); [unchecked] unchecked cast required: ArrayList found: Object – ikevin8me Aug 31 '12 at 19:56
  • 1
    Never mind unchecked or unsafe operations, it's the verbose and repetitive operations you need to worry about ... – Tom Anderson Aug 31 '12 at 20:12

1 Answers1

1

Class is a parameterized class. you should use Class<?> (or something more specific if necessary). of course, the compiler error probably tells you exactly where the problem is.

Also, this line is an unchecked cast, but you have no choice on that one:

channelAccounts = (ArrayList<ChannelAccount>)channelAccountStringsParserImplementedParseMethod.invoke(channelAccountStringsParserImplementedInstance, responseContent);
jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • You mean no solution this this, and I have to live with the @SuppressWarning? – ikevin8me Aug 31 '12 at 20:01
  • 1
    @ikevinjp - for the result of the `invoke()` call, yes, you cannot solve that in java without an unchecked cast (reflection does not play nicely with generics). – jtahlborn Aug 31 '12 at 20:04