[Javassist user questions] - Strategy for classpool = null clearing
by PaulKeeble
In the tutorial one of the noted problems is the memory usage climb with creating a lot of CtClass's etc that are all stored in the ClassPool. I am having trouble combining that advice with a javaagent implementation.
The problem is really the order you see classes in and knowing when its safe to clear the pool. For instance say I have the following two classes:
| class Account {
| ...
| }
|
| Class SavingsAccount extends Account {
| ...
| }
|
The javaagent will receive first the Account class, followed by the SavingsAccount. If you clear the pool after the Account class then SavingsAccount will see an a non enhanced class (which I can't do as I am adding constructors which the subclass will call).
So the question is really what is the usual approach to this? So far I have tried the following:
- Clearing every X times called, fails for obvious reasons
- Clearing at the beginning of the program, fails because not all classes are necessarily loaded at that point
I need something that some how mind reads the future calls! Any ideas?
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4241820#4241820
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4241820
16 years, 10 months
[Javassist user questions] - Re: Adding annotations to methods
by PaulKeeble
In the end I managed to work it out and answer my own question. The key was understanding that you need to add the AnnotationsAttribute instance to the Info of the method.
That is the rough solution is:
| CtClass clazz = ....
| ClassFile classFile = clazz.getClassFile();
| ConstPool cp = classFile.getConstPool();
| AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
|
| Annotation useAnnotation = new Annotation("org.package.AnnotationType",cp);
| annotation.addMemberValue("name", new StringMemberValue("Paul",cp));
|
| attr.setAnnotation(useAnnotation);
|
| m.getMethodInfo().addAttribute(attr);
|
|
So the only real difference between the CtClass, CtMethod and even parameters is I needed to do the appropriate getXXXInfo and then add the attribute directly.
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4241819#4241819
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4241819
16 years, 10 months
[Javassist user questions] - Re: adding annotations
by PaulKeeble
The beauty of a Java agent is it gets passed every loaded class, regardless of classloader. So although you need to filter the list as quickly as possible (you'll get thousands of calls from java.* classes for instance) it is the best way to do the instrumentation.
I have happily got annotation adding working in my code base recently so thought it might help you if I explained roughly how I do it.
First up getting a CtClass:
byte[] byteCode = ....
ClassPool pool = ....
CtClass clazz = pool.makeClass(new ByteArrayInputStream(byteCode));
If your using a Java agent it will give you the bytes directly, if you load them yourself in a custom classloader.
Next up you the annotations adding to a CtClass:
CtClass clazz = ....
ClassFile classFile = clazz.getClassFile();
ConstPool cp = classFile.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
Annotation theAnnotation = new Annotation("org.package.AnnotationType",cp);
annotation.addMemberValue("name", new StringMemberValue("Paul",cp));
attr.setAnnotation(theAnnotation);
clazz.getClassInfo().addAttribute(attr);
That worked well for me as an approach. If you want to see a working version (the far more complicated version based on methods and arrays of values) you can see the latest version of my code at JEMM Git MethodAnnotationTransformation.java
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4241818#4241818
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4241818
16 years, 10 months
[Tomcat, HTTPD, Servlets & JSP] - Asynchronous HTTP Request is not working as excepted for Re
by vijayravi_srn
Hi,
I have written a simple Asynchronous HTTP Request RestEasy Application which contains two methods .
1) asyncBasic() -- asynchronous method
2) basic() --- normal RESTeasy http handler method i.e. synchronous
I deployed this App in Jboss 5.0.0(native library enabled) and
Case 1: when i send a request for both basic and asyncBasic , i am getting response back after 10 Seconds which i set as @suspend interval. but basic() should send response immediately which is not happening here.
can someone help me to resolve this problem ? i followed the instruction
given at
http://www.jboss.org/file-access/default/members/resteasy/freezone/docs/1...
here is my code and web.xml
@Path("/")
public class SimpleResource
{
@GET
@Path("basicasync")
@Produces("text/plain")
public void asyncBasic(final @Suspend(10000) AsynchronousResponse response) throws Exception
{
Thread t = new Thread()
{
@Override
public void run()
{
try
{
Thread.sleep(5000);
Response jaxrs = Response.ok("Async Date : "+(new Date())).type(MediaType.TEXT_PLAIN).build();
response.setResponse(jaxrs);
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
t.start();
}
@GET
@Path("basic")
@Produces("text/plain")
public Response syncBasic() throws Exception
{
Response jaxrs = Response.ok("Sync Date "+(new Date())).type(MediaType.TEXT_PLAIN).build();
response.setResponse(jaxrs);
}
}
**********************************************************
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.BasicApplication</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest-services</param-value>
</context-param>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.JBossWebDispatcherServlet
</servlet-class>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest-services/*</url-pattern>
</servlet-mapping>
</web-app>
View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4241801#4241801
Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4241801
16 years, 10 months