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: API Hell

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default API Hell

    Hey guys. So, I am working in an environment where certain API's may or may not exist depending on the scenario. I have classes that implement certain interfaces from API's which might not always exist. The thing is: my classes don't DEPEND on those interfaces. I just want to implement them if I can. Sounds like an easy fix: java.lang.reflect.Proxy. Problem with that.. heres some example code:

    class MyClass
    {
        public void methodFromTheInterface();
    }
     
    MyClass c = (MyClass) ReflectionHelper.createProxy("someInterface", new MyClass())
     
    //so far so good...
     
    SomeAPIThatUsesReflection.registerMyClass((someInterface) c); //NoClassDefFoundError because of the cast.. referencing the interface...

    Since "registerMyClass" takes a "someInterface", I have to cast it...
    And distributing the api in my jar is not (afaik) a viable solution... because what happens if there are 2 of the same class (interface in this case)...

    What I have learned so far: API's suck (dynamic, runtime API's).

    I will keep on this and try to find a solution...
    If anyone can help, please let me know! Thanks!

    Edit: I just realized something... I could call the method with reflection to avoid casting. hmm... should work. Will try later.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: API Hell

    The thing is: my classes don't DEPEND on those interfaces. I just want to implement them if I can.
    That sounds counter-intuitive. If you want to implement an interface, the class implementing the interface is dependent on the interface, otherwise: how does it know what it is implementing?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: API Hell

    Perhaps I should word this better... My code will work with or without the interface, the interface is simply a way to add an "extra feature" to the class. It is not required for the class to function properly.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,107
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: API Hell

    Having a class implement an interface has little to do with a class functioning properly. It has to do with telling a method in another class what the class can do so that the compiler can validate that contract between the method and the instance of the class that is being passed to it.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,619
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: API Hell

    Ok... I feel you are confusing the English definition of "depend" with what it means in java when we talk about "dependencies". Let me try to explain this the best I can.
    In java, when you say that Class A depends on Class B, you are creating the relationship where Class A is aware of the existence of Class B, and that it has access to Class B's compiled code. Class A depending on Class B does not guarantee Class A inherits from Class B or anything, but rather that Class A uses Class B for some reason (could be inheritance, or Class A might simply have a variable of type B ).
    So you say that MyClass works without SomeInterface, which would mean MyClass makes no reference to anything SomeInterface related. Nothing wrong with that. Then you say you are adding some extra feature, which is coming from SomeInterface (am I correct?). When you add the extra feature to MyClass, MyClass becomes "dependent on" SomeInterface.
    You may get this to work with reflection, but reflection should always be considered a last-resort option, since it is an inconsistent means of breaking normal java convention. There is absolutely no guarantee that reflected code will be portable, which oracle even warns about. For example, there are some security restrictions which may prevent reflection from even working. These security restrictions will result in client-dependent errors and crashes. The lack of stable portability (which is arguably the entire purpose of java) is the main reason reflection is discouraged.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Java giving me all the hell!!
    By Mar5000k in forum Member Introductions
    Replies: 1
    Last Post: October 10th, 2012, 07:33 PM
  2. Where the hell is the error coming from. . . ?
    By Yunus Einsteinium in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 13th, 2012, 12:01 PM
  3. [SOLVED] SQLServer Hell
    By xyrer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 21st, 2010, 04:46 PM
  4. Hell world :)
    By Kassino in forum Member Introductions
    Replies: 2
    Last Post: May 10th, 2010, 08:48 AM