classloader

Java adding to a unknown type generic list

两盒软妹~` 提交于 2019-11-29 12:13:21
问题 I've come into something I haven't come across before in Java and that is, I need to create a new instance of say the ArrayList class at runtime without assigning a known type then add data to the list. It sounds a bit vague so here is an example: Class<?> c = i.getClass(); Constructor<?> con = ArrayList.class.getConstructor(); ArrayList<?> al = (ArrayList<?>)con.newInstance(); al.add("something"); Now the reason I'm doing this versus just using generics is because generics are already being

Struts2 + Classloader Vulnerability + How to reproduce

浪子不回头ぞ 提交于 2019-11-29 11:35:57
How to reproduce the security issue CVE-2014-0094 . I googled but couldn't able to find any reference to the same. Got it working. I have to enable logging (for ognl package) to see the error. Pass in parameter like class.classLoader.resource.dircontext.docBase=someText to the struts2 application. localhost:8080/sampleApp/showlogin.do?class.classLoader.resource.diretext.docBase=someText Then in the log I would see something like this. java.lang.IllegalArgumentException: Document base base does not exist or is not a readable directory at org.apache.naming.resources.FileDirContext.setDocBase

What for Sun JVM creates instances of sun.reflect.DelegatingClassLoader at runtime?

陌路散爱 提交于 2019-11-29 11:27:54
问题 While analyzing a heap dump using jhat I have observed many instances of DelegatingClassLoader created although they were not called explicitly in code. I expect this to be some sort of reflection optimization mechanism. Does anybody know the details? 回答1: Yes, it is probably a reflection optimisation. On the Sun JVM, reflective access to properties and methods is initially performed by calling through JNI into the JVM implementation. If the JVM notices that a method or field is being

getClass().getResource(“/”) returns null in command line

ⅰ亾dé卋堺 提交于 2019-11-29 11:06:22
I'm trying to read a file in my maven project at /src/main/resources/file.txt. I'm using URL url=this.getClass().getResource("/"); String filePath=url.getPath()+"file.txt"; url object gets the correct value when this is run thru eclipse. But, when I package the jar and run it in command line: jar -cp myjar.jar SampleTest It returns null for 'url' object and throws a NullPointerException in the next line. I have openend my Jar file using file browser and checked. It has the 'file.txt' in "/" location inside the Jar. Where am I going wrong ? There are (often) no directories inside jar files.

Java ClassLoader change

℡╲_俬逩灬. 提交于 2019-11-29 10:56:25
I have some class A : public class A { public A(String str) { System.out.println("Create A instance: " + str); } public void methodA() { System.out.println("#methodA1()"); } } And my class loader implementation: public class MyClassLoader extends ClassLoader { public MyClassLoader() { super(); } @Override public synchronized Class<?> loadClass(String name) throws ClassNotFoundException { System.out.println("Load: " + name); return super.loadClass(name); } } And now I try to change default class loader in current thread: import java.util.ArrayList; import java.util.List; public class

Java: How to load a class (and its inner classes) that is already on the class path?

青春壹個敷衍的年華 提交于 2019-11-29 10:47:34
How can I load a class that is already on the class path, instantiate it, and also instantiate any inner classes defined within it? EG: public class TestClass { public class InnerClass { } } Inner classes cannot exist outside the parent class. You need to construct the parent class first. Without reflection this would look like: InnerClass innerClass = new TestClass().new InnerClass(); In reflection, you need to pass the parent class in during construction of the inner class. Object testClass = Class.forName("com.example.TestClass").newInstance(); for (Class<?> cls : testClass.getClass()

Using JPA 2.1 in EAP 6.4.0

。_饼干妹妹 提交于 2019-11-29 10:01:23
问题 I searched for a solution for this problem without having success so far. We're migrating our aplications over to EAP 6.4.0, and our applications relies on JPA 2.1. However, JBoss is a Java EE 6 server, and therefore includes JPA 2.0 in the form of a module. I tried including the JPA API jar directly in my WEB-INF/lib directory, thinking the classloader would prefer this one over the one from the system. I have tried to include a jboss-deployment-structure.xml file in my ear. I tried it under

How is the Classloader for a class chosen?

99封情书 提交于 2019-11-29 09:41:27
问题 Motivation Assume that we have a class loading hierarchy that looks like this: Bootstrap | System | Custom Let's say that Custom Classloader is used for loading a class com.example.SomeClass . It checks if the System classloader can load it which again checks whether Bootstrap classloader can load it. Since both can't, com.example.SomeClass is loaded by Custom classloader. Any class that com.example.SomeClass depends on goes throught the same. I believe I understand that process. Question I

Is it possible to create an URL pointing to an in-memory object?

懵懂的女人 提交于 2019-11-29 09:32:42
I'm trying to extend my library for integrating Swing and JPA by making JPA config as automatic (and portable) as can be done, and it means programmatically adding <class> elements. (I know it can be done via Hibernate's AnnotationConfiguration or EclipseLInk's ServerSession , but - portability). I'd also like to avoid using Spring just for this single purpose. I can create a persistence.xml on the fly, and fill it with <class> elements from specified packages (via the Reflections library). The problem starts when I try to feed this persistence.xml to a JPA provider. The only way I can think

Get bytecode from loaded class

霸气de小男生 提交于 2019-11-29 09:25:30
Suppose in my JVM I have a loaded class Class<C> myClass . Is there a reliable way to ask the JVM for the bytecode contents of the .class? I.e. something like this: <C> byte[] getClassBytecode(Class<C> myClass) { return /* the contents of the .class resource where C was loaded from */; } (obviously an InputStream would be as good as the byte[] ). I know I can use myClass.getResource() (and friends) to fetch the class file, but hammering on the class name to get an URL to feed to getResource feels wrong. Also, I am not sure how this would behave in case C was dynamically generated (e.g. using