SmalideaMethod.java revision ff4c85c5e4e380607fe1f89dc72db7339f77db8c
1/* 2 * Copyright 2014, 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.smalidea.dexlib; 33 34import com.google.common.base.Function; 35import com.google.common.collect.ImmutableList; 36import com.google.common.collect.ImmutableSet; 37import com.google.common.collect.Lists; 38import com.intellij.psi.PsiClass; 39import com.intellij.psi.PsiMethod; 40import com.intellij.psi.PsiModifierList; 41import com.intellij.psi.PsiParameter; 42import org.jetbrains.annotations.NotNull; 43import org.jetbrains.annotations.Nullable; 44import org.jf.dexlib2.AccessFlags; 45import org.jf.dexlib2.base.reference.BaseMethodReference; 46import org.jf.dexlib2.iface.*; 47import org.jf.dexlib2.iface.debug.DebugItem; 48import org.jf.dexlib2.iface.instruction.Instruction; 49import org.jf.smalidea.dexlib.instruction.SmalideaInstruction; 50import org.jf.smalidea.psi.impl.SmaliCatchStatement; 51import org.jf.smalidea.psi.impl.SmaliInstruction; 52import org.jf.smalidea.psi.impl.SmaliMethod; 53import org.jf.smalidea.util.NameUtils; 54 55import javax.annotation.Nonnull; 56import java.util.Arrays; 57import java.util.List; 58import java.util.Set; 59 60public class SmalideaMethod extends BaseMethodReference implements Method { 61 private final PsiMethod psiMethod; 62 63 public SmalideaMethod(@NotNull PsiMethod psiMethod) { 64 this.psiMethod = psiMethod; 65 } 66 67 @Nonnull @Override public String getDefiningClass() { 68 PsiClass cls = psiMethod.getContainingClass(); 69 assert cls != null; 70 String qualifiedName = cls.getQualifiedName(); 71 assert qualifiedName != null; 72 return NameUtils.javaToSmaliType(qualifiedName); 73 } 74 75 @Nonnull @Override public List<? extends MethodParameter> getParameters() { 76 PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); 77 78 return Lists.transform(Arrays.asList(parameters), new Function<PsiParameter, MethodParameter>() { 79 @Nullable @Override 80 public MethodParameter apply(@Nullable PsiParameter psiParameter) { 81 if (psiParameter == null) { 82 return null; 83 } 84 return new SmalideaMethodParameter(psiParameter); 85 } 86 }); 87 } 88 89 @Override public int getAccessFlags() { 90 if (psiMethod instanceof SmaliMethod) { 91 return ((SmaliMethod)psiMethod).getModifierList().getAccessFlags(); 92 } else { 93 int flags = 0; 94 PsiModifierList modifierList = psiMethod.getModifierList(); 95 if (modifierList.hasModifierProperty("public")) { 96 flags |= AccessFlags.PUBLIC.getValue(); 97 } else if (modifierList.hasModifierProperty("protected")) { 98 flags |= AccessFlags.PROTECTED.getValue(); 99 } else if (modifierList.hasModifierProperty("private")) { 100 flags |= AccessFlags.PRIVATE.getValue(); 101 } 102 103 if (modifierList.hasModifierProperty("static")) { 104 flags |= AccessFlags.STATIC.getValue(); 105 } 106 107 if (modifierList.hasModifierProperty("final")) { 108 flags |= AccessFlags.FINAL.getValue(); 109 } 110 111 boolean isNative = false; 112 if (modifierList.hasModifierProperty("native")) { 113 flags |= AccessFlags.NATIVE.getValue(); 114 isNative = true; 115 } 116 117 if (modifierList.hasModifierProperty("synchronized")) { 118 if (isNative) { 119 flags |= AccessFlags.SYNCHRONIZED.getValue(); 120 } else { 121 flags |= AccessFlags.DECLARED_SYNCHRONIZED.getValue(); 122 } 123 } 124 125 if (psiMethod.isVarArgs()) { 126 flags |= AccessFlags.VARARGS.getValue(); 127 } 128 129 if (modifierList.hasModifierProperty("abstract")) { 130 flags |= AccessFlags.ABSTRACT.getValue(); 131 } 132 133 if (modifierList.hasModifierProperty("strictfp")) { 134 flags |= AccessFlags.STRICTFP.getValue(); 135 } 136 137 if (psiMethod.isConstructor()) { 138 flags |= AccessFlags.CONSTRUCTOR.getValue(); 139 } 140 return flags; 141 } 142 } 143 144 @Nonnull @Override public Set<? extends Annotation> getAnnotations() { 145 // TODO: implement this 146 return ImmutableSet.of(); 147 } 148 149 @Nullable @Override public MethodImplementation getImplementation() { 150 if (psiMethod instanceof SmaliMethod) { 151 final SmaliMethod smaliMethod = (SmaliMethod)this.psiMethod; 152 153 List<SmaliInstruction> instructions = smaliMethod.getInstructions(); 154 if (instructions.size() == 0) { 155 return null; 156 } 157 158 // TODO: cache this? 159 return new MethodImplementation() { 160 @Override public int getRegisterCount() { 161 return smaliMethod.getRegisterCount(); 162 } 163 164 @Nonnull @Override public Iterable<? extends Instruction> getInstructions() { 165 return Lists.transform(smaliMethod.getInstructions(), 166 new Function<SmaliInstruction, Instruction>() { 167 @Override 168 public Instruction apply(SmaliInstruction smaliInstruction) { 169 return SmalideaInstruction.of(smaliInstruction); 170 } 171 }); 172 } 173 174 @Nonnull @Override public List<? extends TryBlock<? extends ExceptionHandler>> getTryBlocks() { 175 return Lists.transform(smaliMethod.getCatchStatements(), 176 new Function<SmaliCatchStatement, TryBlock<? extends ExceptionHandler>>() { 177 @Override 178 public TryBlock<? extends ExceptionHandler> apply( 179 SmaliCatchStatement smaliCatchStatement) { 180 assert smaliCatchStatement != null; 181 return new SmalideaTryBlock(smaliCatchStatement); 182 } 183 }); 184 } 185 186 @Nonnull @Override public Iterable<? extends DebugItem> getDebugItems() { 187 // TODO: implement this 188 return ImmutableList.of(); 189 } 190 }; 191 } 192 return null; 193 } 194 195 @Nonnull @Override public String getName() { 196 return psiMethod.getName(); 197 } 198 199 @Nonnull @Override public List<? extends CharSequence> getParameterTypes() { 200 PsiParameter[] parameters = psiMethod.getParameterList().getParameters(); 201 202 return Lists.transform(Arrays.asList(parameters), new Function<PsiParameter, CharSequence>() { 203 @Nullable @Override 204 public CharSequence apply(@Nullable PsiParameter psiParameter) { 205 if (psiParameter == null) { 206 return null; 207 } 208 return psiParameter.getText(); 209 } 210 }); 211 } 212 213 @Nonnull @Override public String getReturnType() { 214 return psiMethod.getReturnTypeElement().getText(); 215 } 216} 217