ArrayProto.java revision 39e4d4487e20041700f036a58a4dd7fb50e954bf
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 32package org.jf.dexlib2.analysis; 33 34import com.google.common.base.Strings; 35import org.jf.dexlib2.util.TypeUtils; 36import org.jf.util.ExceptionWithContext; 37 38import javax.annotation.Nonnull; 39import javax.annotation.Nullable; 40 41public class ArrayProto implements TypeProto { 42 protected final ClassPath classPath; 43 protected final int dimensions; 44 protected final String elementType; 45 46 public ArrayProto(@Nonnull ClassPath classPath, @Nonnull String type) { 47 this.classPath = classPath; 48 int i=0; 49 while (type.charAt(i) == '[') { 50 i++; 51 if (i == type.length()) { 52 throw new ExceptionWithContext("Invalid array type: %s", type); 53 } 54 } 55 56 dimensions = i; 57 elementType = type.substring(i); 58 } 59 60 @Override public String toString() { return getType(); } 61 @Nonnull @Override public ClassPath getClassPath() { return classPath; } 62 @Nonnull @Override public String getType() { return makeArrayType(elementType, dimensions); } 63 @Nonnull public String getElementType() { return elementType; } 64 @Override public boolean isInterface() { return false; } 65 66 @Override public boolean implementsInterface(@Nonnull String iface) { 67 return iface.equals("Ljava/lang/Cloneable;") || iface.equals("Ljava/io/Serializable;"); 68 } 69 70 @Nullable @Override 71 public String getSuperclass() { 72 return "Ljava/lang/Object;"; 73 } 74 75 @Nonnull @Override 76 public TypeProto getCommonSuperclass(@Nonnull TypeProto other) { 77 if (other instanceof ArrayProto) { 78 if (TypeUtils.isPrimitiveType(getElementType()) || 79 TypeUtils.isPrimitiveType(((ArrayProto)other).getElementType())) { 80 if (dimensions == ((ArrayProto)other).dimensions && 81 getElementType().equals(((ArrayProto)other).getElementType())) { 82 return this; 83 } 84 return classPath.getClass("Ljava/lang/Object;"); 85 } 86 87 if (dimensions == ((ArrayProto)other).dimensions) { 88 TypeProto thisClass = classPath.getClass(elementType); 89 TypeProto otherClass = classPath.getClass(((ArrayProto)other).elementType); 90 TypeProto mergedClass = thisClass.getCommonSuperclass(otherClass); 91 if (thisClass == mergedClass) { 92 return this; 93 } 94 if (otherClass == mergedClass) { 95 return other; 96 } 97 return classPath.getClass(makeArrayType(mergedClass.getType(), dimensions)); 98 } 99 100 int dimensions = Math.min(this.dimensions, ((ArrayProto)other).dimensions); 101 return classPath.getClass(makeArrayType("Ljava/lang/Object;", dimensions)); 102 } 103 104 if (other instanceof ClassProto) { 105 try { 106 if (other.isInterface()) { 107 if (implementsInterface(other.getType())) { 108 return other; 109 } 110 } 111 } catch (UnresolvedClassException ex) { 112 // ignore 113 } 114 return classPath.getClass("Ljava/lang/Object;"); 115 } 116 117 // otherwise, defer to the other class' getCommonSuperclass 118 return other.getCommonSuperclass(this); 119 } 120 121 private static final String BRACKETS = Strings.repeat("[", 256); 122 123 @Nonnull 124 private static String makeArrayType(@Nonnull String elementType, int dimensions) { 125 return BRACKETS.substring(0, dimensions) + elementType; 126 } 127} 128