Home | History | Annotate | Download | only in impl
      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.openapi.util.TextRange;
     35 import com.intellij.psi.PsiClass;
     36 import com.intellij.psi.PsiElement;
     37 import com.intellij.psi.PsiField;
     38 import com.intellij.psi.PsiReference;
     39 import com.intellij.util.ArrayUtil;
     40 import com.intellij.util.IncorrectOperationException;
     41 import org.jetbrains.annotations.NotNull;
     42 import org.jetbrains.annotations.Nullable;
     43 import org.jf.smalidea.psi.SmaliCompositeElementFactory;
     44 import org.jf.smalidea.psi.SmaliElementTypes;
     45 
     46 public class SmaliFieldReference extends SmaliCompositeElement implements PsiReference {
     47     public static final SmaliCompositeElementFactory FACTORY = new SmaliCompositeElementFactory() {
     48         @Override public SmaliCompositeElement createElement() {
     49             return new SmaliFieldReference();
     50         }
     51     };
     52 
     53     public SmaliFieldReference() {
     54         super(SmaliElementTypes.FIELD_REFERENCE);
     55     }
     56 
     57     @Nullable
     58     public PsiClass getContainingClass() {
     59         SmaliClassTypeElement containingClassReference = getContainingType();
     60         if (containingClassReference == null) {
     61             return null;
     62         }
     63         PsiClass containingClass = containingClassReference.resolve();
     64         if (containingClass == null) {
     65             return null;
     66         }
     67 
     68         return containingClass;
     69     }
     70 
     71     @Nullable
     72     public SmaliClassTypeElement getContainingType() {
     73         return findChildByClass(SmaliClassTypeElement.class);
     74     }
     75 
     76     @Nullable
     77     public SmaliMemberName getMemberName() {
     78         return findChildByClass(SmaliMemberName.class);
     79     }
     80 
     81     @Nullable
     82     public SmaliTypeElement getFieldType() {
     83         SmaliTypeElement[] types = findChildrenByClass(SmaliTypeElement.class);
     84         assert types.length == 2;
     85         return types[1];
     86     }
     87 
     88     @Override public PsiReference getReference() {
     89         return this;
     90     }
     91 
     92     @Override public String getName() {
     93         SmaliMemberName memberName = getMemberName();
     94         if (memberName == null) {
     95             return null;
     96         }
     97         return memberName.getText();
     98     }
     99 
    100     @Override public PsiElement getElement() {
    101         return this;
    102     }
    103 
    104     @Override public TextRange getRangeInElement() {
    105         return new TextRange(0, getTextLength());
    106     }
    107 
    108     @NotNull @Override public String getCanonicalText() {
    109         return getText();
    110     }
    111 
    112     @Nullable @Override public PsiField resolve() {
    113         PsiClass containingClass = getContainingClass();
    114         if (containingClass == null) {
    115             return null;
    116         }
    117 
    118         SmaliMemberName memberName = getMemberName();
    119         if (memberName == null) {
    120             return null;
    121         }
    122 
    123         return containingClass.findFieldByName(memberName.getText(), true);
    124     }
    125 
    126     @Override public boolean isReferenceTo(PsiElement element) {
    127         return resolve() == element;
    128     }
    129 
    130     @NotNull @Override public Object[] getVariants() {
    131         return ArrayUtil.EMPTY_OBJECT_ARRAY;
    132     }
    133 
    134     @Override public boolean isSoft() {
    135         return false;
    136     }
    137 
    138     @Override public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
    139         //TODO: implement this
    140         throw new IncorrectOperationException();
    141     }
    142 
    143     @Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
    144         SmaliMemberName memberName = getMemberName();
    145         if (memberName == null) {
    146             throw new IncorrectOperationException();
    147         }
    148         memberName.setName(newElementName);
    149         return this;
    150     }
    151 }
    152