From 34e2f61dccfa0902eff201c31e291d062c8796d5 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Wed, 11 Sep 2024 13:06:54 +0100 Subject: [PATCH 1/8] WIP --- .../semanticdb_javac/SemanticdbBuilders.java | 7 ++++ .../src/main/protobuf/semanticdb.proto | 7 ++++ .../semanticdb_javac/SemanticdbTrees.java | 33 ++++++++++--------- .../semanticdb_javac/SemanticdbVisitor.java | 29 ++++++++++++++++ .../java/minimized/AnnotationParameters.java | 3 ++ 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java index da11befd..d22aec4b 100644 --- a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java +++ b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java @@ -163,6 +163,13 @@ public static Semanticdb.AssignTree assignTree(Semanticdb.Tree lhs, Semanticdb.T return Semanticdb.AssignTree.newBuilder().setLhs(lhs).setRhs(rhs).build(); } + public static Semanticdb.CastTree castTree( + Semanticdb.Type type, Semanticdb.Tree value) { + return Semanticdb.CastTree.newBuilder().setTpe(type).setValue(value).build(); + } + // SemanticDB Constants + + public static Semanticdb.AnnotationTree annotationTree( Semanticdb.Type type, Iterable parameters) { return Semanticdb.AnnotationTree.newBuilder().setTpe(type).addAllParameters(parameters).build(); diff --git a/semanticdb-java/src/main/protobuf/semanticdb.proto b/semanticdb-java/src/main/protobuf/semanticdb.proto index 5f5ec5b4..2e72263c 100644 --- a/semanticdb-java/src/main/protobuf/semanticdb.proto +++ b/semanticdb-java/src/main/protobuf/semanticdb.proto @@ -298,6 +298,7 @@ message Tree { AssignTree assign_tree = 10; BinaryOperatorTree binop_tree = 11; UnaryOperatorTree unaryop_tree = 12; + CastTree cast_tree = 13; // -- OUT OF SPEC -- // } } @@ -347,6 +348,12 @@ message AnnotationTree { repeated Tree parameters = 2; } + +message CastTree { + Type tpe = 1; + Tree value = 2; +} + message AssignTree { Tree lhs = 1; Tree rhs = 2; diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java index a95a74fb..5511719d 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java @@ -1,26 +1,13 @@ package com.sourcegraph.semanticdb_javac; -import com.sun.source.tree.Tree; +import com.sun.source.tree.*; import com.sun.source.util.Trees; import javax.lang.model.element.Element; import javax.lang.model.util.Types; +import javax.lang.model.type.TypeMirror; import com.sun.source.util.TreePath; import com.sun.source.tree.Tree; import com.sun.source.tree.Tree.Kind; -import com.sun.source.tree.BinaryTree; -import com.sun.source.tree.UnaryTree; -import com.sun.source.tree.AssignmentTree; -import com.sun.source.tree.MemberSelectTree; -import com.sun.source.tree.ClassTree; -import com.sun.source.tree.VariableTree; -import com.sun.source.tree.MethodTree; -import com.sun.source.tree.ModifiersTree; -import com.sun.source.tree.IdentifierTree; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.LiteralTree; -import com.sun.source.tree.NewArrayTree; -import com.sun.source.tree.AnnotationTree; -import com.sun.source.tree.ParenthesizedTree; import java.util.HashMap; import java.util.ArrayList; @@ -44,6 +31,7 @@ public SemanticdbTrees( this.types = types; this.trees = trees; this.nodes = nodes; + this.typeVisitor = new SemanticdbTypeVisitor(globals, locals, types); } private final GlobalSymbolsCache globals; @@ -52,6 +40,7 @@ public SemanticdbTrees( private final Types types; private final Trees trees; private final HashMap nodes; + private final SemanticdbTypeVisitor typeVisitor; public List annotations(Tree node) { if (!(node instanceof ClassTree) @@ -101,10 +90,18 @@ public Semanticdb.AnnotationTree annotationBuilder(AnnotationTree annotation) { Element annotationSym = trees.getElement(annotationTreePath); Semanticdb.Type type = - new SemanticdbTypeVisitor(globals, locals, types).semanticdbType(annotationSym.asType()); + typeVisitor.semanticdbType(annotationSym.asType()); return annotationTree(type, params); } + private TypeMirror getTreeType(Tree tree) { + TreePath path = nodes.get(tree); + System.out.println("Path: " + path); + Element sym = trees.getElement(path); + System.out.println("SYM: " + sym); + return sym.asType(); + } + private Semanticdb.Tree annotationParameter(ExpressionTree expr) { if (expr instanceof MemberSelectTree) { TreePath expressionTreePath = nodes.get(expr); @@ -164,6 +161,10 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) { } else if (expr instanceof ParenthesizedTree) { ParenthesizedTree parenExpr = (ParenthesizedTree) expr; return annotationParameter(parenExpr.getExpression()); + } else if (expr instanceof TypeCastTree) { + TypeCastTree tree = (TypeCastTree) expr; + System.out.println(typeVisitor.semanticdbType(getTreeType(tree.getType()))); +// tree.getType() } throw new IllegalArgumentException( semanticdbUri diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java index cebd32f9..1c461aed 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java @@ -15,6 +15,7 @@ import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.NewClassTree; import com.sun.source.tree.MemberSelectTree; +import com.sun.source.tree.TypeCastTree; import com.sun.source.tree.TypeParameterTree; import com.sun.source.tree.ParameterizedTypeTree; @@ -223,6 +224,9 @@ void resolveNodes() { resolveMemberSelectTree((MemberSelectTree) node, entry.getValue()); } else if (node instanceof NewClassTree) { resolveNewClassTree((NewClassTree) node, entry.getValue()); + } else if (node instanceof TypeCastTree) { + resolveCastTree((TypeCastTree) node, entry.getValue()); + System.out.println("Skipping resolution of [[\n" + node.getClass() + node.toString() + "\n]]"); } } } @@ -337,6 +341,31 @@ private void resolveMemberSelectTree(MemberSelectTree node, TreePath treePath) { } } + + private void resolveCastTree(TypeCastTree node, TreePath treePath) { + // ignore anonymous classes - otherwise there will be a local reference to itself + System.out.println("helo"); + // if (node.getIdentifier() != null && node.getClassBody() == null) { + // Element sym = trees.getElement(treePath); + // if (sym != null) { + // TreePath parentPath = treePath.getParentPath(); + // Element parentSym = trees.getElement(parentPath); + + // if (parentSym == null || parentSym.getKind() != ElementKind.ENUM_CONSTANT) { + // TreePath identifierTreePath = nodes.get(node.getIdentifier()); + // Element identifierSym = trees.getElement(identifierTreePath); + // emitSymbolOccurrence( + // sym, + // node, + // identifierSym.getSimpleName(), + // Role.REFERENCE, + // CompilerRange.FROM_TEXT_SEARCH); + // } + // } + // } + } + + private void resolveNewClassTree(NewClassTree node, TreePath treePath) { // ignore anonymous classes - otherwise there will be a local reference to itself if (node.getIdentifier() != null && node.getClassBody() == null) { diff --git a/tests/minimized/src/main/java/minimized/AnnotationParameters.java b/tests/minimized/src/main/java/minimized/AnnotationParameters.java index af376dff..c4ec2229 100644 --- a/tests/minimized/src/main/java/minimized/AnnotationParameters.java +++ b/tests/minimized/src/main/java/minimized/AnnotationParameters.java @@ -25,4 +25,7 @@ interface Foo { @Nullable(("what")) Foo test4(); + + @Bar((double) -1) + double test(); } From 60f660f3cd42bacc49cbcf2e45cf580ba6cf84fd Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 29 Oct 2024 12:02:42 +0000 Subject: [PATCH 2/8] Fix cast tree --- .../scip_semanticdb/SignatureFormatter.java | 2 ++ .../semanticdb_javac/SemanticdbBuilders.java | 5 +++++ .../semanticdb_javac/SemanticdbTrees.java | 13 ++++++++----- .../main/java/minimized/AnnotationParameters.java | 2 +- .../main/java/minimized/AnnotationParameters.java | 8 ++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java b/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java index aec78d52..b4972416 100644 --- a/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java +++ b/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java @@ -470,6 +470,8 @@ private String formatTree(Tree tree) { + formatTree(tree.getAssignTree().getRhs()); } else if (tree.hasUnaryopTree()) { return formatUnaryOperation(tree.getUnaryopTree()); + } else if (tree.hasCastTree()) { + return "FIX ME"; } throw new IllegalArgumentException("tree was of unexpected type " + tree); diff --git a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java index d22aec4b..024f9781 100644 --- a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java +++ b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java @@ -150,6 +150,11 @@ public static Semanticdb.Tree tree(Semanticdb.UnaryOperatorTree unaryOperatorTre return Semanticdb.Tree.newBuilder().setUnaryopTree(unaryOperatorTree).build(); } + public static Semanticdb.Tree tree(Semanticdb.CastTree castTree) { + return Semanticdb.Tree.newBuilder().setCastTree(castTree).build(); + } + + public static Semanticdb.UnaryOperatorTree unaryOpTree( Semanticdb.UnaryOperator operator, Semanticdb.Tree rhs) { return Semanticdb.UnaryOperatorTree.newBuilder().setOp(operator).setTree(rhs).build(); diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java index 5511719d..2a150a00 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java @@ -96,10 +96,11 @@ public Semanticdb.AnnotationTree annotationBuilder(AnnotationTree annotation) { private TypeMirror getTreeType(Tree tree) { TreePath path = nodes.get(tree); - System.out.println("Path: " + path); - Element sym = trees.getElement(path); - System.out.println("SYM: " + sym); - return sym.asType(); + System.out.println("Path: " + path.getLeaf()); + // System.out.println("TM:" + trees.getTypeMirror(path)); + // Element sym = trees.getElement(path); + // System.out.println("SYM: " + sym); + return trees.getTypeMirror(path); } private Semanticdb.Tree annotationParameter(ExpressionTree expr) { @@ -164,14 +165,16 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) { } else if (expr instanceof TypeCastTree) { TypeCastTree tree = (TypeCastTree) expr; System.out.println(typeVisitor.semanticdbType(getTreeType(tree.getType()))); + return tree(castTree(typeVisitor.semanticdbType(getTreeType(tree.getType())), annotationParameter(tree.getExpression()))); // tree.getType() - } + } else { throw new IllegalArgumentException( semanticdbUri + ": annotation parameter rhs was of unexpected tree node type " + expr.getClass() + "\n" + expr); + } } private Semanticdb.BinaryOperator semanticdbBinaryOperator(Tree.Kind kind) { diff --git a/tests/minimized/src/main/java/minimized/AnnotationParameters.java b/tests/minimized/src/main/java/minimized/AnnotationParameters.java index c4ec2229..aab18e84 100644 --- a/tests/minimized/src/main/java/minimized/AnnotationParameters.java +++ b/tests/minimized/src/main/java/minimized/AnnotationParameters.java @@ -27,5 +27,5 @@ interface Foo { Foo test4(); @Bar((double) -1) - double test(); + double testCast(); } diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java index d2c068fa..efc0f49e 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java @@ -80,4 +80,12 @@ interface Foo { // display_name test4 // signature_documentation java @Nullable("what")\npublic abstract Foo test4() // kind AbstractMethod + +→@Bar((double) -1) +//^^^ reference semanticdb maven . . minimized/Bar# +→double testCast(); +// ^^^^^^^^ definition semanticdb maven . . minimized/Foo#testCast(). +// display_name testCast +// signature_documentation java @Bar(FIX ME)\npublic abstract double testCast() +// kind AbstractMethod } From 28984b1dff1809f01b8ac6fcc3e65d964a80a774 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 29 Oct 2024 12:07:03 +0000 Subject: [PATCH 3/8] Fix signature formatter for cast tree --- .../com/sourcegraph/scip_semanticdb/SignatureFormatter.java | 2 +- .../minimized/src/main/java/minimized/AnnotationParameters.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java b/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java index b4972416..7f53c8c9 100644 --- a/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java +++ b/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java @@ -471,7 +471,7 @@ private String formatTree(Tree tree) { } else if (tree.hasUnaryopTree()) { return formatUnaryOperation(tree.getUnaryopTree()); } else if (tree.hasCastTree()) { - return "FIX ME"; + return "(" + formatType(tree.getCastTree().getTpe()) + ")" + " " + formatTree(tree.getCastTree().getValue()); } throw new IllegalArgumentException("tree was of unexpected type " + tree); diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java index efc0f49e..faa7603f 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java @@ -86,6 +86,6 @@ interface Foo { →double testCast(); // ^^^^^^^^ definition semanticdb maven . . minimized/Foo#testCast(). // display_name testCast -// signature_documentation java @Bar(FIX ME)\npublic abstract double testCast() +// signature_documentation java @Bar((double) -1)\npublic abstract double testCast() // kind AbstractMethod } From ed9302d306a0b0d730dde58de653c805218dd941 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 29 Oct 2024 12:08:26 +0000 Subject: [PATCH 4/8] cleanup --- .../semanticdb_javac/SemanticdbTrees.java | 6 ---- .../semanticdb_javac/SemanticdbVisitor.java | 29 +------------------ 2 files changed, 1 insertion(+), 34 deletions(-) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java index 2a150a00..2099d04d 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java @@ -96,10 +96,6 @@ public Semanticdb.AnnotationTree annotationBuilder(AnnotationTree annotation) { private TypeMirror getTreeType(Tree tree) { TreePath path = nodes.get(tree); - System.out.println("Path: " + path.getLeaf()); - // System.out.println("TM:" + trees.getTypeMirror(path)); - // Element sym = trees.getElement(path); - // System.out.println("SYM: " + sym); return trees.getTypeMirror(path); } @@ -164,9 +160,7 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) { return annotationParameter(parenExpr.getExpression()); } else if (expr instanceof TypeCastTree) { TypeCastTree tree = (TypeCastTree) expr; - System.out.println(typeVisitor.semanticdbType(getTreeType(tree.getType()))); return tree(castTree(typeVisitor.semanticdbType(getTreeType(tree.getType())), annotationParameter(tree.getExpression()))); -// tree.getType() } else { throw new IllegalArgumentException( semanticdbUri diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java index 1c461aed..0c741f74 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java @@ -224,10 +224,7 @@ void resolveNodes() { resolveMemberSelectTree((MemberSelectTree) node, entry.getValue()); } else if (node instanceof NewClassTree) { resolveNewClassTree((NewClassTree) node, entry.getValue()); - } else if (node instanceof TypeCastTree) { - resolveCastTree((TypeCastTree) node, entry.getValue()); - System.out.println("Skipping resolution of [[\n" + node.getClass() + node.toString() + "\n]]"); - } + } } } } @@ -342,30 +339,6 @@ private void resolveMemberSelectTree(MemberSelectTree node, TreePath treePath) { } - private void resolveCastTree(TypeCastTree node, TreePath treePath) { - // ignore anonymous classes - otherwise there will be a local reference to itself - System.out.println("helo"); - // if (node.getIdentifier() != null && node.getClassBody() == null) { - // Element sym = trees.getElement(treePath); - // if (sym != null) { - // TreePath parentPath = treePath.getParentPath(); - // Element parentSym = trees.getElement(parentPath); - - // if (parentSym == null || parentSym.getKind() != ElementKind.ENUM_CONSTANT) { - // TreePath identifierTreePath = nodes.get(node.getIdentifier()); - // Element identifierSym = trees.getElement(identifierTreePath); - // emitSymbolOccurrence( - // sym, - // node, - // identifierSym.getSimpleName(), - // Role.REFERENCE, - // CompilerRange.FROM_TEXT_SEARCH); - // } - // } - // } - } - - private void resolveNewClassTree(NewClassTree node, TreePath treePath) { // ignore anonymous classes - otherwise there will be a local reference to itself if (node.getIdentifier() != null && node.getClassBody() == null) { From 45398258cd2e58e7c98632cd7f92d6582088c394 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 29 Oct 2024 12:11:59 +0000 Subject: [PATCH 5/8] Formatting --- .../scip_semanticdb/SignatureFormatter.java | 6 ++++- .../semanticdb_javac/SemanticdbBuilders.java | 5 +---- .../semanticdb_javac/SemanticdbTrees.java | 22 ++++++++++--------- .../semanticdb_javac/SemanticdbVisitor.java | 3 +-- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java b/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java index 7f53c8c9..fc306ad2 100644 --- a/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java +++ b/scip-semanticdb/src/main/java/com/sourcegraph/scip_semanticdb/SignatureFormatter.java @@ -471,7 +471,11 @@ private String formatTree(Tree tree) { } else if (tree.hasUnaryopTree()) { return formatUnaryOperation(tree.getUnaryopTree()); } else if (tree.hasCastTree()) { - return "(" + formatType(tree.getCastTree().getTpe()) + ")" + " " + formatTree(tree.getCastTree().getValue()); + return "(" + + formatType(tree.getCastTree().getTpe()) + + ")" + + " " + + formatTree(tree.getCastTree().getValue()); } throw new IllegalArgumentException("tree was of unexpected type " + tree); diff --git a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java index 024f9781..e49eb9c0 100644 --- a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java +++ b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java @@ -154,7 +154,6 @@ public static Semanticdb.Tree tree(Semanticdb.CastTree castTree) { return Semanticdb.Tree.newBuilder().setCastTree(castTree).build(); } - public static Semanticdb.UnaryOperatorTree unaryOpTree( Semanticdb.UnaryOperator operator, Semanticdb.Tree rhs) { return Semanticdb.UnaryOperatorTree.newBuilder().setOp(operator).setTree(rhs).build(); @@ -168,13 +167,11 @@ public static Semanticdb.AssignTree assignTree(Semanticdb.Tree lhs, Semanticdb.T return Semanticdb.AssignTree.newBuilder().setLhs(lhs).setRhs(rhs).build(); } - public static Semanticdb.CastTree castTree( - Semanticdb.Type type, Semanticdb.Tree value) { + public static Semanticdb.CastTree castTree(Semanticdb.Type type, Semanticdb.Tree value) { return Semanticdb.CastTree.newBuilder().setTpe(type).setValue(value).build(); } // SemanticDB Constants - public static Semanticdb.AnnotationTree annotationTree( Semanticdb.Type type, Iterable parameters) { return Semanticdb.AnnotationTree.newBuilder().setTpe(type).addAllParameters(parameters).build(); diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java index 2099d04d..439bf28f 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java @@ -89,15 +89,14 @@ public Semanticdb.AnnotationTree annotationBuilder(AnnotationTree annotation) { TreePath annotationTreePath = nodes.get(annotation); Element annotationSym = trees.getElement(annotationTreePath); - Semanticdb.Type type = - typeVisitor.semanticdbType(annotationSym.asType()); + Semanticdb.Type type = typeVisitor.semanticdbType(annotationSym.asType()); return annotationTree(type, params); } private TypeMirror getTreeType(Tree tree) { TreePath path = nodes.get(tree); return trees.getTypeMirror(path); - } + } private Semanticdb.Tree annotationParameter(ExpressionTree expr) { if (expr instanceof MemberSelectTree) { @@ -160,14 +159,17 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) { return annotationParameter(parenExpr.getExpression()); } else if (expr instanceof TypeCastTree) { TypeCastTree tree = (TypeCastTree) expr; - return tree(castTree(typeVisitor.semanticdbType(getTreeType(tree.getType())), annotationParameter(tree.getExpression()))); + return tree( + castTree( + typeVisitor.semanticdbType(getTreeType(tree.getType())), + annotationParameter(tree.getExpression()))); } else { - throw new IllegalArgumentException( - semanticdbUri - + ": annotation parameter rhs was of unexpected tree node type " - + expr.getClass() - + "\n" - + expr); + throw new IllegalArgumentException( + semanticdbUri + + ": annotation parameter rhs was of unexpected tree node type " + + expr.getClass() + + "\n" + + expr); } } diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java index 0c741f74..f8298532 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbVisitor.java @@ -224,7 +224,7 @@ void resolveNodes() { resolveMemberSelectTree((MemberSelectTree) node, entry.getValue()); } else if (node instanceof NewClassTree) { resolveNewClassTree((NewClassTree) node, entry.getValue()); - } + } } } } @@ -338,7 +338,6 @@ private void resolveMemberSelectTree(MemberSelectTree node, TreePath treePath) { } } - private void resolveNewClassTree(NewClassTree node, TreePath treePath) { // ignore anonymous classes - otherwise there will be a local reference to itself if (node.getIdentifier() != null && node.getClassBody() == null) { From 878b914723926baec56ca7271137b77af14f10f8 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 29 Oct 2024 12:35:54 +0000 Subject: [PATCH 6/8] Handle null constants --- .../sourcegraph/semanticdb_javac/SemanticdbBuilders.java | 6 ++++++ .../com/sourcegraph/semanticdb_javac/SemanticdbTrees.java | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java index e49eb9c0..fb656fad 100644 --- a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java +++ b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java @@ -190,6 +190,12 @@ public static Semanticdb.Constant doubleConst(Double value) { .build(); } + public static Semanticdb.Constant nullConst() { + return Semanticdb.Constant.newBuilder() + .setNullConstant(Semanticdb.NullConstant.newBuilder()) + .build(); + } + public static Semanticdb.Constant floatConst(Float value) { return Semanticdb.Constant.newBuilder() .setFloatConstant(Semanticdb.FloatConstant.newBuilder().setValue(value)) diff --git a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java index 439bf28f..f78b5abe 100644 --- a/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java +++ b/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java @@ -118,7 +118,12 @@ private Semanticdb.Tree annotationParameter(ExpressionTree expr) { // Literals can either be a primitive or String Object value = ((LiteralTree) expr).getValue(); final Semanticdb.Constant constant; - if (value instanceof String) constant = stringConst((String) value); + // Technically, annotation parameter values cannot be null, + // according to JLS: https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.7.1 + // But this codepath is still possible to hit when compiling invalid code - and + // we should handle the null const case in order to fail more gracefully + if (value == null) constant = nullConst(); + else if (value instanceof String) constant = stringConst((String) value); else if (value instanceof Boolean) constant = booleanConst((Boolean) value); else if (value instanceof Byte) constant = byteConst((Byte) value); else if (value instanceof Short) constant = shortConst((Short) value); From 51a7e302ff72797111d55bc1892c05c935700b40 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 29 Oct 2024 12:36:04 +0000 Subject: [PATCH 7/8] Add a test case for annotations in annotation parameters --- .../java/minimized/AnnotationParameters.java | 10 ++++++ .../java/minimized/AnnotationParameters.java | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/tests/minimized/src/main/java/minimized/AnnotationParameters.java b/tests/minimized/src/main/java/minimized/AnnotationParameters.java index aab18e84..1ec1faf3 100644 --- a/tests/minimized/src/main/java/minimized/AnnotationParameters.java +++ b/tests/minimized/src/main/java/minimized/AnnotationParameters.java @@ -12,6 +12,11 @@ String value() default ""; } + +@interface BarRef{ + SuppressWarnings value(); +} + interface Foo { @Bar(-1d) double test(); @@ -29,3 +34,8 @@ interface Foo { @Bar((double) -1) double testCast(); } + +interface TestRef { + @BarRef(@SuppressWarnings(value = "unchecked")) + abstract double testCase(); +} diff --git a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java index faa7603f..8dc8786f 100644 --- a/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java +++ b/tests/snapshots/src/main/generated/tests/minimized/src/main/java/minimized/AnnotationParameters.java @@ -40,6 +40,21 @@ // kind AbstractMethod } + +@interface BarRef{ +// ^^^^^^ definition semanticdb maven . . minimized/BarRef# +// display_name BarRef +// signature_documentation java @interface BarRef +// kind Interface +// relationship is_implementation semanticdb maven jdk 11 java/lang/annotation/Annotation# +→SuppressWarnings value(); +//^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +// ^^^^^ definition semanticdb maven . . minimized/BarRef#value(). +// display_name value +// signature_documentation java public abstract SuppressWarnings value() +// kind AbstractMethod +} + interface Foo { // ^^^ definition semanticdb maven . . minimized/Foo# // display_name Foo @@ -89,3 +104,19 @@ interface Foo { // signature_documentation java @Bar((double) -1)\npublic abstract double testCast() // kind AbstractMethod } + +interface TestRef { +// ^^^^^^^ definition semanticdb maven . . minimized/TestRef# +// display_name TestRef +// signature_documentation java interface TestRef +// kind Interface +→@BarRef(@SuppressWarnings(value = "unchecked")) +//^^^^^^ reference semanticdb maven . . minimized/BarRef# +// ^^^^^^^^^^^^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings# +// ^^^^^ reference semanticdb maven jdk 11 java/lang/SuppressWarnings#value(). +→abstract double testCase(); +// ^^^^^^^^ definition semanticdb maven . . minimized/TestRef#testCase(). +// display_name testCase +// signature_documentation java @BarRef(@SuppressWarnings("unchecked"))\npublic abstract double testCase() +// kind AbstractMethod +} From bd70d53df426ecc136007db3645bc9d25df0850e Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 29 Oct 2024 12:40:45 +0000 Subject: [PATCH 8/8] remove wrong comment --- .../com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java index fb656fad..d592efaf 100644 --- a/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java +++ b/semanticdb-java/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbBuilders.java @@ -170,12 +170,12 @@ public static Semanticdb.AssignTree assignTree(Semanticdb.Tree lhs, Semanticdb.T public static Semanticdb.CastTree castTree(Semanticdb.Type type, Semanticdb.Tree value) { return Semanticdb.CastTree.newBuilder().setTpe(type).setValue(value).build(); } - // SemanticDB Constants public static Semanticdb.AnnotationTree annotationTree( Semanticdb.Type type, Iterable parameters) { return Semanticdb.AnnotationTree.newBuilder().setTpe(type).addAllParameters(parameters).build(); } + // SemanticDB Constants public static Semanticdb.Constant stringConst(String value) {