Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2013, 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.analysis.util;
     33 
     34 import org.jf.dexlib2.analysis.TypeProto;
     35 import org.jf.dexlib2.analysis.UnresolvedClassException;
     36 
     37 import javax.annotation.Nonnull;
     38 import javax.annotation.Nullable;
     39 import java.util.Iterator;
     40 import java.util.NoSuchElementException;
     41 
     42 public class TypeProtoUtils {
     43     /**
     44      * Get the chain of superclasses of the given class. The first element will be the immediate superclass followed by
     45      * it's superclass, etc. up to java.lang.Object.
     46      *
     47      * Returns an empty iterable if called on java.lang.Object or a primitive.
     48      *
     49      * If any class in the superclass chain can't be resolved, the iterable will return Ujava/lang/Object; to represent
     50      * the unknown class.
     51      *
     52      * @return An iterable containing the superclasses of this class.
     53      */
     54     @Nonnull
     55     public static Iterable<TypeProto> getSuperclassChain(@Nonnull final TypeProto typeProto) {
     56         return new Iterable<TypeProto>() {
     57 
     58             @Override public Iterator<TypeProto> iterator() {
     59                 return new Iterator<TypeProto>() {
     60                     @Nullable private TypeProto type = getSuperclassAsTypeProto(typeProto);
     61 
     62                     @Override public boolean hasNext() {
     63                         return type != null;
     64                     }
     65 
     66                     @Override public TypeProto next() {
     67                         TypeProto type = this.type;
     68                         if (type == null) {
     69                             throw new NoSuchElementException();
     70                         }
     71 
     72                         this.type = getSuperclassAsTypeProto(type);
     73                         return type;
     74                     }
     75 
     76                     @Override public void remove() {
     77                         throw new UnsupportedOperationException();
     78                     }
     79                 };
     80             }
     81         };
     82     }
     83 
     84     @Nullable
     85     public static TypeProto getSuperclassAsTypeProto(@Nonnull TypeProto type) {
     86         try {
     87             String next = type.getSuperclass();
     88             if (next != null) {
     89                 return type.getClassPath().getClass(next);
     90             } else {
     91                 return null;
     92             }
     93         } catch (UnresolvedClassException ex) {
     94             return type.getClassPath().getUnknownClass();
     95         }
     96     }
     97 
     98     public static boolean extendsFrom(@Nonnull TypeProto candidate, @Nonnull String possibleSuper) {
     99         if (candidate.getType().equals(possibleSuper)) {
    100             return true;
    101         }
    102         for (TypeProto superProto: getSuperclassChain(candidate)) {
    103             if (superProto.getType().equals(possibleSuper)) {
    104                 return true;
    105             }
    106         }
    107         return false;
    108     }
    109 }
    110