Is there some way to tell java to think that Object passed to reflected method implements interface of input attribute of method?
public class Debuger /*implements IDebuger*/{
...
}
and this Debuger I need to use in reflected method someDocument.attachDebuger(IDebuger).
I know the structure of IDebuger interface, but I cant just simply write implements IDebuger since it is not in my project.
I want to be able to call something similar
Debuger dbg = new Debuger();
Class theClassINeedToImplement = ...;
Object document = ...;
Class docClass = document.GetClass();
/*
HERE call something like
Object Idbg = theClassINeedToImplement.ForceImplementInterface(dbg);
*/
Method m = docClass.getMethod("attachDebuger", theClassINeedToImplement);
m.invoke(document, Idbg);
I think you need to import IDebuger to your project and then implement it with Debuger. Otherwise the method won't know it is being given an IDebuger, and that will cause a compile error.
Even if its in an external jar, you can import it and use the interface. In Eclipse, right click the project, then select build path, then add external archives. I hope that works.
Related
I am in a situation that I want to do some rewriting on loaded,i.e., currently running application class. I do not want to rewrite loaded library class. Thus I need to sort of filter the rewriting based either on the type of the class, being application or none application class, or another way I could do it is by checking the ClassLoader and see if it is of Application Class type.
To give some context let's assume I have the following code
URLClassLoader urlcl = new URLClassLoader(cp);
Class c = urlcl.loadClass(_className);
Assuming that _className is the current running class, that was intercepted by a listener, how can I know if this class c is an application class or not?
Much appreciated!
I'm not entirely sure of what do you mean by application class, but those hints still might be helpful.
You can simply check if one class is subtype of another with:
public static boolean is1stSubTypeOf2nd(Class clazz1, Class clazz2) {
return clazz2.isAssignableFrom(clazz1);
}
If you would like to check if the class belongs to some package (to check if it is the class from standard API, third party library or not), you can use:
public static boolean isInPackage(Class clazz, String packageName) {
return clazz.getPackageName().contains(packageName);
}
Further the standard API is able to provide you an info about all super classes of given class.
I'm implementing a plugin in which I need to add extends clause for an existing class.
I have PsiClass instance representing say MyClass.
There is an API that allows to get all the classes that MyClass extends:
PsiReferenceList extendsList = psiClass.getExtendsList()
And theoretically I can add something to it and that will work.
Problem: PsiReferenceList.add() consumes PsiElement and I don't know how to create an object of PsiElement having fully qualified name of the class I want to use.
More specifically, how to transform string com.mycompany.MyAbstractClass to PsiElement representing this class?
Update:
I managed to achieve the result using the following logic:
PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
PsiReferenceList extendsList = aClass.getExtendsList();
PsiShortNamesCache instance = PsiShortNamesCache.getInstance(project);
PsiClass[] abstractClasses = instance.getClassesByName(
"MyAbstractClass",
GlobalSearchScope.allScope(project)
);
PsiJavaCodeReferenceElement referenceElement = factory
.createClassReferenceElement(abstractClasses[0]);
extendsList.add(referenceElement);
It works but I guess there should be more optimal way.
You can make a String which is the code you want to generate, like
String code = "class A extends B { }"
Then, use this code to convert text into PsiElement:
PsiElement fromText(String code, Project project) {
return PsiFileFactory
.getInstance(project)
.createFileFromText(JavaLanguage.INSTANCE, code)
.getFirstChild()
}
And you'll get the corresponding PsiElement.
Then, myClass.replace(fromText(code)).
BTW you can also do classNamePsiElement.addAfter(fromText("extends Xxx")) which is considered more efficient.
This is from the Minecraft server source code, also called the Minecraft Bukkit API, now you know as much as I do.
There is an interface called Server:
public interface Server extends PluginMessegeRecipient {
public String getVersion();
}
PluginMessageRecipient is an interface also.
There is a class called Bukkit that instantiates Server:
public final class Bukkit {
private static Server server;
}
Inside methods in the Bucket class they invoke methods from the server object. For example:
server.getVersion();
The thing is, there is no code for getVersion in the Server interface, just a method signature. There is also no code in the PluginMessageRecipient interface nor does it extend anything.
I have read all the questions and answers on SO that say I need an anonymous class or an inner class and this does not seem to fit those solutions.
There is a class called Bucket that instantiates Server:
Actually Bucket doesn't instantiate Server. The class Bucket contains a reference to a Server. You haven't shown how that got set so we don't know the actual class.
However, it is guaranteed that what is assigned to that reference (Bucket.server), assuming it's not null, is a an object of some concrete class that implements Server. That class will provide an implementation of getVersion() and that is what is being called.
Bukkit is just a Modding API. If you want to implement Bukkit, you need to create such an instance yourself and pass it there.
Take for example the unit tests that Bukkit includes:
https://github.com/Bukkit/Bukkit/blob/f210234e59275330f83b994e199c76f6abd41ee7/src/test/java/org/bukkit/TestServer.java#L77
A real implementation that allows you to run a Bukkit server is Spigot.
If I recall correctly, the particular concrete class that's being selected is determined at runtime via reflection. Because Minecraft is not open source, all the developers have are the obfuscated compiled class files to work with.
The code searches through each class file within the minecraft jar, searching for a class that matches certain conditions, and then, using a bytecode library, force that class to implement that interface.
For example, let's say that the following (obfuscated) class was the real Server class within the Minecraft code
class a {
String x_x317() {
return q_q98;
}
static a a_a1;
static String q_q98 = "1.9.4";
}
In this case, the method x_x317 returns the version string. The tool that allows them too hook into this class might do it based on the following conditions:
The class has default access
The class has only one default access static reference to itself
The class has only one default access static String field.
The class has a single method, that has default access, that returns String, and the returned value is the FieldRef found in 3.
This generally returns only one class. In the case that multiple are returned (usually in the dev phase of the new Bukkit version), they get more specific with their conditions to ensure that they only get the right class returned. They do this for every field, class, and method they need to identify.
Since they now know which exact class is the Server class, they can go ahead and make changes to it. First they would need to implement the interface
class a implements org.bukkit.Server
And then implement the method
class a implements org.bukkit.Server {
String x_x317() {
return q_q98;
}
public String getVersionNumber() {
return x_x317();
}
static a a_a1;
static String q_q98 = "1.9.4";
}
Now, we have a class that conforms to the Bukkit API.
When they need to instantiate that class, they just do something along the lines of
Server server = findAndTransformServerClassFromMinecraftJar();
// ...
Server findAndTransformServerClassFromMinecraftJar() {
// load classes from jar
// map them to the appropriate interfaces
// transform and hook the required classes and methods
Class<?> serverClass = doTheFirstThreeSteps();
return (Server) serverClass.newInstance();
}
I have classes that are automatically generated in Java. I want to add a method to that class (in another file) so that I can add additional functionality without changing the generated file. The idea being that if I have to recreate the generated file, I won't lose my new functionality.
In Objective-c I know this is called categories, and in JavaScript you can append the object's prototype, but I am unaware of how to do this in Java or what it is called.
This is not supported in java.
partial classes/partial class file
One thing you can do is inherit the generated class (if it is not final) and add your methods.
You could just use composition, ie:
public class JasonString {
String wrapped;
public JasonString() {
wrapped = new String();
}
public String toString() {
return wrapped.toLowerCase().toString();
}
// other methods of wrapped class you're using should just call the corresponding method in wrapped.
}
If you do not have access to the generated class you could try to extend it (if generated class is not final) and add new methods to sub-class... Java doesn't support dynamic attributes as JavaScript.
I agree with #Itay I used to have auto-generated classes from Ibatis and the best way to go about with your problem is to extend all the generated classes and add the functionality that you want.
I've got a library that allows clients to provide a list of text files, each of which contains groovy code for a class that extends java class Z. For instance file 'A.groovy' contains
package com.mypkg;
public class A extends Z {
#Override
public void someMethod() {
// do something A-ish
}
}
etc.
The library compiles each of these and (in this case) would return to the clients an instance of type Z.
My issue comes when a client needs something like this:
package com.mypkg;
public class B extends A { // extends A!
#Override
public void someMethod() {
// do something B-ish instead of A-ish
}
}
where B extends A, and class A was parsed before class B.
The issue is that the GroovyClassLoader can't seem to find class A, even though it just parsed A. Here's the code that compiles the scripts and creates the instances:
for (String fileName : listOfScriptFiles) {
InputStream in = getInputStreamFromFile(fileName);
CompilerConfiguration compConfig = new CompilerConfiguration();
GroovyClassLoader classLoader = new GroovyClassLoader(Thread.currentThread()
.getContextClassLoader(), compConfig);
Z service = null;
Class clazz = classLoader.parseClass(in);
service = (Z) clazz.newInstance();
return service;
}
Is there a way to 'register' class A with the runtime so that when Groovy tries to compile class B it will not complain that class A doesn't exist?
UPDATE
I was actually able to solve this by instantiating the GroovyClassLoader outside the loop that iterates through the client's code list, so the classloader that parses A is the same that parses B.
The question still stands, though, because I could envision a case where in one part of someone's code they parse A, and then in a completely different part, where the same classloader is unavailable, they parse B.
In my experience with the Groovy classloader (which is similar in behavior with Ant and beanshell's classloader in this respect) , you have to decide up front whether you are going to use the default system classloader, in which case you would build the classpath into the command that launches the Groovy script, OR on the other hand, you specify ONLY the groovy jar on the command line classpath and then you dynamically add classes at the beginning of your Groovy script on the custom classloader.
You aren't providing much information in your question, but my guess is that you put class "A" on the classpath before you launched the script and then your trying to load class "B" dynamically. That wouldn't work as far as I know.
NOTE: I myself have been trying to figure out how to do this kind of thing. It seems it would be possible but I still haven't figured it out.