Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2012, Google Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 package org.jf.dexlib2.util;
     33 
     34 import com.google.common.base.Predicate;
     35 import org.jf.dexlib2.AccessFlags;
     36 import org.jf.dexlib2.iface.Method;
     37 import org.jf.dexlib2.iface.reference.MethodReference;
     38 import org.jf.util.CharSequenceUtils;
     39 
     40 import javax.annotation.Nonnull;
     41 import javax.annotation.Nullable;
     42 import java.util.Collection;
     43 
     44 public final class MethodUtil {
     45     private static int directMask = AccessFlags.STATIC.getValue() | AccessFlags.PRIVATE.getValue() |
     46             AccessFlags.CONSTRUCTOR.getValue();
     47 
     48     public static Predicate<Method> METHOD_IS_DIRECT = new Predicate<Method>() {
     49         @Override public boolean apply(@Nullable Method input) {
     50             return input != null && isDirect(input);
     51         }
     52     };
     53 
     54     public static Predicate<Method> METHOD_IS_VIRTUAL = new Predicate<Method>() {
     55         @Override public boolean apply(@Nullable Method input) {
     56             return input != null && !isDirect(input);
     57         }
     58     };
     59 
     60     public static boolean isDirect(@Nonnull Method method) {
     61         return (method.getAccessFlags() & directMask) != 0;
     62     }
     63 
     64     public static boolean isStatic(@Nonnull Method method) {
     65         return AccessFlags.STATIC.isSet(method.getAccessFlags());
     66     }
     67 
     68     public static boolean isConstructor(@Nonnull MethodReference methodReference) {
     69         return methodReference.getName().equals("<init>");
     70     }
     71 
     72     public static boolean isPackagePrivate(@Nonnull Method method) {
     73         return (method.getAccessFlags() & (AccessFlags.PRIVATE.getValue() |
     74                 AccessFlags.PROTECTED.getValue() |
     75                 AccessFlags.PUBLIC.getValue())) == 0;
     76     }
     77 
     78     public static int getParameterRegisterCount(@Nonnull Method method) {
     79         return getParameterRegisterCount(method, MethodUtil.isStatic(method));
     80     }
     81 
     82     public static int getParameterRegisterCount(@Nonnull MethodReference methodRef, boolean isStatic) {
     83         return getParameterRegisterCount(methodRef.getParameterTypes(), isStatic);
     84     }
     85 
     86     public static int getParameterRegisterCount(@Nonnull Collection<? extends CharSequence> parameterTypes,
     87                                                 boolean isStatic) {
     88         int regCount = 0;
     89         for (CharSequence paramType: parameterTypes) {
     90             int firstChar = paramType.charAt(0);
     91             if (firstChar == 'J' || firstChar == 'D') {
     92                 regCount += 2;
     93             } else {
     94                 regCount++;
     95             }
     96         }
     97         if (!isStatic) {
     98             regCount++;
     99         }
    100         return regCount;
    101     }
    102 
    103     private static char getShortyType(CharSequence type) {
    104         if (type.length() > 1) {
    105             return 'L';
    106         }
    107         return type.charAt(0);
    108     }
    109 
    110     public static String getShorty(Collection<? extends CharSequence> params, String returnType) {
    111         StringBuilder sb = new StringBuilder(params.size() + 1);
    112         sb.append(getShortyType(returnType));
    113         for (CharSequence typeRef: params) {
    114             sb.append(getShortyType(typeRef));
    115         }
    116         return sb.toString();
    117     }
    118 
    119     public static boolean methodSignaturesMatch(@Nonnull MethodReference a, @Nonnull MethodReference b) {
    120         return (a.getName().equals(b.getName()) &&
    121                 a.getReturnType().equals(b.getReturnType()) &&
    122                 CharSequenceUtils.listEquals(a.getParameterTypes(), b.getParameterTypes()));
    123     }
    124 
    125     private MethodUtil() {}
    126 }
    127