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.extapi.psi.PsiFileBase;
     35 import com.intellij.psi.FileViewProvider;
     36 import com.intellij.psi.PsiClassOwner;
     37 import com.intellij.psi.PsiElement;
     38 import com.intellij.psi.stubs.StubElement;
     39 import com.intellij.util.IncorrectOperationException;
     40 import org.jetbrains.annotations.NotNull;
     41 import org.jetbrains.annotations.Nullable;
     42 import org.jf.smalidea.SmaliFileType;
     43 import org.jf.smalidea.SmaliLanguage;
     44 import org.jf.smalidea.psi.SmaliElementTypes;
     45 
     46 public class SmaliFile extends PsiFileBase implements PsiClassOwner {
     47     public SmaliFile(FileViewProvider viewProvider) {
     48         super(viewProvider, SmaliLanguage.INSTANCE);
     49     }
     50 
     51     @NotNull @Override public SmaliFileType getFileType() {
     52         return SmaliFileType.INSTANCE;
     53     }
     54 
     55     @Nullable
     56     public SmaliClass getPsiClass() {
     57         StubElement<? extends PsiElement> stub = (StubElement<? extends PsiElement>)getStub();
     58         if (stub != null) {
     59             StubElement<SmaliClass> classElement = stub.findChildStubByType(SmaliElementTypes.CLASS);
     60             if (classElement != null) {
     61                 return classElement.getPsi();
     62             } else {
     63                 return null;
     64             }
     65         } else {
     66             return findChildByClass(SmaliClass.class);
     67         }
     68     }
     69 
     70     @NotNull @Override public SmaliClass[] getClasses() {
     71         SmaliClass smaliClass = getPsiClass();
     72         if (smaliClass == null) {
     73             return new SmaliClass[] {};
     74         } else {
     75             return new SmaliClass[] { smaliClass };
     76         }
     77     }
     78 
     79     @NotNull @Override public String getPackageName() {
     80         SmaliClass smaliClass = getPsiClass();
     81         if (smaliClass == null) {
     82             return "";
     83         }
     84         return smaliClass.getPackageName();
     85     }
     86 
     87     @Override public void setPackageName(String packageName) throws IncorrectOperationException {
     88         SmaliClass smaliClass = getPsiClass();
     89         if (smaliClass == null) {
     90             return;
     91         }
     92         smaliClass.setPackageName(packageName);
     93     }
     94 }
     95