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.pom.java.LanguageLevel;
     35 import com.intellij.psi.*;
     36 import com.intellij.psi.search.GlobalSearchScope;
     37 import org.jetbrains.annotations.NonNls;
     38 import org.jetbrains.annotations.NotNull;
     39 import org.jetbrains.annotations.Nullable;
     40 import org.jf.smalidea.util.NameUtils;
     41 
     42 public class SmaliClassType extends PsiClassType {
     43     private final PsiTypeElement element;
     44 
     45     public SmaliClassType(PsiTypeElement element) {
     46         this(element, LanguageLevel.JDK_1_5);
     47     }
     48 
     49     public SmaliClassType(PsiTypeElement element, LanguageLevel languageLevel) {
     50         super(languageLevel);
     51         this.element = element;
     52     }
     53 
     54     @Nullable
     55     @Override
     56     public PsiClass resolve() {
     57         PsiReference reference = element.getReference();
     58         if (reference == null) {
     59             return null;
     60         }
     61         PsiElement resolved = reference.resolve();
     62         if (resolved instanceof PsiClass) {
     63             return (PsiClass)resolved;
     64         }
     65         return null;
     66     }
     67 
     68     @Override
     69     public String getClassName() {
     70         PsiClass resolved = resolve();
     71         if (resolved != null) {
     72             return NameUtils.shortNameFromQualifiedName(resolved.getQualifiedName());
     73         }
     74         return NameUtils.shortNameFromQualifiedName(element.getText());
     75     }
     76 
     77     @NotNull
     78     @Override
     79     public PsiType[] getParameters() {
     80         // TODO: (generics) implement this
     81         return PsiType.EMPTY_ARRAY;
     82     }
     83 
     84     @NotNull
     85     @Override
     86     public ClassResolveResult resolveGenerics() {
     87         // TODO: (generics) implement this
     88         return new ClassResolveResult() {
     89             @Override
     90             public PsiClass getElement() {
     91                 return resolve();
     92             }
     93 
     94             @Override
     95             public PsiSubstitutor getSubstitutor() {
     96                 return PsiSubstitutor.EMPTY;
     97             }
     98 
     99             @Override
    100             public boolean isPackagePrefixPackageReference() {
    101                 return false;
    102             }
    103 
    104             @Override
    105             public boolean isAccessible() {
    106                 return true;
    107             }
    108 
    109             @Override
    110             public boolean isStaticsScopeCorrect() {
    111                 return true;
    112             }
    113 
    114             @Override
    115             public PsiElement getCurrentFileResolveScope() {
    116                 return null;
    117             }
    118 
    119             @Override
    120             public boolean isValidResult() {
    121                 return true;
    122             }
    123         };
    124     }
    125 
    126     @NotNull
    127     @Override
    128     public SmaliClassType rawType() {
    129         // TODO: (generics) implement this
    130         return this;
    131     }
    132 
    133     @Override
    134     @NotNull
    135     public String getPresentableText() {
    136         return getCanonicalText();
    137     }
    138 
    139     @Override
    140     @NotNull
    141     public String getCanonicalText() {
    142         PsiClass psiClass = resolve();
    143         if (psiClass != null) {
    144             String qualifiedName = psiClass.getQualifiedName();
    145             if (qualifiedName != null) {
    146                 return qualifiedName;
    147             }
    148         }
    149         return NameUtils.smaliToJavaType(element.getText());
    150     }
    151 
    152     @Override
    153     @NotNull
    154     public String getInternalCanonicalText() {
    155         return getCanonicalText();
    156     }
    157 
    158     @Override
    159     public boolean isValid() {
    160         return element.isValid();
    161     }
    162 
    163     @Override
    164     public boolean equalsToText(@NonNls String text) {
    165         return text.equals(getCanonicalText());
    166     }
    167 
    168     @NotNull
    169     @Override
    170     public GlobalSearchScope getResolveScope() {
    171         return element.getResolveScope();
    172     }
    173 
    174     @NotNull
    175     @Override
    176     public LanguageLevel getLanguageLevel() {
    177         return myLanguageLevel;
    178     }
    179 
    180     @NotNull
    181     @Override
    182     public PsiClassType setLanguageLevel(@NotNull LanguageLevel languageLevel) {
    183         return new SmaliClassType(element, languageLevel);
    184     }
    185 }
    186