Implement PsiClass.getQualifiedName()

This commit is contained in:
Ben Gruver 2014-03-19 21:57:31 -07:00
parent 7106c64346
commit db1b08807f
5 changed files with 102 additions and 1 deletions

View File

@ -53,11 +53,16 @@ public class SmaliClass extends SmaliStubBasedPsiElement<SmaliClassStub> impleme
}
@Override public boolean hasTypeParameters() {
// TODO: implement generics
return false;
}
@Nullable @Override public String getQualifiedName() {
return null;
SmaliClassStatement classStatement = findChildByClass(SmaliClassStatement.class);
if (classStatement == null) {
return null;
}
return classStatement.getJavaType();
}
@Override public boolean isInterface() {

View File

@ -31,6 +31,7 @@
package org.jf.smalidea.psi.impl;
import org.jetbrains.annotations.Nullable;
import org.jf.smalidea.psi.SmaliCompositeElementFactory;
import org.jf.smalidea.psi.SmaliElementTypes;
@ -44,4 +45,16 @@ public class SmaliClassStatement extends SmaliCompositeElement {
public SmaliClassStatement() {
super(SmaliElementTypes.CLASS_STATEMENT);
}
/**
* @return the fully qualified java-style name of the class in this .class statement
*/
@Nullable
public String getJavaType() {
SmaliClassTypeElement classType = findChildByClass(SmaliClassTypeElement.class);
if (classType == null) {
return null;
}
return classType.getJavaType();
}
}

View File

@ -31,8 +31,10 @@
package org.jf.smalidea.psi.impl;
import org.jetbrains.annotations.NotNull;
import org.jf.smalidea.psi.SmaliCompositeElementFactory;
import org.jf.smalidea.psi.SmaliElementTypes;
import org.jf.smalidea.util.NameUtils;
public class SmaliClassTypeElement extends SmaliCompositeElement {
public static final SmaliCompositeElementFactory FACTORY = new SmaliCompositeElementFactory() {
@ -44,4 +46,12 @@ public class SmaliClassTypeElement extends SmaliCompositeElement {
public SmaliClassTypeElement() {
super(SmaliElementTypes.CLASS_TYPE);
}
/**
* @return the fully qualified java-style name of the class in this .class statement
*/
@NotNull
public String getJavaType() {
return NameUtils.smaliToJavaType(getText());
}
}

View File

@ -31,11 +31,38 @@
package org.jf.smalidea.psi.impl;
import com.intellij.psi.PsiElement;
import com.intellij.psi.impl.source.tree.CompositePsiElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.ReflectionCache;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public abstract class SmaliCompositeElement extends CompositePsiElement {
public SmaliCompositeElement(IElementType type) {
super(type);
}
@NotNull
@SuppressWarnings("unchecked")
protected <T> T[] findChildrenByClass(Class<T> aClass) {
List<T> result = new ArrayList<T>();
for (PsiElement cur = getFirstChild(); cur != null; cur = cur.getNextSibling()) {
if (ReflectionCache.isInstance(cur, aClass)) result.add((T)cur);
}
return result.toArray((T[]) Array.newInstance(aClass, result.size()));
}
@Nullable
@SuppressWarnings("unchecked")
protected <T> T findChildByClass(Class<T> aClass) {
for (PsiElement cur = getFirstChild(); cur != null; cur = cur.getNextSibling()) {
if (ReflectionCache.isInstance(cur, aClass)) return (T)cur;
}
return null;
}
}

View File

@ -0,0 +1,46 @@
/*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.smalidea.util;
import javax.annotation.Nonnull;
public class NameUtils {
@Nonnull
public static String javaToSmaliType(@Nonnull String javaType) {
return 'L' + javaType.replace('.', '/') + ';';
}
@Nonnull
public static String smaliToJavaType(@Nonnull String smaliType) {
return smaliType.replace('/', '.').substring(1, smaliType.length()-1);
}
}