I am working on a project that needs an API. In most APIs (for example Minecraft Modloader), the API runs the "mod" class, without knowing its name. How is this possible? For this project, I need to get all instances of a class called Spell, without ever calling them directly. All tips and answers are appreciated. Thanks!
I am pretty sure what forge modloader and risu's modloader do is look inside the zips and find any classes with what it needs to be considered a mod class, for example in forge, a #mod annotation. Then it will attempt to load that mod. This is actually how many mods like buildcraft have different parts of the mod in the same zip.
Related
For example i want customize BluetoothGatt class. Is it possible to create android.bluetooth package and put own version of this class?
BluetoothGatt uses android interface files shown in here. Can i access these files and use it in own version of BluetoothGatt?
Yes as long as you match the package and class name, when you reference it in your code, you will be able invoke your custom behaviour / modified contract
Android classes are simply Java. You can modify the support SDK, as it is simply a Java file. However, this is only applying to your app, or it would be possible for apps to change the entire SDK, which is defeating the point of sandboxing. If you want to extend the class, that is done just the same as in Java, as the class is just Java. However, this specific class is final, so you can't, at least not without hacks. However, the support library is usually a wrapper, so you may as well write your own. If the reason you want to do this is to access a private method, use reflection!
EDIT: To pedantically answer your question, you have already accessed said files, and copy-and-paste is always your friend!
Hope I helped!
Rewriting this for greater clarity.
R.java is a great resource in Android. It's basically cheating, by dynamically changing at compile time to provide access to all of your resources.
It would be awesome to have something like this available for applications running core Java. However, I understand that R.java does some crazy hackery to make this work.
So, here are my goals. If anyone has any ideas to point me in the right direction, that would be great.
1) Public static fields that link to resources defined by the developer.
2) The fields are created dynamically, ie they are not hard-coded into existence.
3) The dynamic creation occurs at compile time, so the following statement would be accepted by the compiler: MyRJava.resourceName.
Is this even remotely possible? Where would I start looking to see if it can be done?
Java doesn't support this, but Groovy does. As a Java developer, you should have no problem picking up Groovy for something like this. One of the nice things about Groovy is that it coexists very well with Java code. Where I work, we frequently go back and forth between the two languages.
Remotely possible. We were successfully using APT and Maven to set up a compile time goal which generates additional Java classes. This however not a real dynamic class generation, as we're doing everything in compile time.
The advantage is that after saving (and the code generator has run), you can use all the genarated fields normally (i.e. in autocomplete, and so on).
It is also possible to create something similar in runtime, i.e. you can create a class file, compile it, and then get access to its fields and methods via reflection. In this case however accessing to the genrated fields are more problematic - and finally you'd better go with a simple Map to hold "resource ids" like R.java.
I've been messing with Android for a couple of weeks, i found many tutorials to follow, but i didnt find anywhere some "Style rules" to make the code looks better.
I would like to know if its possible (im sure that it is, but dont know how to make it xD) to use more .java files to organize the functions. I mean, right now, i have myApp.java where i coded all my application, but is starting to grow so much, so i would like to separate some functions into another .java file.
As i told before, im almost sure that this is possible, but i dont know how to link that second file so, can anybody help me?
Thank you in advance :)
If I understand you correctly you haven't really learned how to use classes in your application? My suggestion is to do a Google search 'Java for beginners' and look for references to Classes and objects.
You normally don't "link" a file in Java as opposed to some other programming languages. In Java you have java files that compile to class files and use them by creating instances of them like so.
MyClass instance = new MyClass();
Where MyClass is defined in a file called MyClass.java (and located in the same package/folder as your main application). If you are unsure about package another Google search can illustrate how to use them.
If you are using Eclipse, it can help you with this. You can create a class and use it by creating a new instance of it in your main application.
You're talking about separation of concerns - you should examine your application design and have classes where the functionality is broken down into logical units per class.
If you're talking about static methods, where you wish to call some functionality which doesn't rely on the state of an object, then perhaps a utility class could be appropriate.
The java.lang.Math is an example, where all the methods on the (final) class are static. Ideally you would just import the methods you'd want to use in your code using the import static keywords.
I have a functionality that I wish to provide to a customer for a software mockup that we are preparing - and I want to know if it's
possible
intelligent (a.k.a. not stupid)
the best thing
I want the customer to be able to write a java class that implements my Computable interface and stick it in some predetermined folder. This folder will contain the .java files rather than .class files. Then, at runtime, I want my program to search that folder and extract all of the Computables from that folder and store them in a map from the name of the Computable to the Computable object. The Computable should only have a default constructor and the it interface will only have one method called compute which maps an array of Object to an Object.
The Java Compiler API introduced in Java SE 6 should give you what you need.
You may find Google Reflections useful to find classes implementing/extending a certain interface/superclass in the classpath. It's then as straightforward as
Reflections reflections = new Reflections("my.project.prefix");
Set<Class<? extends SomeClassOrInterface>> subTypes = reflections.getSubTypesOf(SomeClassOrInterface.class);
Then, to test if it indeed has a no-arg default constructor, just check for each if Class#newInstance() doesn't throw any exception.
There are several suggestions provided as answers to this question.
Here too On-the-fly, in-memory java code compilation for Java 5 and Java 6
If it's easy enough to compile at runtime that would be fine.
You can use javax.tools to do the compilation as needed. Create dynamic applications with javax.tools may help, too. It's also possible to do it in memory.
One caveat: using the compiler creates a dependency on the JDK; the JRE alone is insufficient.
take a look: Find Java classes implementing an interface
I think this would be simpler if you allowed your customer to type in a code declaration using something like Groovy, which is Java-ish enough, and easy to execute at runtime from a String value.
It's easy enough to iterate through the list of files in a folder. Someone mentioned that it's possible to call the Java compiler from Java (if you re-distribute the JDK, which I think is a point whose legality needs checking!!) That's much of the battle.
You seem to have a fixed model in your mind where only files fulfilling a certain interface are extracted from the folder. I think this is where your method needs to give a little. The sensible way (IMO) to do this would be to compile all files in that folder, and then with their classes stashed away somewhere, you can load and reflect them and then determine which of them "do" the interface and which don't. Those that don't will have been needlessly loaded into your JVM, but unless it's intentionally very space-wasteful, code you don't execute can't harm your program.
Having determined which ones do the computable thing, you can then store those classes (or instances thereof) in a Collection and do whatever you like with them. You simply ignore the other ones.
You could use BeanShell. This library is small and doesn't require the JDK. It is used in a number of IDE and web servers. The latest version appears to have the support you need loading .java files from the class path. (Still in beta)
The title speaks for itself. The language is Java.
Yes, there is. This is however a tedious and expensive work. You need to crawl through all class files and all JAR files with help of ClassLoader#getResources() and a shot of java.io.File and load all classes of it with help of Class#forName() and finally check if the method is there by Class#getMethod().
However, there are 3rd party API's which can take the tedious work from hands, but it is still expensive, because loading a class would cause its static initializers being executed.
A cleaner way is to make use of annotations and annotate the methods in question and then make use of libraries which searches for classes/methods/fields based on the annotations, such as Google Reflections.
On the other hand, if the entire package name or the JAR file name is known beforehand, then the work will be less tedious and expensive (no need to do stuff recursively nor to load the all of the classes of entire classpath).
Update: I remember, I ever wrote sample code to achieve something like that, you can find it here. It's good to start with, you only need to change it a bit to check the method.
No, you can't, in general. If you could get a complete list of available classes you could check each of them using reflection - but you can't ask a classloader for a list of everything that's available. (For instance, it may be fetching classes over HTTP, and may not know all the files available.)
If you knew that you were interested in classes in a jar file, however, you could open the jar file, find all the class files within it and ask the classloader for those classes. It would be somewhat fiddly.
What's the bigger picture here? There may be a better way to approach the problem.
Also, in Eclipse, you can simply ask for this :
Clic on the method, and type Ctrl-T.