Is it possible to get the Extension class loader object? - jvm

I'm learning about ClassLoading concepts in java.
I know that can get the System classLoader with ClassLoader.getSystemClassLoader() method using java.lang.ClassLoader class. Is it possible to get Extension class loader also?
I'm aware that JVM loads the BootStrapClassLoader and we cannot instantiate it. But what about Extension class loader?
Thanks in Advance.

You can try this one sun.net.spi.nameservice.dns.DNSNameService.getClassLoader(). DNSNameService is one the class that exist as the Extension class loader.
Another option is to write something like this:
ClassLoader cl =new Object(){}.getClass().getEnclosingClass().getClassLoader();
ClassLoader prev = null;
while(cl!=null){
prev=cl;
cl=cl.getParent();
}
System.out.println(prev);
prev will contain reference to extension class loader.
Note: You can write besides
new Object(){}.getClass().getEnclosingClass().getClassLoader() idiom Thread.currentThread().getContextClassLoader() or even simpler YourClassName.class.getClassLoader() While these idioms are not identical any of them will do the job.
See http://www.javacodegeeks.com/2011/03/understanding-extending-java.html for some more details.

Related

Kotlin - Why isn't there a "Progression" interface like in the case of "ClosedRange"?

Kotlin has defined:
class CharRange : CharProgression, ClosedRange<Char>
but looking at CharProgression:
open class CharProgression : Iterable<Char>
there is no Progression interface. It directly implements Iterable.
Why doesn't Kotlin define a Progression interface as it has done with ClosedRange?
Maybe someone from JetBrains will correct me, but I would assume that the reason lies here:
// Auto-generated file. DO NOT EDIT!
import kotlin.internal.getProgressionLastElement
All this class hierarchy, including internal CharProgressionIterator is being auto-generated. Hence, being both internal and generated, it doesn't make much sense to have an interface nothing but the generated code would use.

ByteBuddy: Workaround for modifying schema of loaded class

The issue: We generate classes using ByteBuddy, load them and at one point will need to add/remove fields without re-starting the application.
If I understand correctly, in Java it is not possible to modify the schema (add fields/methods) of a class already loaded into a class loader.
My question: As a workaround, would it be a possible/reasonable to create a new class loader when a such a conflict occurs - load all generated classes into the new class loader and close the old class loader?
Thank you.
There is a VM version of OpenJDK that supports such transformations, the Dynamic Code Evolution VM.
For a regular VM, you can load a class in a new class loader. However, in this case no previous instance will be an instance of this altered class. You would need to recreate all instances. If this is feasible for you, this is an option.

Javassist NotFoundException when getting java.io.Serializable with JDK9

I have the following code:
private static CtClass resolveCtClass(String clazz) throws NotFoundException {
ClassPool pool = ClassPool.getDefault();
return pool.get( clazz );
}
When running under JDK8, if this method is called using java.io.Serializable, it works, but when running under the JDK9 environment, it throws the NotFoundException.
Is there something I overlooked here?
This does no longer happen with the current EA builds of Java 9. Class files are now always locatable even if they are encapsulated in a module.
This is a consequence of Java 9's module encapsulation where non-exported resources are no longer available via the ClassLoader API. Under the covers, Javassist calls
ClassLoader.getSystemClassLoader().findResource("java/io/Serializable.class");
to get hold of the class file for Serializable. It then parses this class file and represents the information similarly to the Java reflection API but without loading the class such that it can be edited prior to loading it.
Until Java 8, this class file was accessible as most class loaders rely on looking up a class file before loading it such that the above call returned a URL pointing to the file. Since Java 9, resources of named modules are only available via the new API method findResource(String, String) where the second arguments names the module of that class.
The short answer is: Javassist does no longer work with Java 9 and none of its dependant projects will. This is a known issue with the current Java 9 implementation and will hopefully be fixed prior to release.
(I never used Javassist so I'm just shooting in the dark, here...)
The documentation of ClassPool says:
If get() is called on this object, it searches various sources represented by ClassPath to find a class file and then it creates a CtClass object representing that class file.
This seems to be bound to the concept of the class path. Looking at ClassPath and CtClass supports that assumption.
If that is the case, then Javassist might just not be fit to look into JDK 9's brand new modules.
If my guess is correct, you should not be able to get any JDK class from the pool. This should be easily verifiable.

Where is Java Main method defined in source?

Pardon me for asking this silly question. Where can i find java main method definition in java source? it is not in object class or system class? so how and where is it defined exactly?
public static void main(String[] args) {}
Where is Java Main method defined in source?
It is declared in a class. Conventionally, it is a top-level (i.e. non-nested) public class, but that is not a requirement. (A non-public class will work, and I think a static nested class will work too.)
How do you find the main method?
Use grep or similar to search the source code of your application.
Use your IDE's method search capability.
Read the application's user documentation or launch script.
Look for the main method in the index of the application's javadoc.
How does the java command find it?
It doesn't! You specify the fully qualified class name of the class containing the main method you want to use on the java command line. Alternatively, you can set the Main-Class attribute in a JAR file's manifest so that the user doesn't need to know the class name.
UPDATE - If you are looking for the code in the OpenJDK source tree that loads the entrypoint class, finds the main method and invokes it, it is all in "jdk8u/jdk/src/share/bin/java.c". Happy reading.
It's not defined anywhere as code (in the standard libraries).
The JVM expects to find it if you're running a class, and if it's not found you get an error. Therefore it's up to you to create a public static void main(String[] args) method if you want to run your class.
main method is the entry point of an application in java. All the java classes are packaged as libraries which will be used in any application. So class files are used as references instead of separte executable. You can't execute the java source code separately because there won't be any main method definition in java source code.

How to instantiate AbstractSourceLookupDirector?

I'm writing a plugin for Eclipse Juno, I want to make use of class AbstractSourceLookupDirector. When I look at the API it says it has a constructor, but when I use following statement in my code, it says "cannot instantiate type AbstractSourceLookupDirector"
AbstractSourceLookupDirector srclookupDir = new AbstractSourceLookupDirector();
Could you please let me know how to make use of AbstractSourceLookupDirector.
Ref: http://help.eclipse.org/indigo/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/debug/core/sourcelookup/AbstractSourceLookupDirector.html
Many thanks in advance!
It is an Abstract class, you can't instantiate abstract classes in Java.
What you need to do is configure an extension point org.eclipse.debug.core.sourceLocators
and have your class extend AbstractSourceLookupDirector
An example is given in the juno docs
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fextension-points%2Forg_eclipse_debug_core_sourceLocators.html