Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Struggling with generics

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Struggling with generics

    Hi all, this is my first post here. After not doing any much Java for 15 years (mostly C and C#) I decided to revisit my old Java projects. I'm using OpenJDK 11.0.12 because Microsoft was kind enough to install this on my system without even telling or asking me. Ah well, it seems to work. Previously I used a JDK from Sun (before Oracle took over).

    But upon compiling my old code I am getting tons of warnings about code using raw types like Vector, Stack, Hashtable etc. Despite generics being new to me the first few were easily fixed (I passionately hate warnings).
    However the next one up is stumping me. I have this code (exception handling removed for clarity)

        Class cls = Class.forName(className, false, classLoader);
        Method getComponentMethod = cls.getMethod("getComponent", (Class[])null);

    and the compiler gives this warning about the second line:

    chris\utilities\cMenu.java:875: warning: [unchecked] unchecked call to getMethod(String,Class<?>...) as a member of the raw type Class
            getComponentMethod = cls.getMethod("getComponent", Class[])null);
                                            ^

    I vaguely feel what the compiler's point is, but I'm at a loss of how to fix this warning. A gentle nudge would be much appreciated.
    Last edited by cbreemer; January 7th, 2022 at 06:02 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Struggling with generics

    When all else fails, suppress it:
    @SuppressWarnings({"rawtypes", "unchecked"})
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Thumbs up Re: Struggling with generics

    Good tip Norm, thanks. I don't like suppressing warnings, but fixing them all in the code is getting like a huge pain in the butt. And I admit I WAS compiling with -Xlint:unchecked (after a suggestion to that effect in the very first compilation). Taking that out helps a lot I'll see what happens if I just ignore these warnings, with a bit of luck it might just all work.
    Much as I love Java, it seems to have grown even more pedantic over the years. I find C# a lot more pragmatic.
    Last edited by cbreemer; December 30th, 2021 at 05:31 PM.

  4. #4
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Struggling with generics

    use wildcard Class<?>

    import java.lang.reflect.Method;
     
    class Class_forName_test {
      public static void main( String[] args) throws Exception {
        String className = "javax.swing.JMenu";
        ClassLoader classLoader = null;
     
        Class<?> cls = Class.forName(className, false, classLoader);
        Method getComponentMethod = cls.getMethod("getComponent", (Class[]) null);
     
        System.out.println( getComponentMethod );
      }
    }

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Thumbs up Re: Struggling with generics

    Quote Originally Posted by zemiak View Post
    use wildcard Class<?>
    Awesome, that does it. Many thanks !!!

Similar Threads

  1. Struggling with Java :o
    By Jabdennel in forum Java Theory & Questions
    Replies: 2
    Last Post: January 7th, 2022, 05:56 AM
  2. Struggling with an Array of Arrays
    By bhenry1790 in forum Loops & Control Statements
    Replies: 6
    Last Post: July 23rd, 2012, 09:38 AM
  3. Struggling with if and if else
    By spongyballs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 21st, 2011, 09:09 PM
  4. Hi from UK, Struggling with java!
    By Sneak in forum Member Introductions
    Replies: 6
    Last Post: December 14th, 2009, 09:39 AM
  5. Replies: 3
    Last Post: June 26th, 2009, 11:06 AM

Tags for this Thread