You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-dev@jakarta.apache.org by Shrinivas Joshi <sj...@cc.gatech.edu> on 2005/06/11 04:35:49 UTC

Naming Arrays

Hi,
  Is there any way by which some sort of special attribute can be
assigned to instructions. like for e.g. is it possible to assign a name to
array created using InstructionFactory.createNewArray(). The problem that
I am facing is because of not being able to distinguish between
instructions of original class file from that of instructions being added
to that class file using BCEL.

I want to perform some action on array instructions. Before doing that I
I am also adding some array instructions to the class file in some
other step.

So when I perform modification of array instructions, the array
instructions added in the first step also get modified. Previously I
distinguished between such instructions by adding NOPs before modified
instruction. However, I was wondering whethere this can be done more
elegantly.

Hope I have made my point clear.

Thanks and Regards
Shrinivas Joshi.



---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: createInvoke method

Posted by David Foster <da...@earthlink.net>.
I personally would recommend using the Reflection API  
(java.lang.Class, java.lang.reflect.*) if you truly must determine  
the name of the invoked method at runtime (i.e., at the time the  
function is called).

- David

On Jul 2, 2005, at 2:24 AM, Shrinivas Joshi wrote:

> Hi All,
>   InstructionFactory.createInvoke method takes name of the called  
> class as
> it first parameter. Can this be used to create an invoke  
> instruction that
> decides the name of the class at run-time. This is especially  
> useful while
> creating invoke for some method call that is defined in super- 
> classes of
> the calling class and appropriate class's method needs to be called  
> based
> on the run-time type of the objectref.
> Kindly let me know about this.
>
> Thanks and Regards
> Shrinivas Joshi
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-dev-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: createInvoke method

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi All,
  I guess I am missing something over here. Probably it will work if I
just create invoke instruction with called class as the class C and JVM
will decide the class/method to be called based on the run-time type of
the objectref. Will try with it and post abt it.

Thanks and Regards
Shrinivas Joshi.



On Sat, 2 Jul 2005, Shrinivas Joshi wrote:

> Hi Torsten,
>  I want to know whether one can provide something like "this" reference as
> the called class to createInvoke method. Since the called class might vary
> depending on the run-time type of objectref used by invoke* instruction.
> for e.g if A is superclass of B is superclass of C and one wants to add a
> invoke in class C to some method defined in A as well as B class then the
> called class can either be A or B depending on the run-time type of this
> reference loaded using aload_0 prior to invoke* instruction.
> Hope this clarifies.
>
> Thanks and Regards
> Shrinivas Joshi.
>
>
>
> On Sat, 2 Jul 2005, Torsten Curdt wrote:
>
> > Shrinivas Joshi wrote:
> > > Hi All,
> > >   InstructionFactory.createInvoke method takes name of the called class as
> > > it first parameter. Can this be used to create an invoke instruction that
> > > decides the name of the class at run-time. This is especially useful while
> > > creating invoke for some method call that is defined in super-classes of
> > > the calling class and appropriate class's method needs to be called based
> > > on the run-time type of the objectref.
> > > Kindly let me know about this.
> >
> > Shrinivas,
> >
> > sorry but ...but what is the question? :)
> >
> > cheers
> > --
> > Torsten
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: createInvoke method

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi Torsten,
 I want to know whether one can provide something like "this" reference as
the called class to createInvoke method. Since the called class might vary
depending on the run-time type of objectref used by invoke* instruction.
for e.g if A is superclass of B is superclass of C and one wants to add a
invoke in class C to some method defined in A as well as B class then the
called class can either be A or B depending on the run-time type of this
reference loaded using aload_0 prior to invoke* instruction.
Hope this clarifies.

Thanks and Regards
Shrinivas Joshi.



On Sat, 2 Jul 2005, Torsten Curdt wrote:

> Shrinivas Joshi wrote:
> > Hi All,
> >   InstructionFactory.createInvoke method takes name of the called class as
> > it first parameter. Can this be used to create an invoke instruction that
> > decides the name of the class at run-time. This is especially useful while
> > creating invoke for some method call that is defined in super-classes of
> > the calling class and appropriate class's method needs to be called based
> > on the run-time type of the objectref.
> > Kindly let me know about this.
>
> Shrinivas,
>
> sorry but ...but what is the question? :)
>
> cheers
> --
> Torsten
>

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: createInvoke method

Posted by Torsten Curdt <tc...@apache.org>.
Shrinivas Joshi wrote:
> Hi All,
>   InstructionFactory.createInvoke method takes name of the called class as
> it first parameter. Can this be used to create an invoke instruction that
> decides the name of the class at run-time. This is especially useful while
> creating invoke for some method call that is defined in super-classes of
> the calling class and appropriate class's method needs to be called based
> on the run-time type of the objectref.
> Kindly let me know about this.

Shrinivas,

sorry but ...but what is the question? :)

cheers
--
Torsten

createInvoke method

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi All,
  InstructionFactory.createInvoke method takes name of the called class as
it first parameter. Can this be used to create an invoke instruction that
decides the name of the class at run-time. This is especially useful while
creating invoke for some method call that is defined in super-classes of
the calling class and appropriate class's method needs to be called based
on the run-time type of the objectref.
Kindly let me know about this.

Thanks and Regards
Shrinivas Joshi




---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: Determining Caller class of a method

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi Dave,

 Thanks very much for the information. I will try this out.

Regards
Shrinivas Joshi



On Tue, 21 Jun 2005, David Hovemeyer wrote:

> Shrinivas Joshi wrote:
>
> >Hi All,
> >  Is there any way by which one can determine the name/type of the class
> >that invokes a particular method of some other class. I need to do this
> >when the application control is within the method being called. I looked
> >at BCEL ControlFlowGraph, Frame and OperandStack classes but it seems that
> >they dont fetch any information related to caller class.
> >Kindly let me know if someone has worked on similar requirement.
> >
> >
>
> I am assuming you need to do this at runtime, since finding out the
> callers statically is generally impossible.
>
> One possible approach is to insert a call to a static instrumentation method
> into the method you are trying to capture the callers for.   The
> instrumentation
> method can create a stack trace as follows:
>
>     StackTraceElement[] trace = new Throwable().getStackTrace();
>
> Each stack trace element has the class name, method name, and line number
> of the call site.  You'll want to skip the top element, since it refers to
> the instrumentation method.
>
> Note that you will have to use the line number to figure out
> which version of the method is called if there are overloaded variants.
> (If the class doesn't have line number information, then you're out of
> luck.)
> However, since all you need is the class name, then this approach should
> do what you need.
>
> This works for 1.4 and later VMs.  On earlier VMs you can accomplish
> more or less the same thing using printStackTrace() and supplying a
> PrintWriter backed by a StringWriter or similar object, and manually parsing
> the printed stack trace.
>
> Hope this helps.
>
> -Dave
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: Determining Caller class of a method

Posted by Stephen Kolaroff <xc...@netscape.net>.
A good way could be overriding ScurityManager and using its protected 
method getClassContext(), which " Returns the current execution stack as 
an array of classes" (See javadocs for details).
This also addresses the issue for a malicious code packed in class 
HarmlessClassName, but loaded from a different class loader.
Unfortunately, this will not give you the name of the method invoking you.

Best regards
Cheffo

daveho@cs.umd.edu wrote:

> Shrinivas Joshi wrote:
>
>> Hi All,
>>  Is there any way by which one can determine the name/type of the class
>> that invokes a particular method of some other class. I need to do this
>> when the application control is within the method being called. I looked
>> at BCEL ControlFlowGraph, Frame and OperandStack classes but it seems 
>> that
>> they dont fetch any information related to caller class.
>> Kindly let me know if someone has worked on similar requirement.
>>  
>>
>
> I am assuming you need to do this at runtime, since finding out the
> callers statically is generally impossible.
>
> One possible approach is to insert a call to a static instrumentation 
> method
> into the method you are trying to capture the callers for.   The 
> instrumentation
> method can create a stack trace as follows:
>
>    StackTraceElement[] trace = new Throwable().getStackTrace();
>
> Each stack trace element has the class name, method name, and line number
> of the call site.  You'll want to skip the top element, since it 
> refers to
> the instrumentation method.
>
> Note that you will have to use the line number to figure out
> which version of the method is called if there are overloaded variants.
> (If the class doesn't have line number information, then you're out of 
> luck.)
> However, since all you need is the class name, then this approach should
> do what you need.
>
> This works for 1.4 and later VMs.  On earlier VMs you can accomplish
> more or less the same thing using printStackTrace() and supplying a
> PrintWriter backed by a StringWriter or similar object, and manually 
> parsing
> the printed stack trace.
>
> Hope this helps.
>
> -Dave
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-dev-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: Determining Caller class of a method

Posted by David Hovemeyer <da...@cs.umd.edu>.
Shrinivas Joshi wrote:

>Hi All,
>  Is there any way by which one can determine the name/type of the class
>that invokes a particular method of some other class. I need to do this
>when the application control is within the method being called. I looked
>at BCEL ControlFlowGraph, Frame and OperandStack classes but it seems that
>they dont fetch any information related to caller class.
>Kindly let me know if someone has worked on similar requirement.
>  
>

I am assuming you need to do this at runtime, since finding out the
callers statically is generally impossible.

One possible approach is to insert a call to a static instrumentation method
into the method you are trying to capture the callers for.   The 
instrumentation
method can create a stack trace as follows:

    StackTraceElement[] trace = new Throwable().getStackTrace();

Each stack trace element has the class name, method name, and line number
of the call site.  You'll want to skip the top element, since it refers to
the instrumentation method.

Note that you will have to use the line number to figure out
which version of the method is called if there are overloaded variants.
(If the class doesn't have line number information, then you're out of 
luck.)
However, since all you need is the class name, then this approach should
do what you need.

This works for 1.4 and later VMs.  On earlier VMs you can accomplish
more or less the same thing using printStackTrace() and supplying a
PrintWriter backed by a StringWriter or similar object, and manually parsing
the printed stack trace.

Hope this helps.

-Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Determining Caller class of a method

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi All,
  Is there any way by which one can determine the name/type of the class
that invokes a particular method of some other class. I need to do this
when the application control is within the method being called. I looked
at BCEL ControlFlowGraph, Frame and OperandStack classes but it seems that
they dont fetch any information related to caller class.
Kindly let me know if someone has worked on similar requirement.

Thanks and Regards
Shrinivas Joshi.




---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


WRT - Incompatible field Types

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi All,
  Sorry for the previous two mails. I was not popping off the objectref of
getfield instruction and that was causing problem.

Thanks
Shrinivas Joshi




---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: Incompatible field Types

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi All,
  With reference to below mail, I just wanted to let you know that
the same code works without any problem if I have an assignment of the
form
   String testString = b.g;
It fails only when I try to assign field of class A with value of field of
class B. Regular variable assignment just works fine.

Thanks and Regards
Shrinivas Joshi.


On Sat, 18 Jun 2005, Shrinivas Joshi wrote:

> Hi All,
>   Below r the e.g classes that I m having problem with.
>
> public  class A {
>    public String f;
>
>    public A(B b)
>    {
> 	f = b.g;
>    }
>    public static void main(String arg[])
>    {
> 	A a = new A(new B);
>    }
>
> }
>
> public class B {
>    public String g;
>    public B()
>    {
> 	g = "TEST";
>    }
> }
>
> The statement f = b.g compiles into
>
> getfield g of B
> putfield f of A
>
> I am changing these 2 instructions into
>
> INVOKE* method //which returns object of Object type
> checkcast java/lang/String
> putfield f of A
>
> But this gives VerifyError saying : Incompatible type for
> getting or setting field
>
> Please let me know whether I m missing something over here.
>
> Thanks and Regards
> Shrinivas Joshi
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Incompatible field Types

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi All,
  Below r the e.g classes that I m having problem with.

public  class A {
   public String f;

   public A(B b)
   {
	f = b.g;
   }
   public static void main(String arg[])
   {
	A a = new A(new B);
   }

}

public class B {
   public String g;
   public B()
   {
	g = "TEST";
   }
}

The statement f = b.g compiles into

getfield g of B
putfield f of A

I am changing these 2 instructions into

INVOKE* method //which returns object of Object type
checkcast java/lang/String
putfield f of A

But this gives VerifyError saying : Incompatible type for
getting or setting field

Please let me know whether I m missing something over here.

Thanks and Regards
Shrinivas Joshi



---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: Interested in development

Posted by Torsten Curdt <tc...@apache.org>.
>   During the discussion on BCEL bug resolution ( and how ASM is better
> than BCEL ) it was pointed out that there are very few developers working
> on these issues. I have been using BCEL for almost a year now and have
> been referring JVM specs since then. I feel that I would be able to work
> on BCEL bugs (atleast minor). I cannot gurantee particular number of
> commitment hours since I am working on other things as well. But I can
> make it sure that I will work on BCEL during my free time. Kindly let me
> know if I can be of any help.

Would be good ..any help appreciated!

cheers
--
Torsten

Interested in development

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi All,

  During the discussion on BCEL bug resolution ( and how ASM is better
than BCEL ) it was pointed out that there are very few developers working
on these issues. I have been using BCEL for almost a year now and have
been referring JVM specs since then. I feel that I would be able to work
on BCEL bugs (atleast minor). I cannot gurantee particular number of
commitment hours since I am working on other things as well. But I can
make it sure that I will work on BCEL during my free time. Kindly let me
know if I can be of any help.

Thanks and Regards
Shrinivas Joshi.




---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: Naming Arrays

Posted by Shrinivas Joshi <sj...@cc.gatech.edu>.
Hi Ray,
Thanks a lot for the information.
  Actually restructuring the code so that modifications do not overlap is
the best option. However, doing so would incur loss of modularity in my
particular case. Actually in the project, we are instrumenting calls to
methods which involves adding a call to some other method. Now all such
newly added methods start with specific string pattern. Thus I can
safely ignore these INVOKE* instructions in subsequent instrumentation
steps by looking at the name of the method being called by the INVOKE*
instruction. So I was just wondering whether similar option can also be
used to identify instrumented class members like arrays without having a
corresponding local variable for the same.

Regards
Shrinivas Joshi.

On Sat, 11 Jun 2005, Ray Blaak wrote:

> Shrinivas Joshi wrote:
>
> >Hi,
> >  Is there any way by which some sort of special attribute can be
> >assigned to instructions. like for e.g. is it possible to assign a name to
> >array created using InstructionFactory.createNewArray().
> >
> If at generation time, then you simply store it in a local var in your
> generation code and hang on to it.
>
> If you are loading and need to distinguish it then, you could prefix it
> with a specially named string constant, e.g.:
>
>   final String mySpecialArrayIsNext = "my special array is next";
>   ...
> But why do you need to do this? Code just runs. Do you need to mark the
> class file as being marked up in some way? You could simply insert a
> constant attribute to indicate that, perhaps storing the code location
> in it if you need to know where.
>
> I would try instead to restructure your problem so that you don't need
> to do this at all.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: bcel-dev-help@jakarta.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org


Re: Naming Arrays

Posted by Ray Blaak <bl...@ensemble-systems.com>.
Shrinivas Joshi wrote:

>Hi,
>  Is there any way by which some sort of special attribute can be
>assigned to instructions. like for e.g. is it possible to assign a name to
>array created using InstructionFactory.createNewArray(). 
>
If at generation time, then you simply store it in a local var in your 
generation code and hang on to it.

If you are loading and need to distinguish it then, you could prefix it 
with a specially named string constant, e.g.:

  final String mySpecialArrayIsNext = "my special array is next";
  ...
But why do you need to do this? Code just runs. Do you need to mark the 
class file as being marked up in some way? You could simply insert a 
constant attribute to indicate that, perhaps storing the code location 
in it if you need to know where.

I would try instead to restructure your problem so that you don't need 
to do this at all.


---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-dev-help@jakarta.apache.org