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.psi.impl;
33
34import com.intellij.lang.ASTNode;
35import org.jetbrains.annotations.NonNls;
36import org.jetbrains.annotations.NotNull;
37import org.jetbrains.annotations.Nullable;
38import org.jf.smalidea.psi.SmaliElementTypes;
39import org.jf.smalidea.psi.iface.SmaliModifierListOwner;
40import org.jf.smalidea.psi.leaf.SmaliClassDescriptor;
41import org.jf.smalidea.psi.stub.SmaliClassStatementStub;
42import org.jf.smalidea.util.NameUtils;
43
44public class SmaliClassStatement extends SmaliStubBasedPsiElement<SmaliClassStatementStub>
45        implements SmaliModifierListOwner {
46    public SmaliClassStatement(@NotNull SmaliClassStatementStub stub) {
47        super(stub, SmaliElementTypes.CLASS_STATEMENT);
48    }
49
50    public SmaliClassStatement(@NotNull ASTNode node) {
51        super(node);
52    }
53
54    @Nullable
55    public SmaliClassTypeElement getNameElement() {
56        return findChildByClass(SmaliClassTypeElement.class);
57    }
58
59    @Nullable
60    public SmaliClass getContainingClass() {
61        return getStubOrPsiParentOfType(SmaliClass.class);
62    }
63
64    @Nullable
65    public SmaliClassDescriptor getNameIdentifier() {
66        SmaliClassTypeElement classTypeElement = getNameElement();
67        if (classTypeElement == null) {
68            return null;
69        }
70        return classTypeElement.getReferenceNameElement();
71    }
72
73    /**
74     * @return the fully qualified java-style name of the class in this .class statement
75     */
76    @Nullable
77    public String getQualifiedName() {
78        SmaliClassStatementStub stub = getStub();
79        if (stub != null) {
80            return stub.getQualifiedName();
81        }
82
83        SmaliClassTypeElement classType = findChildByClass(SmaliClassTypeElement.class);
84        if (classType == null) {
85            return null;
86        }
87        // Since this is a class declared in smali, we don't have to worry about handling inner classes,
88        // so we can do a pure textual translation of the class name
89        return NameUtils.smaliToJavaType(classType.getSmaliName());
90    }
91
92    @Nullable
93    public SmaliModifierList getModifierList() {
94        return getStubOrPsiChild(SmaliElementTypes.MODIFIER_LIST);
95    }
96
97    @NotNull
98    @Override
99    public SmaliAnnotation addAnnotation(@NotNull @NonNls String qualifiedName) {
100        SmaliClass containingClass = getContainingClass();
101        if (containingClass == null) {
102            // TODO: what should we do here?
103            return null;
104        }
105        return containingClass.addAnnotation(qualifiedName);
106    }
107
108    @NotNull
109    @Override
110    public SmaliAnnotation[] getAnnotations() {
111        SmaliClass containingClass = getContainingClass();
112        if (containingClass == null) {
113            return new SmaliAnnotation[0];
114        }
115        return containingClass.getAnnotations();
116    }
117
118    @NotNull
119    @Override
120    public SmaliAnnotation[] getApplicableAnnotations() {
121        SmaliClass containingClass = getContainingClass();
122        if (containingClass == null) {
123            return new SmaliAnnotation[0];
124        }
125        return containingClass.getApplicableAnnotations();
126    }
127
128    @Nullable
129    @Override
130    public SmaliAnnotation findAnnotation(@NotNull @NonNls String qualifiedName) {
131        SmaliClass containingClass = getContainingClass();
132        if (containingClass == null) {
133            return null;
134        }
135        return containingClass.findAnnotation(qualifiedName);
136    }
137
138    @Override
139    public boolean hasModifierProperty(@NonNls @NotNull String name) {
140        SmaliClass containingClass = getContainingClass();
141        if (containingClass == null) {
142            return false;
143        }
144        return containingClass.hasModifierProperty(name);
145    }
146}
147