JavaMethodAnalyzer.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 30 Aug 2013 17:28:45 +0100
branchdevelopment
changeset 2698 c243b2455f71
child 2704 0f7f5f5c67c7
permissions -rw-r--r--
Added JavaMethodAnalyzer to gather statistics about Java method. Useful for tools.

"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modifications done at SWING Research Group [1]:

 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 [1] Code written at SWING Research Group contains a signature
     of one of the above copright owners. For exact set of such code,
     see the differences between this version and version stx:libjava
     as of 1.9.2010
"
"{ Package: 'stx:libjava' }"

JavaByteCodeProcessorAdapter subclass:#JavaMethodAnalyzer
	instanceVariableNames:'fieldsAccessed fieldsModified methodsInvoked'
	classVariableNames:''
	poolDictionaries:''
	category:'Languages-Java-Support-Decompiling'
!

!JavaMethodAnalyzer class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996-2011 by Claus Gittinger

 New code and modifications done at SWING Research Group [1]:

 COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko
                            SWING Research Group, Czech Technical University in Prague

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.

 [1] Code written at SWING Research Group contains a signature
     of one of the above copright owners. For exact set of such code,
     see the differences between this version and version stx:libjava
     as of 1.9.2010

"
!

documentation
"
    A helper class to analyze method's bytecode and keep some statistics
    like read/written fields, sent messages, referenced classes...

    [author:]
        Jan Vrany <jan.vrany@fit.cvut.cz>

    [instance variables:]

    [class variables:]

    [see also:]

"
! !

!JavaMethodAnalyzer class methodsFor:'analyzing'!

analyze: aJavaMethod
    "Analyzes the given method and return the analyzer,
     which can be in turn asked for various informstion"

     ^ self new
        process: aJavaMethod 
        receiver: nil
        arguments: (Array new: aJavaMethod javaNumArgs);
        yourself

    "Created: / 30-08-2013 / 13:33:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaMethodAnalyzer methodsFor:'instructions'!

getfield
    | fieldRef |

    fieldRef := constantPool at: self fetchIndex2.     
    fieldsAccessed add: fieldRef.

    "Created: / 30-08-2013 / 13:25:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 30-08-2013 / 17:02:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

invinterface
    methodsInvoked add: (constantPool at: self fetchBytes4)

    "Created: / 30-08-2013 / 17:05:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

invnonvirt
     methodsInvoked add: (constantPool at: self fetchBytes2)

    "Created: / 30-08-2013 / 17:05:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

invstatic
    methodsInvoked add: (constantPool at: self fetchBytes2)

    "Created: / 30-08-2013 / 17:05:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

invvirt
    methodsInvoked add: (constantPool at: self fetchBytes2)

    "Created: / 30-08-2013 / 17:05:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

putfield
    | fieldRef |

    fieldRef := constantPool at: self fetchIndex2.     
    fieldsAccessed add: fieldRef.
    fieldsModified add: fieldRef.

    "Created: / 30-08-2013 / 13:26:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 30-08-2013 / 17:02:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaMethodAnalyzer methodsFor:'processing loop'!

process: aMethod receiver: aReceiver arguments: args 
    fieldsAccessed := Set new.
    fieldsModified := Set new.
    methodsInvoked := Set new.

    ^ super process: aMethod receiver: aReceiver arguments: args.

    "Created: / 30-08-2013 / 13:23:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 30-08-2013 / 17:01:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!JavaMethodAnalyzer methodsFor:'queries-statistic'!

messagesPossiblySent
    ^ methodsInvoked collect:[:methodRef | methodRef selector ]

    "Created: / 30-08-2013 / 14:05:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 30-08-2013 / 17:11:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

messagesSentToSuper
    ^#()

    "Created: / 30-03-2013 / 09:59:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

modifiedClassVars
    ^#() "/ No class vars in Java

    "Created: / 30-08-2013 / 13:16:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

modifiedInstVars
    ^ fieldsModified collect:[:ref | ref name ]

    "Created: / 30-08-2013 / 13:18:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 30-08-2013 / 17:02:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

sends:symbol1 or:symbol2

    ^false
!

sendsAny: selectors

    ^false

    "Created: / 02-12-2011 / 23:05:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

usedClassVars
    ^ #() "/ No class vars in Java

    "Created: / 30-08-2013 / 13:18:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

usedInstVars
    ^ fieldsAccessed collect:[:ref | ref name ]

    "Created: / 30-08-2013 / 13:18:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 30-08-2013 / 17:02:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !