From 65eecc51b264f73d6470a529a139317de921f5ec Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Sun, 16 Mar 2014 15:05:49 -0700 Subject: [PATCH] Implement initial SmaliAnnotation --- smalidea/src/main/antlr3/smalideaParser.g | 18 ++++- .../jf/smalidea/psi/SmaliElementTypes.java | 6 +- .../jf/smalidea/psi/impl/SmaliAnnotation.java | 47 +++++++++++ .../psi/stub/SmaliAnnotationStub.java | 43 ++++++++++ .../element/SmaliAnnotationElementType.java | 80 +++++++++++++++++++ 5 files changed, 188 insertions(+), 6 deletions(-) create mode 100644 smalidea/src/main/java/org/jf/smalidea/psi/impl/SmaliAnnotation.java create mode 100644 smalidea/src/main/java/org/jf/smalidea/psi/stub/SmaliAnnotationStub.java create mode 100644 smalidea/src/main/java/org/jf/smalidea/psi/stub/element/SmaliAnnotationElementType.java diff --git a/smalidea/src/main/antlr3/smalideaParser.g b/smalidea/src/main/antlr3/smalideaParser.g index 462e293b..0c28b35e 100644 --- a/smalidea/src/main/antlr3/smalideaParser.g +++ b/smalidea/src/main/antlr3/smalideaParser.g @@ -192,19 +192,31 @@ add them to the $smali_file::classAnnotations list*/ field @init { Marker marker = mark(); + Marker annotationsMarker = null; boolean classAnnotations = true; } : FIELD_DIRECTIVE access_list member_name COLON nonvoid_type_descriptor (EQUAL literal)? ( END_FIELD_DIRECTIVE - | (ANNOTATION_DIRECTIVE)=> ( ((ANNOTATION_DIRECTIVE)=> annotation)+ + | (ANNOTATION_DIRECTIVE)=> ( {annotationsMarker = mark();} + ((ANNOTATION_DIRECTIVE)=> annotation)+ (END_FIELD_DIRECTIVE {classAnnotations = false;})? ) | /*epsilon*/ ); finally { - marker.done(SmaliElementTypes.FIELD); + if (annotationsMarker != null) { + if (classAnnotations) { + marker.doneBefore(SmaliElementTypes.FIELD, annotationsMarker); + annotationsMarker.drop(); + } else { + annotationsMarker.drop(); + marker.done(SmaliElementTypes.FIELD); + } + } else { + marker.done(SmaliElementTypes.FIELD); + } } method @@ -371,8 +383,10 @@ annotation_element : simple_name EQUAL literal; annotation + @init { Marker marker = mark(); } : ANNOTATION_DIRECTIVE ANNOTATION_VISIBILITY CLASS_DESCRIPTOR annotation_element* END_ANNOTATION_DIRECTIVE; + finally { marker.done(SmaliElementTypes.ANNOTATION); } subannotation : SUBANNOTATION_DIRECTIVE CLASS_DESCRIPTOR annotation_element* END_SUBANNOTATION_DIRECTIVE; diff --git a/smalidea/src/main/java/org/jf/smalidea/psi/SmaliElementTypes.java b/smalidea/src/main/java/org/jf/smalidea/psi/SmaliElementTypes.java index 22acaef6..1f479012 100644 --- a/smalidea/src/main/java/org/jf/smalidea/psi/SmaliElementTypes.java +++ b/smalidea/src/main/java/org/jf/smalidea/psi/SmaliElementTypes.java @@ -31,14 +31,12 @@ package org.jf.smalidea.psi; -import org.jf.smalidea.psi.stub.element.SmaliClassElementType; -import org.jf.smalidea.psi.stub.element.SmaliFieldElementType; -import org.jf.smalidea.psi.stub.element.SmaliFileElementType; -import org.jf.smalidea.psi.stub.element.SmaliMethodElementType; +import org.jf.smalidea.psi.stub.element.*; public class SmaliElementTypes { public static final SmaliFileElementType FILE = SmaliFileElementType.INSTANCE; public static final SmaliClassElementType CLASS = SmaliClassElementType.INSTANCE; public static final SmaliFieldElementType FIELD = SmaliFieldElementType.INSTANCE; public static final SmaliMethodElementType METHOD = SmaliMethodElementType.INSTANCE; + public static final SmaliAnnotationElementType ANNOTATION = SmaliAnnotationElementType.INSTANCE; } diff --git a/smalidea/src/main/java/org/jf/smalidea/psi/impl/SmaliAnnotation.java b/smalidea/src/main/java/org/jf/smalidea/psi/impl/SmaliAnnotation.java new file mode 100644 index 00000000..cc4d8fa9 --- /dev/null +++ b/smalidea/src/main/java/org/jf/smalidea/psi/impl/SmaliAnnotation.java @@ -0,0 +1,47 @@ +/* + * 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.psi.impl; + +import com.intellij.lang.ASTNode; +import org.jetbrains.annotations.NotNull; +import org.jf.smalidea.psi.SmaliElementTypes; +import org.jf.smalidea.psi.stub.SmaliAnnotationStub; + +public class SmaliAnnotation extends SmaliStubBasedPsiElement { + public SmaliAnnotation(@NotNull SmaliAnnotationStub stub) { + super(stub, SmaliElementTypes.ANNOTATION); + } + + public SmaliAnnotation(@NotNull ASTNode node) { + super(node); + } +} diff --git a/smalidea/src/main/java/org/jf/smalidea/psi/stub/SmaliAnnotationStub.java b/smalidea/src/main/java/org/jf/smalidea/psi/stub/SmaliAnnotationStub.java new file mode 100644 index 00000000..684abf76 --- /dev/null +++ b/smalidea/src/main/java/org/jf/smalidea/psi/stub/SmaliAnnotationStub.java @@ -0,0 +1,43 @@ +/* + * 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.psi.stub; + +import com.intellij.psi.stubs.StubBase; +import com.intellij.psi.stubs.StubElement; +import org.jf.smalidea.psi.SmaliElementTypes; +import org.jf.smalidea.psi.impl.SmaliAnnotation; + +public class SmaliAnnotationStub extends StubBase { + public SmaliAnnotationStub(StubElement parent) { + super(parent, SmaliElementTypes.ANNOTATION); + } +} diff --git a/smalidea/src/main/java/org/jf/smalidea/psi/stub/element/SmaliAnnotationElementType.java b/smalidea/src/main/java/org/jf/smalidea/psi/stub/element/SmaliAnnotationElementType.java new file mode 100644 index 00000000..1ce306b7 --- /dev/null +++ b/smalidea/src/main/java/org/jf/smalidea/psi/stub/element/SmaliAnnotationElementType.java @@ -0,0 +1,80 @@ +/* + * 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.psi.stub.element; + +import com.intellij.lang.ASTNode; +import com.intellij.psi.stubs.IndexSink; +import com.intellij.psi.stubs.StubElement; +import com.intellij.psi.stubs.StubInputStream; +import com.intellij.psi.stubs.StubOutputStream; +import org.jetbrains.annotations.NotNull; +import org.jf.smalidea.psi.impl.SmaliAnnotation; +import org.jf.smalidea.psi.stub.SmaliAnnotationStub; +import org.jf.smalidea.psi.stub.SmaliStubElementType; + +import java.io.IOException; + +public class SmaliAnnotationElementType extends SmaliStubElementType { + public static final SmaliAnnotationElementType INSTANCE = new SmaliAnnotationElementType(); + + private SmaliAnnotationElementType() { + super("ANNOTATION"); + } + + @NotNull @Override public String getExternalId() { + return "smali.annotation"; + } + + @Override public SmaliAnnotation createPsi(@NotNull SmaliAnnotationStub stub) { + return new SmaliAnnotation(stub); + } + + @Override public SmaliAnnotation createPsi(@NotNull ASTNode node) { + return new SmaliAnnotation(node); + } + + @Override public SmaliAnnotationStub createStub(@NotNull SmaliAnnotation psi, StubElement parentStub) { + return new SmaliAnnotationStub(parentStub); + } + + @Override + public void serialize(@NotNull SmaliAnnotationStub stub, @NotNull StubOutputStream dataStream) throws IOException { + } + + @NotNull @Override + public SmaliAnnotationStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException { + return new SmaliAnnotationStub(parentStub); + } + + @Override public void indexStub(@NotNull SmaliAnnotationStub stub, @NotNull IndexSink sink) { + } +}