Results 1 to 8 of 8
  1. #1
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default How to check a class name against a class?

    Let me clarify!

    Firstly, consider the following definitions:

    Java Code:
    public interface MyInterface
    {
      ...
    }
    
    public class MyFirstClass implements MyInterface
    {
      ...
    }
    
    public class MySecondClass implements MyInterface
    {
      ...
    }
    
    public class MyThirdClass
    {
      ...
    }
    Scondly, let's say I have a text file called "myfile.txt", the contents of which looks like this:


    java.lang.String
    java.util.Vector
    MyFirstClass
    MySecondClass
    MyThirdClass


    Thirdly, consider the following code snippet (which I haven't actually written but which is presented here simply to illustrate my question, rather than demonstrate good/bad coding!):

    Java Code:
    String[] lines = readFile("myfile.txt");  //Arbitrary method that reads myfile.txt into an array of strings - one per line - i.e. ["java.lang.String", "java.util.Vector", etc...]
    
    for (int n=0; n<lines.length; n++)
      System.out.println(
        isMyInterface(lines[n])  //This is the bit that I need help with!
      );
    Look at the bit that I need help with (above) - the function isMyInterface needs to inspect the string that is passed to it and return true if it represents the name of a class that implements MyInterface or false if it does not.

    In other words, the code above should output:


    false
    false
    true
    true
    false



    Please can someone tell me how to write the code for isMyInterface? Or point me in the right direction? I am thinking there's something in the reflection package to do it, but I'm not sure ...

    Many thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Yes, there are probably classes and methods in the reflection package to solve your problem.

  3. #3
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Yes, there are probably classes and methods in the reflection package to solve your problem.

    Please use the following two words in a well-known sentence:

    "chocolate" and "teapot".

    :rolleyes:

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by XmisterIS View Post
    Please use the following two words in a well-known sentence:

    "chocolate" and "teapot".

    :rolleyes:
    The main entry point for reflection/introspection is the Class class; it has a method that determines whether or not the current Class object is an interface; read the API documentation for that class.

    kind regards,

    Jos

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I second Norm and Jos's recommendations. A vital skill to master if you want to learn to code in Java is how to glean information from the API and the Java tutorials. If after review of these you're still stuck, then by all means, come on back and let's see if we can help you.

    Best of luck!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    The main entry point for reflection/introspection is the Class class; it has a method that determines whether or not the current Class object is an interface; read the API documentation for that class.

    kind regards,

    Jos
    Even better, if I've read the OPs requirements correctly, it has a method which returns all the interfaces implemented by the class.

  7. #7
    XmisterIS is offline Member
    Join Date
    Aug 2010
    Posts
    35
    Rep Power
    0

    Default

    Many thanks for the replies! My comment about the chocolate teapot was made in wry jest ;)

    I've fixed it, many thanks. :)

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

Similar Threads

  1. How to check instance of a generic class?
    By chan_nguyen in forum New To Java
    Replies: 7
    Last Post: 09-08-2010, 05:20 AM
  2. Give Value to other class to check database
    By dread_arisawa in forum New To Java
    Replies: 1
    Last Post: 06-23-2010, 10:52 PM
  3. check me my class please
    By javanew in forum New To Java
    Replies: 2
    Last Post: 05-05-2010, 03:58 AM
  4. JDBC class check
    By DavidBainbridge in forum New To Java
    Replies: 4
    Last Post: 08-17-2009, 01:52 PM
  5. Replies: 7
    Last Post: 05-06-2009, 01:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •