How to make custom ClassLoader to be returned by .getClassLoader()? - class

Need your assistance. I created 2 custom ClassLoaders to load class files. But when I try to use .getClassLoader() on class I loaded, I saw ApplicationClassloader every time.

ClassLoader in Java works on three principle:
delegation,
visibility
uniqueness
Delegation principle forward request of class loading to parent class loader and only loads the class, if parent is not able to find or load class. Visibility principle allows child class loader to see all the classes loaded by parent ClassLoader, but parent class loader can not see classes loaded by child. Uniqueness principle allows to load a class exactly once, which is basically achieved by delegation and ensures that child ClassLoader doesn't reload the class already loaded by parent.
The class you are trying load should be there in your class path. Please check your application classpath
Read more: here

It means that your class is indeed defined by the application class loader, not your custom one. Your custom class loader is just initiating class loader that delegates to its parent (application class loader). Make sure the custom class loader defines the class itself.

Related

Implement an interface that is exposed by a component in a class inside the component and show it in the corresponding class model

I have a Component in a component diagram that provides several interfaces. Now i'd like a class in a class diagram that describes this component to implement this interface.
What i tried:
Drag the interface into the class model --> results in the message 'When dropping embedded elements to a diagram, you must drop them on theit correct owner.'
Creating the interface in the class model, then trying to link it with the interface in the component model --> I didn't find a way to accomplish that.
Adding the class to the component model and add a realization to the exposed interface. This results in pretty arrows, but the class is in the component model. If I remove it from the model, the interfaces are still in the linked element list of the class, but i have found no way to add the interfaces to the model then, neither show in another way that this class implements those interfaces.
Any proposals how to accomplish that?
The second way DOES work. I now found the [...] button beside the textfield where to enter the name of the interface. It is possible to add an existing interface there.

How to get data from Application class to PhoneStateListener class?

I have an Android app that has an Application class and I have another class that extends PhoneStateListener.
I would like to use one of the objects from the Application class in my class that extends the PhoneStateListener. I wasn't able to do so because it the PhoneStateListener class doesn't extend Activity.
Is there any way I can use the object from the Application class within my PhoneStateListener class?
See this example and this Question
Extend PhoneStateListener and have a constructor that takes what it wants as argument.

How do I share a method between two classes?

I have an iPhone app that uses a Tab Bar Controller with 3 tabs. Each tab is a separate class. There are several methods that are identical in each class. Instead of having three copies of the same method, I'd like to share the method between the classes. However, I have not figured out how to do this.
Thanks.
This is a classical case of inheritance. Create a base class, and put all things that are common across the classes you are trying to build, into it, both functions and data members. Then, derive your three classes for your tabs from this class (inherit from it, or make it the parent class, lots of overlapping terms here that people generally throw around). Make sure your methods in the parent class are NOT defined as private methods. That would make them inaccessible to your child classes. Hope that helps!
Here you can make a new class and define that method inside that class which you want to share between more than one class.
Now whenever you want to access that method, just import the class and you can use the same method in multiple classes.
Let me know if you need more help.

Dynamic include classes OR Instantiate objects without including class

Basically I have a load of classes which are all subviews of UIView.
The app is a (sort of) slideshow application. I want to be able to make lists of potential "routes" that the slides could take in plists.
As such I may have 100s of classes and I don't want to have to include all of them in the potential that I may use one of them once.
So is there a way around this? Can I instantiate a class without including it somehow?
Or am I going to have to include every potential class I'm going to use?
Is there a "global include" like include all... clutching at straws here. :p
Thanks
Tom
Why would you have hundreds of classes? It sounds like you have roughly one custom class, which would represent a slide.
Your comment on the question helps. Consider separating the slide from its content. You could have a single slide class which provides the functionality common to all slides, including the ability to manage one or more content objects. Then you'd create a bunch of different content classes: spreadsheet, animatable graph, checklist, whatever. A slide would look at its data to figure out which content class to instantiate.
If you have a common base class for all your content classes, the slide class only needs to know about the base class. The content base class could act as the manager for all the content classes. You could give it a factory method like -contentForData:(NSData*)data that returns an appropriate content object for the given data. The slide class doesn't need to know about anything more than the content base class then, so this sort of accomplishes your goal of instantiating your content classes without having to include all their headers in your slide class. The content base class would, of course, still have to know about all the content classes.
It gets a little tricky with the base class needing to know about its subclasses but the subclasses being derived from the base class. I think you can get around this by using a forward #class declaration in the content subclasses.
If you're using the class, you must know what set of messages it responds to. Factor out that information into either a common superclass or a protocol, and then you only need to use that.
Can I instantiate a class without including it somehow?
I guess this is not possible because compiler should definitely see class definition in the current scope before using a variable of class type.
IF I understand your question correctly, you want a single place to put your #include directives so that every class in your project has access to them. You can do this in the ProjectName_Prefix.pch file. Any #include or #import statements there will be included in every source code file.

Is it possible to share code betwee classes that does not have the same parent?

I have two classes that are already subclasses of a different parent (due to an external library I cannot change this). However I want to share some common code between them, say for example, code that handles an action after a popup dialog etc. How do I do this?
Refactor the shared code into its own class, and include an instance of that class as a field/property within your other two classes.
You can re-factor the appropriate code into a utilities class, and then have the two classes call it. As for the iPhoneSDK, you can probably have the utility method be the delegate method itself.
You could write a category on a common ancestor class. Then both classes could import that Category and call the common code.