Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Soot - a J*va Optimization Framework
   * Copyright (C) 1997-1999 Raja Vallee-Rai
   * Copyright (C) 2004 Ondrej Lhotak
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 
 /*
  * Modified by the Sable Research Group and others 1997-1999.  
  * See the 'credits' file distributed with Soot for the complete list of
  * contributors.  (Soot is distributed at http://www.sable.mcgill.ca/soot)
  */
 
 package soot;
 import java.util.List;
 
Soot representation of a Java method. Can be declared to belong to a SootClass. Does not contain the actual code, which belongs to a Body. The getActiveBody() method points to the currently-active body.
 
 public class SootMethod 
     extends AbstractHost
     implements ClassMemberNumberableMethodOrMethodContext {
     public static final String constructorName = "<init>";
     public static final String staticInitializerName = "<clinit>";
     public static boolean DEBUG=false;
    
Name of the current method.
 
     String name;

    
A list of parameter types taken by this SootMethod object, in declaration order.
 
     List parameterTypes;

    
The return type of this object.
 
     Type returnType;

    
True when some SootClass object declares this SootMethod object.
 
     boolean isDeclared;

    
Holds the class which declares this SootClass method.
 
     SootClass declaringClass;

    
Modifiers associated with this SootMethod (e.g. private, protected, etc.)
 
     int modifiers;

    
Is this method a phantom method?
 
     boolean isPhantom = false;

    
Declared exceptions thrown by this method. Created upon demand.
 
     List<SootClassexceptions = null;

    
Active body associated with this method.
 
     Body activeBody;

    
Tells this method how to find out where its body lives.
 
     protected MethodSource ms;

    
Uses methodSource to retrieve the method body in question; does not set it to be the active body.

Parameters:
phaseName Phase name for body loading.
 
     private Body getBodyFromMethodSource(String phaseName) {
         return .getBody(thisphaseName);
     }

    
Sets the MethodSource of the current SootMethod.
 
     public void setSource(MethodSource ms) {
         this. = ms;
     }

    
Returns the MethodSource of the current SootMethod.
 
     public MethodSource getSource() {
         return ;
     }

    
Returns a hash code for this method consistent with structural equality.
    public int equivHashCode() {
        return .hashCode() * 101 +  * 17 + .hashCode();
    }

    
Constructs a SootMethod with the given name, parameter types and return type.
    public SootMethod(String nameList parameterTypesType returnType) {
        this(nameparameterTypesreturnType, 0, Collections.<SootClass>emptyList());
    }

    
Constructs a SootMethod with the given name, parameter types, return type and modifiers.
    public SootMethod(
        String name,
        List parameterTypes,
        Type returnType,
        int modifiers) {
        this(nameparameterTypesreturnTypemodifiers, Collections.<SootClass>emptyList());
    }

    
Constructs a SootMethod with the given name, parameter types, return type, and list of thrown exceptions.
    public SootMethod(
        String name,
        List parameterTypes,
        Type returnType,
        int modifiers,
        List<SootClassthrownExceptions) {
        this. = name;
        this. = new ArrayList();
        this..addAll(parameterTypes);
        this. = Collections.unmodifiableList(this.);
        this. = returnType;
        this. = modifiers;
        if ( == null && !thrownExceptions.isEmpty()) {
             = new ArrayList<SootClass>();
            this..addAll(thrownExceptions);
            /*DEBUG=true;
            if(DEBUG)
            	System.out.println("Added thrown exceptions"+thrownExceptions);
            DEBUG=false;
            */
        }
        Scene.v().getMethodNumberer().add(this);
         =
            Scene.v().getSubSigNumberer().findOrAdd(getSubSignature());
        
        
    }

    
Returns the name of this method.
    public String getName() {
        return ;
    }

    
Nomair A. Naeem , January 14th 2006 Need it for the decompiler to create a new SootMethod The SootMethod can be created fine but when one tries to create a SootMethodRef there is an error because there is no declaring class set. Dava cannot add the method to the class until after it has ended decompiling the remaining method (new method added is added in the PackManager) It would make sense to setDeclared to true within this method too. However later when the sootMethod is added it checks that the method is not set to declared (isDeclared).
    public void setDeclaringClass(SootClass declClass){
	if(declClass != null){
	    =declClass;
	    //setDeclared(true);
	}
    }
    
    
Returns the class which declares the current SootMethod.
    public SootClass getDeclaringClass() {
        if (!)
            throw new RuntimeException("not declared: " + getName());
        return ;
    }
    public void setDeclared(boolean isDeclared) {
        this. = isDeclared;
    }

    
Returns true when some SootClass object declares this SootMethod object.
    public boolean isDeclared() {
        return ;
    }

    
Returns true when this SootMethod object is phantom.
    public boolean isPhantom() {
        return ;
    }

    
Returns true if this method is not phantom, abstract or native, i.e. this method can have a body.
    public boolean isConcrete() {
        return !isPhantom() && !isAbstract() && !isNative();
    }

    
Sets the phantom flag on this method.
    public void setPhantom(boolean value) {
        if (value) {
            if (!Scene.v().allowsPhantomRefs())
                throw new RuntimeException("Phantom refs not allowed");
            if ( != null && !.isPhantom())
                throw new RuntimeException("Declaring class would have to be phantom");
        }
         = value;
    }

    
Sets the name of this method.
    public void setName(String name) {
        boolean wasDeclared = ;
        SootClass oldDeclaringClass = ;
        ifwasDeclared ) oldDeclaringClass.removeMethod(this);
        this. = name;
         =
            Scene.v().getSubSigNumberer().findOrAdd(getSubSignature());
        ifwasDeclaredoldDeclaringClass.addMethod(this);
    }

    
Gets the modifiers of this method.

See also:
Modifier
    public int getModifiers() {
        return ;
    }

    
Sets the modifiers of this method.

See also:
Modifier
    public void setModifiers(int modifiers) {
        // RoboVM note: We allow all classes to be modified
//        if ((declaringClass != null) && (!declaringClass.isApplicationClass()))
//            throw new RuntimeException("Cannot set modifiers of a method from a non-app class!");
        this. = modifiers;
    }

    
Returns the return type of this method.
    public Type getReturnType() {
        return ;
    }

    
Sets the return type of this method.
    public void setReturnType(Type t) {
        boolean wasDeclared = ;
        SootClass oldDeclaringClass = ;
        ifwasDeclared ) oldDeclaringClass.removeMethod(this);
         = t;
         =
            Scene.v().getSubSigNumberer().findOrAdd(getSubSignature());
        ifwasDeclaredoldDeclaringClass.addMethod(this);
    }

    
Returns the number of parameters taken by this method.
    public int getParameterCount() {
        return .size();
    }

    
Gets the type of the nth parameter of this method.
    public Type getParameterType(int n) {
        return (Type.get(n);
    }

    
Returns a read-only list of the parameter types of this method.
    public List getParameterTypes() {
        return ;
    }

    
Changes the set of parameter types of this method.
    public void setParameterTypesList l ) {
        boolean wasDeclared = ;
        SootClass oldDeclaringClass = ;
        ifwasDeclared ) oldDeclaringClass.removeMethod(this);
        List al = new ArrayList();
        al.addAll(l);
        this. = Collections.unmodifiableList(al);
         =
            Scene.v().getSubSigNumberer().findOrAdd(getSubSignature());
        ifwasDeclaredoldDeclaringClass.addMethod(this);
    }

    
Retrieves the active body for this method.
    public Body getActiveBody() {
        if (!=null && .isPhantomClass())
            throw new RuntimeException(
                "cannot get active body for phantom class: " + getSignature());
		// ignore empty body exceptions if we are just computing coffi metrics
        if (!hasActiveBody())
            throw new RuntimeException(
                "no active body present for method " + getSignature());
        return ;
    }

    
Returns the active body if present, else constructs an active body and returns that. If you called Scene.v().loadClassAndSupport() for a class yourself, it will not be an application class, so you cannot get retrieve its active body. Please call setApplicationClass() on the relevant class.
    public Body retrieveActiveBody() {
        if (.isPhantomClass())
            throw new RuntimeException(
                "cannot get resident body for phantom class : "
                    + getSignature()
                    + "; maybe you want to call c.setApplicationClass() on this class!");
        if (!hasActiveBody()) {
            //	    G.v().out.println("Retrieving "+this.getSignature());
            setActiveBody(this.getBodyFromMethodSource("jb"));
             = null;
        }
        return getActiveBody();
    }

    
Sets the active body for this method.
    public void setActiveBody(Body body) {
        if (( != null)
            && .isPhantomClass())
            throw new RuntimeException(
                "cannot set active body for phantom class! " + this);
        if (!isConcrete())
            throw new RuntimeException(
                "cannot set body for non-concrete method! " + this);
        if (body!= null && body.getMethod() != this)
            body.setMethod(this);
         = body;
    }

    
Returns true if this method has an active body.
    public boolean hasActiveBody() {
        return  != null;
    }

    
Releases the active body associated with this method.
    public void releaseActiveBody() {
         = null;
    }

    
Adds the given exception to the list of exceptions thrown by this method unless the exception is already in the list.
    public void addExceptionIfAbsent(SootClass e) {
        if( !throwsException(e) ) addException(e);
    }
    
Adds the given exception to the list of exceptions thrown by this method.
    public void addException(SootClass e) {
    	if()
    		..println("Adding exception "+e);
    	
        if ( == null)
             = new ArrayList<SootClass>();
        else if (.contains(e))
            throw new RuntimeException(
                "already throws exception " + e.getName());
        .add(e);
    }

    
Removes the given exception from the list of exceptions thrown by this method.
    public void removeException(SootClass e) {
    	if()
    		..println("Removing exception "+e);
        if ( == null)
             = new ArrayList<SootClass>();
        if (!.contains(e))
            throw new RuntimeException(
                "does not throw exception " + e.getName());
        .remove(e);
    }

    
Returns true if this method throws exception e.
    public boolean throwsException(SootClass e) {
        return  != null && .contains(e);
    }
    public void setExceptions(List exceptions) {
        this. = new ArrayList<SootClass>();
        this..addAll(exceptions);
    }

    
Returns a backed list of the exceptions thrown by this method.
    public List<SootClassgetExceptions() {
        if ( == null)
             = new ArrayList<SootClass>();
        return ;
    }

    
Convenience method returning true if this method is static.
    public boolean isStatic() {
        return Modifier.isStatic(this.getModifiers());
    }

    
Convenience method returning true if this method is private.
    public boolean isPrivate() {
        return Modifier.isPrivate(this.getModifiers());
    }

    
Convenience method returning true if this method is public.
    public boolean isPublic() {
        return Modifier.isPublic(this.getModifiers());
    }

    
Convenience method returning true if this method is protected.
    public boolean isProtected() {
        return Modifier.isProtected(this.getModifiers());
    }

    
Convenience method returning true if this method is abstract.
    public boolean isAbstract() {
        return Modifier.isAbstract(this.getModifiers());
    }

    
Convenience method returning true if this method is native.
    public boolean isNative() {
        return Modifier.isNative(this.getModifiers());
    }

    
Convenience method returning true if this method is synchronized.
    public boolean isSynchronized() {
        return Modifier.isSynchronized(this.getModifiers());
    }
    
    

Returns:
yes if this is the main method
	public boolean isMain()
	{
		if ( isPublic() && isStatic() ) {
			NumberedString main_sig = Scene.v().getSubSigNumberer().findOrAdd"void main(java.lang.String[])" );
			if ( main_sig.equals ) )
				return true;
		}
		return false;
	}

Returns:
yes, if this function is a constructor.
	public boolean isConstructor()
	{
	}

Returns:
yes, if this is a class initializer or main function.
	public boolean isEntryMethod()
	{
		if ( isStatic() &&
				.equals( VirtualCalls.v(). ) )
			return true;
		return isMain();
	}

We rely on the JDK class recognition to decide if a method is JDK method.
	public boolean isJavaLibraryMethod()
	{
		return cl.isJavaLibraryClass();
	}
    


    
Returns the Soot signature of this method. Used to refer to methods unambiguously.
    public String getSignature() {
    }
    public static String getSignature(SootClass clString nameList paramsType returnType) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(
            "<" + Scene.v().quotedNameOf(cl.getName()) + ": ");
        buffer.append(getSubSignatureImpl(nameparamsreturnType));
        buffer.append(">");
        // Again, memory-usage tweak depending on JDK implementation due
        // to Michael Pan.
        return buffer.toString().intern();
    }

    
Returns the Soot subsignature of this method. Used to refer to methods unambiguously.
    public String getSubSignature() {
        String name = getName();
        List params = getParameterTypes();
        Type returnType = getReturnType();
        return getSubSignatureImpl(nameparamsreturnType);
    }
    public static String getSubSignature(
        String name,
        List params,
        Type returnType) {
        return getSubSignatureImpl(nameparamsreturnType);
    }
    private static String getSubSignatureImpl(
        String name,
        List params,
        Type returnType) {
        StringBuffer buffer = new StringBuffer();
        Type t = returnType;
        // RoboVM note: Optimization
        //buffer.append(t.toString() + " " + Scene.v().quotedNameOf(name) + "(");
        buffer.append(t.toString());
        buffer.append(' ');
        buffer.append(Scene.v().quotedNameOf(name) + "(");
        Iterator typeIt = params.iterator();
        if (typeIt.hasNext()) {
            t = (TypetypeIt.next();
            buffer.append(t);
            while (typeIt.hasNext()) {
                buffer.append(",");
                t = (TypetypeIt.next();
                buffer.append(t);
            }
        }
        buffer.append(")");
        // RoboVM note: Optimization
        //return buffer.toString().intern();
        return buffer.toString();
    }
    private NumberedString subsignature;
        return ;
    }

    
Returns the signature of this method.
    public String toString() {
        return getSignature();
    }


    
Returns the declaration of this method, as used at the top of textual body representations (before the {}'s containing the code for representation.)
    public String getDeclaration() {
        StringBuffer buffer = new StringBuffer();
        // modifiers
        StringTokenizer st =
            new StringTokenizer(Modifier.toString(this.getModifiers()));
        if (st.hasMoreTokens())
            buffer.append(st.nextToken());
        // RoboVM note: optimization
//        while (st.hasMoreTokens())
//            buffer.append(" " + st.nextToken());
//
//        if (buffer.length() != 0)
//            buffer.append(" ");
        while (st.hasMoreTokens())
            buffer.append(' ').append(st.nextToken());
        if (buffer.length() != 0)
            buffer.append(' ');
        // return type + name
        // RoboVM note: optimization
//        buffer.append(this.getReturnType() + " ");
//        buffer.append(Scene.v().quotedNameOf(this.getName()));
//
//        buffer.append("(");
        buffer.append(this.getReturnType()).append(' ');
        buffer.append(Scene.v().quotedNameOf(this.getName()));
        buffer.append('(');
        // parameters
        Iterator typeIt = this.getParameterTypes().iterator();
        //int count = 0;
        while (typeIt.hasNext()) {
            Type t = (TypetypeIt.next();
            buffer.append(t);
            if (typeIt.hasNext())
                buffer.append(", ");
        }
        buffer.append(")");
        // Print exceptions
        if ( != null) {
            Iterator<SootClassexceptionIt = this.getExceptions().iterator();
            // RoboVM note: optimization
//            if (exceptionIt.hasNext()) {
//                buffer.append(
//                    " throws " + exceptionIt.next().getName());
//
//                while (exceptionIt.hasNext()) {
//                    buffer.append(
//                        ", " + exceptionIt.next().getName());
//                }
//            }
            if (exceptionIt.hasNext()) {
                buffer.append(
                    " throws ").append(exceptionIt.next().getName());
                while (exceptionIt.hasNext()) {
                    buffer.append(
                        ", ").append(exceptionIt.next().getName());
                }
            }
        }
        // RoboVM note: Optimization
        //return buffer.toString().intern();
        return buffer.toString().intern();
    }
    public final int getNumber() {
        return ;
    }
    public final void setNumber(int number) {
        this. = number;
    }
    private int number = 0;
    public SootMethod method() { return this; }
    public Context context() { return null; }
    public SootMethodRef makeRef() {
        return Scene.v().makeMethodRefisStatic() );
    }
New to GrepCode? Check out our FAQ X