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

Add APIs to get JVM names of functions and property accessors #141

Merged
merged 8 commits into from
Nov 13, 2020

Conversation

yigit
Copy link
Collaborator

@yigit yigit commented Nov 4, 2020

This PR adds a new API getJvmName to get the jvm name of a method or accessor.

This is crucial for annotation processors that generate java source code as they might need to access internal methods in the same module compilation (as it works w/ KAPT).

Fixes #129

@@ -141,4 +141,33 @@ interface Resolver {
function: KSFunctionDeclaration,
containing: KSType
): KSFunction

/**
* Returns the jvm name of the given function.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also mention that this ties to Kotlin compiler's implementation and is not guaranteed to be cross-version compatible? Namely, not guaranteed to work on libraries compiled by other version of compilers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated. lmk.

// internalInlineReturningFun -> internalInlineReturningFun-HRn7Rpw$mainModule
// fileLevelInternalFun -> fileLevelInternalFun
// fileLevelInlineReceivingFun -> fileLevelInlineReceivingFun-E03SJzc
// fileLevelInlineReturningFun -> fileLevelInlineReturningFun
Copy link
Collaborator

@ting-yuan ting-yuan Nov 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why this and fileLevelInternalInlineReturningFun aren't mangled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i honestly don't know 🤦
I got these outputs from compiling the same input on a sample project and it is not mangled there.

➜  yigit-google-ksp git:(mangled-names) javap -p ~/src/ManglingPlayground/mainModule/build/classes/kotlin/main/mainPackage/InputKt.class
Compiled from "input.kt"
public final class mainPackage.InputKt {
  public static final void fileLevelInternalFun();
  public static final void fileLevelInlineReceivingFun-E03SJzc(java.lang.String);
  public static final java.lang.String fileLevelInlineReturningFun();
  public static final void fileLevelInternalInlineReceivingFun-E03SJzc(java.lang.String);
  public static final java.lang.String fileLevelInternalInlineReturningFun();
}

vs it is mangled when it is on a class:

➜  yigit-google-ksp git:(mangled-names) javap -p ~/src/ManglingPlayground/mainModule/build/classes/kotlin/main/mainPackage/Foo.class    
Compiled from "input.kt"
public final class mainPackage.Foo {
  private java.lang.String normalProp;
  private java.lang.String inlineProp;
  private java.lang.String internalProp;
  private java.lang.String internalInlineProp;
  public final java.lang.String getNormalProp();
  public final void setNormalProp(java.lang.String);
  public final java.lang.String getInlineProp-HRn7Rpw();
  public final void setInlineProp-E03SJzc(java.lang.String);
  public final java.lang.String getInternalProp$mainModule();
  public final void setInternalProp$mainModule(java.lang.String);
  public final java.lang.String getInternalInlineProp-HRn7Rpw$mainModule();
  public final void setInternalInlineProp-E03SJzc$mainModule(java.lang.String);
  public final void normalFun();
  public final void inlineReceivingFun-E03SJzc(java.lang.String);
  public final java.lang.String inlineReturningFun-HRn7Rpw();
  public final void internalInlineReceivingFun-E03SJzc$mainModule(java.lang.String);
  public final java.lang.String internalInlineReturningFun-HRn7Rpw$mainModule();
  public mainPackage.Foo();
}

@yigit
Copy link
Collaborator Author

yigit commented Nov 5, 2020

addressed the review comments and also added a test case with JvmName. Which is also another bonus of this CL as it was something that Room would need to handle manually.

@yigit
Copy link
Collaborator Author

yigit commented Nov 10, 2020

can we squash & merge this one? (I don't have merge access)

@yigit
Copy link
Collaborator Author

yigit commented Nov 13, 2020

added KspExperimental annotation.
btw, also noticed the comment above was for another PR 🤦

@yigit yigit requested a review from ting-yuan November 13, 2020 02:32
@ting-yuan ting-yuan merged commit 12e3cbe into google:master Nov 13, 2020
neetopia pushed a commit that referenced this pull request Dec 23, 2020
This API `getJvmName` and `mapToJvmSignature` are also marked as `KspExperimental` and require explicit opt-in.

See https://kotlinlang.org/docs/reference/opt-in-requirements.html for details.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

provide an API to get jvm names
2 participants