classloader

Is the defining classloader of a class level annotation always a parent of the initiating classloader of that class?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 03:55:10
Suppose the following: @SomeAnnotation public interface Foo { } I would like to know if it is always the case that either the defining classloader of SomeAnnotation is equal to or a parent of the initiating classloader of Foo . I have read JVMS v8 section 5.3 . but I'm not sure what applies here. Section 5.3.4 talks about loading constraints, but they seem not to apply for annotations. The question I'm asking is because code like this: Class<?> fooClass = //will in some way obtain a reference to class Foo fooClass.getAnnotation(SomeAnnotation.class); will fail in the presence of different

How can I implement an abstract singleton class in Java?

百般思念 提交于 2019-11-29 02:59:25
Here is my sample abstract singleton class: public abstract class A { protected static A instance; public static A getInstance() { return instance; } //...rest of my abstract methods... } And here is the concrete implementation: public class B extends A { private B() { } static { instance = new B(); } //...implementations of my abstract methods... } Unfortunately I can't get the static code in class B to execute, so the instance variable never gets set. I have tried this: Class c = B.class; A.getInstance() - returns null; and this ClassLoader.getSystemClassLoader().loadClass("B"); A

Load a Byte Array into a Memory Class Loader

依然范特西╮ 提交于 2019-11-29 02:47:14
I am wondering how I can load a byte array into a memory URLClassLoader? The byte array is the decrypted bytes of a jar file (as seen below)! Most of the memory class loaders are using ClassLoader and not URLClassLoader! I need it to be using URLClassLoader. byte[] fileB = Util.crypt.getFileBytes(inputFile); byte[] dec; dec = Util.crypt.decrypt(fileB, "16LENGTHLONGKEYX".getBytes()); //Load bytes into memory and load a class here? Thanks! I will post here a implementation I had done in the past: // main String className = "tests.compiler.DynamicCompilationHelloWorldEtc"; //... ClassLoader

Is it possible to use instanceof when passing objects between Threads?

故事扮演 提交于 2019-11-29 02:32:44
问题 I've run into an issue where instanceof works, and then it doesn't. Going into details is difficult, but I think this might be the problem: Reading this: http://www.theserverside.com/news/thread.tss?thread_id=40229 (search for Thread.currentThread), it seems to imply that, even if the two objects are the same class, if you pass them between threads with different class loaders, instanceof (and isAssignableFrom) might still fail. This certainly would explain the behavior I'm having, but I was

How to use two versions of jar in my java project

旧城冷巷雨未停 提交于 2019-11-29 02:20:30
In my java project, I need to use neo4j-1.9.3 that depends on lucene-3.6.2 , and ElasticSearch which depends on lucene-4.4.0 . I know that if I want to use two versions of lucene directly, I can use ClassLoader to load different classes from the lucenes. But the problem is that I won't use lucene's apis directly now. Is there any way that lucene-3.6.2 can be loaded when neo4j's apis are running, and lucene-4.4.0 can be loaded while running elasticsearch's apis. The two versions of lucene conflict now, and I need to run neo4j and elasticsearch in one project. How could I solve the dependency

Change classloader

蹲街弑〆低调 提交于 2019-11-29 01:58:25
I'm trying to switch the class loader at runtime: public class Test { public static void main(String[] args) throws Exception { final InjectingClassLoader classLoader = new InjectingClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); Thread thread = new Thread("test") { public void run() { System.out.println("running..."); // approach 1 ClassLoader cl = TestProxy.class.getClassLoader(); try { Class c = classLoader.loadClass("classloader.TestProxy"); Object o = c.newInstance(); c.getMethod("test", new Class[] {}).invoke(o); } catch (Exception e) { e.printStackTrace(); } //

Rails doesn't load classes on deserializing YAML/Marshal objects

旧街凉风 提交于 2019-11-29 01:51:08
Rails: 3.0.3 Ruby: 1.9.2 Trying to deserialize a very simple object using YAML.load or Marshal.load produces a corrupted object because the class which belongs to is not required on the deserializing process. Example: # app/models/my_model.rb class MyModel attr_accessor :id end # test/unit/serializing_test.rb require 'test_helper' class SerializingTest < Test::Unit::TestCase def test_yaml_serialize_structure my_model = MyModel.new my_model.id = 'my model' File.open( "#{Rails.root}/tmp/object.yml" , 'w' ) do |f| YAML::dump(my_model, f) end end def test_yaml_deserialize_structure object = YAML

How to get classloader for a bundle in equinox?

≡放荡痞女 提交于 2019-11-29 01:33:49
I have read a lot of equinox code for this, but still can't figure out a non-hacky way of getting the classloader for a osgi bundle in eclipse equinox setup. Is there one? The short answer (certainly for OSGi 4.1, not sure of 4.2) is you can't get a bundle's classloader. However the Bundle interface exposes a loadClass() method and this would allow you to write a classloader that wraps the bundle API and delegates to that loadClass() method. Or you can save some time and use Spring DM's BundleDelegatingClassLoader class instead. Balazs Zsoldos In OSGi 4.3 you can use: bundle.adapt(BundleWiring

How to provide an interface to JavaCompiler when compiling a source file dynamically?

安稳与你 提交于 2019-11-28 23:56:52
I'm trying to compile and load a class at runtime, without knowing the package of the class. I do know that the class should comply with an interface, and the location of the source, (and hence the class name). I'm trying the following: /* Compiling source */ File root = new File("scripts"); File sourceFile = new File(root, "Test.java"); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); compiler.run(null, null, null, sourceFile.getPath()); where the Test.java file looks something like import foo.Itest; public class Test implements Itest{ ... } And I get a cannot find symbol symbol

Does the Java ClassLoader load inner classes?

女生的网名这么多〃 提交于 2019-11-28 21:15:46
If I have a inner class declaration such as: Class A { public static class B { } } followed by: Class<?> implClass = getClass().getClassLoader().loadClass("A"); Will the A$B inner class be loaded as well? What if the B inner class was not declared as "static" ? kuporific Once the code is compiled, there's no such thing as an inner class . If you look at the results of javac , you'll see two files: A.class A$B.class So class B is not loaded when A is loaded, B just happens to be defined in A . Edit For example, given these two files, package kuporific; public class A { private static class B {}