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 32 package org.jf.smalidea.psi.impl; 33 34 import com.intellij.lang.ASTNode; 35 import com.intellij.psi.PsiReferenceList; 36 import com.intellij.psi.StubBasedPsiElement; 37 import com.intellij.psi.stubs.IStubElementType; 38 import org.jetbrains.annotations.NotNull; 39 import org.jf.smalidea.psi.stub.SmaliBaseReferenceListStub; 40 import org.jf.smalidea.util.NameUtils; 41 42 public abstract class SmaliBaseReferenceList<StubT extends SmaliBaseReferenceListStub> 43 extends SmaliStubBasedPsiElement<StubT> implements StubBasedPsiElement<StubT>, PsiReferenceList { 44 protected SmaliBaseReferenceList(@NotNull StubT stub, @NotNull IStubElementType nodeType) { 45 super(stub, nodeType); 46 } 47 48 protected SmaliBaseReferenceList(@NotNull ASTNode node) { 49 super(node); 50 } 51 52 @NotNull @Override public SmaliClassType[] getReferencedTypes() { 53 StubT stub = getStub(); 54 if (stub != null) { 55 return stub.getReferencedTypes(); 56 } 57 58 SmaliClassTypeElement[] references = getReferenceElements(); 59 60 SmaliClassType[] referenceTypes = new SmaliClassType[references.length]; 61 62 for (int i=0; i<references.length; i++) { 63 referenceTypes[i] = references[i].getType(); 64 } 65 return referenceTypes; 66 } 67 68 @NotNull public String[] getReferenceNames() { 69 SmaliBaseReferenceListStub stub = getStub(); 70 71 if (stub != null) { 72 String[] smaliNames = stub.getSmaliTypeNames(); 73 String[] referenceNames = new String[smaliNames.length]; 74 75 for (int i=0; i<smaliNames.length; i++) { 76 referenceNames[i] = NameUtils.resolveSmaliToJavaType(this, smaliNames[i]); 77 } 78 79 return referenceNames; 80 } 81 82 SmaliClassTypeElement[] references = getReferenceElements(); 83 84 String[] referenceNames = new String[references.length]; 85 86 for (int i=0; i<references.length; i++) { 87 referenceNames[i] = references[i].getCanonicalText(); 88 } 89 return referenceNames; 90 } 91 92 @NotNull public String[] getSmaliNames() { 93 SmaliBaseReferenceListStub stub = getStub(); 94 95 if (stub != null) { 96 return stub.getSmaliTypeNames(); 97 } 98 99 SmaliClassTypeElement[] references = getReferenceElements(); 100 101 String[] smaliNames = new String[references.length]; 102 103 for (int i=0; i<references.length; i++) { 104 smaliNames[i] = references[i].getSmaliName(); 105 } 106 return smaliNames; 107 } 108 109 @Override public boolean isWritable() { 110 return false; 111 } 112 113 @NotNull @Override public abstract SmaliClassTypeElement[] getReferenceElements(); 114 } 115