Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java_indexer): mark this/super ctor calls as direct #5611

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions kythe/java/com/google/devtools/kythe/analyzers/java/JavaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableList;
import com.google.devtools.kythe.analyzers.base.EntrySet;
import com.google.devtools.kythe.proto.Storage.VName;
import com.sun.tools.javac.code.Symbol;
import java.util.List;
import java.util.Optional;

Expand All @@ -28,6 +29,7 @@ class JavaNode {
// (i.e. types and abstractions)
private final VName vName;
private JavaNode type;
private Symbol sym;

private ImmutableList<VName> classConstructors = ImmutableList.of();
private Optional<VName> classInit = Optional.empty();
Expand Down Expand Up @@ -71,6 +73,15 @@ JavaNode getType() {
return type;
}

JavaNode setSymbol(Symbol sym) {
this.sym = sym;
return this;
}

Symbol getSymbol() {
return sym;
}

JavaNode setClassConstructors(List<VName> constructors) {
this.classConstructors = ImmutableList.copyOf(constructors);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,12 @@ public JavaNode visitApply(JCMethodInvocation invoke, TreeContext owner) {
return emitDiagnostic(ctx, "error analyzing method", null, null);
}

emitAnchor(ctx, EdgeKind.REF_CALL, method.getVName());
emitAnchor(
ctx,
Optional.ofNullable(method.getSymbol()).map(Symbol::isConstructor).orElse(false)
? EdgeKind.REF_CALL_DIRECT
: EdgeKind.REF_CALL,
method.getVName());
return method;
}

Expand Down Expand Up @@ -1199,15 +1204,19 @@ private static List<VName> toVNames(Iterable<JavaNode> nodes) {
/** Returns the {@link JavaNode} associated with a {@link Symbol} or {@code null}. */
private @Nullable JavaNode getJavaNode(Symbol sym) {
if (sym.getKind() == ElementKind.PACKAGE) {
return new JavaNode(entrySets.newPackageNodeAndEmit((PackageSymbol) sym).getVName());
return new JavaNode(entrySets.newPackageNodeAndEmit((PackageSymbol) sym).getVName())
.setSymbol(sym);
}

VName jvmNode = getJvmNode(sym);

JavaNode node =
signatureGenerator
.getSignature(sym)
.map(sig -> new JavaNode(entrySets.getNode(signatureGenerator, sym, sig, null)))
.map(
sig ->
new JavaNode(entrySets.getNode(signatureGenerator, sym, sig, null))
.setSymbol(sym))
.orElse(null);
if (node != null && jvmNode != null) {
entrySets.emitEdge(node.getVName(), EdgeKind.NAMED, jvmNode);
Expand Down Expand Up @@ -1400,7 +1409,8 @@ private JavaNode resolveJavaLangSymbol(Symbol sym) {
// This usually indicates a problem with the compilation's bootclasspath.
return emitDiagnostic(null, "failed to resolve " + sym, null, null);
}
return new JavaNode(entrySets.getNode(signatureGenerator, sym, signature.get(), null, null));
return new JavaNode(entrySets.getNode(signatureGenerator, sym, signature.get(), null, null))
.setSymbol(sym);
}

private void emitMetadata(Span span, VName node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ public class Classes {

//- @Subclass defines/binding SubclassOne
private static class Subclass extends Classes {
//- ImplicitSuperCall ref/call DefaultCtor
//- ImplicitSuperCall ref/call/direct DefaultCtor
//- ImplicitSuperCall.loc/start @^"{}"
//- ImplicitSuperCall.loc/end @^"{}"
//- ImplicitSuperCall childof SubclassCtor
//- @Subclass defines/binding SubclassCtor
Subclass() {}

Subclass(String s) {
//- @"this()" ref/call/direct SubclassCtor
this();
}

Subclass(int i) {
//- @"super()" ref/call/direct DefaultCtor
super();
}
}

//- @Subclass2 defines/binding SubclassTwo
Expand All @@ -42,7 +52,7 @@ private static class Subclass2 extends Classes {
//- ImplicitSubclassTwoCtor.node/kind function
//- ImplicitSubclassTwoCtor.subkind constructor
//- ImplicitSubclassTwoCtor childof SubclassTwo
//- ExtraImplicitSuperCall ref/call DefaultCtor
//- ExtraImplicitSuperCall ref/call/direct DefaultCtor
//- ExtraImplicitSuperCall childof ImplicitSubclassTwoCtor
}

Expand Down Expand Up @@ -77,7 +87,6 @@ private void localFunc() {
//- @LocalClass defines/binding LC
//- LC childof LF
class LocalClass {}
;
}

//- ClassInit.node/kind function
Expand All @@ -88,7 +97,6 @@ class LocalClass {}
//- @LocalClassInStaticInitializer defines/binding LCISI
//- LCISI childof ClassInit
class LocalClassInStaticInitializer {}
;
}
}

Expand Down