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

Migrate from lombok API to Psi APIs #227

Conversation

eyedol
Copy link
Contributor

@eyedol eyedol commented Dec 3, 2016

This fixes #226

  • Bump lint library version.
  • Migrate CallNeedsPermissionDector to use the new Psi APIs.
  • Migrate NoCorrespondingNeedsPermissionDector to the new Psi APIs.
  • Migrate CallOnRequestPermissionsResultDetector to the new Psi APIs.
  • Add test for CallNeedsPermissionDector migration.
  • Add test for NoCorrespondingNeedsPermissionDector migration.
  • Add test for CallOnRequestPermissionsResultDetector migration.

@hotchemi for a review before I cause enough mess :-)

@eyedol
Copy link
Contributor Author

eyedol commented Dec 3, 2016

I'm getting an error when I run lint sample on the command line. I'm suspecting a bug in the lint tool. For some reason the packaged lint.jar is missing the Psi packages. This issue made me open this initial PR so I can be pointed in the right direction if I'm doing something obviously wrong.

Could not load custom rule jar file ~/.android/lint/lint.jar
java.lang.NoClassDefFoundError: com/android/tools/lint/detector/api/Detector$JavaPsiScanner
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at permissions.dispatcher.PermissionsDispatcherIssueRegistry.getIssues(PermissionsDispatcherIssueRegistry.java:13)
	at com.android.tools.lint.client.api.JarFileIssueRegistry.<init>(JarFileIssueRegistry.java:104)
	at com.android.tools.lint.client.api.JarFileIssueRegistry.get(JarFileIssueRegistry.java:80)
	at com.android.tools.lint.client.api.LintDriver.registerCustomDetectors(LintDriver.java:481)
	at com.android.tools.lint.client.api.LintDriver.analyze(LintDriver.java:415)
	at com.android.tools.lint.client.api.LintDriver.analyze(LintDriver.java:377)
	at com.android.tools.lint.LintCliClient.run(LintCliClient.java:131)
	at com.android.tools.lint.Main.run(Main.java:674)
	at com.android.tools.lint.Main.main(Main.java:118)
Caused by: java.lang.ClassNotFoundException: com.android.tools.lint.detector.api.Detector$JavaPsiScanner
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 20 more

@eyedol eyedol force-pushed the 226-update-custom-rules-to-new-api branch 2 times, most recently from a6bd02f to 3b620d8 Compare December 4, 2016 12:30
@hotchemi
Copy link
Member

hotchemi commented Dec 4, 2016

@eyedol Thx! Let me take a look.

@hotchemi
Copy link
Member

hotchemi commented Dec 4, 2016

Is this still a WIP? I dunno the reason but seems ci had passed.

return Collections.<Class<? extends PsiElement>>singletonList(PsiClass.class);
}

public CallNeedsPermissionDetector() {
Copy link
Member

Choose a reason for hiding this comment

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

Is this constructor needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. It's being used by its corresponding test class. Plus the documentation on writing a lint-check recommends adding it.

Copy link
Member

Choose a reason for hiding this comment

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

Noted.

@eyedol
Copy link
Contributor Author

eyedol commented Dec 5, 2016

@hotchemi Yes still a WIP. I'm yet to migrate CallOnRequestPermissionsResultDetector class. I see that it implements Detector.ClassScanner and I haven't seen its equivalent for Psi. Any reason why you're scanning the class file instead of the Java file? I want to migrate it to Detector.JavaPsiScanner and want to justify if it's okay.

@shiraji
Copy link
Member

shiraji commented Dec 5, 2016

#131

I'm not sure the new api works well or not. But when I used JavaScanner, there are many reports the lint produced red lines. The problem is when I implemented the lint, it does not happen...

@hotchemi
Copy link
Member

hotchemi commented Dec 5, 2016

@eyedol Thx. Can you continue to convert CallOnRequestPermissionsResultDetector?

@eyedol
Copy link
Contributor Author

eyedol commented Dec 5, 2016

@shiraji Thanks for the comment. I see. It's more to circumvent around an issue then. I'll use the Detector.JavaPsiScanner class then.

@hotchemi Yeah, I'll migrate it. I won't be able to get to it until tomorrow though.

@hotchemi
Copy link
Member

hotchemi commented Dec 5, 2016

Got it, Thx!


static List<String> generatedClassNames = new ArrayList<String>();

static List<String> METHODS = Collections.emptyList();

Choose a reason for hiding this comment

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

private static final

Copy link
Member

Choose a reason for hiding this comment

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

Well, it's a little bit not intuitive to add something to final list for me, so how about make this variable being just static and rename like normal variable?

Choose a reason for hiding this comment

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

if you omit the getApplicableMethodNames() method, then you don't need this at all

private static class AnnotationChecker extends ForwardingAstVisitor {
@Override
public List<String> getApplicableMethodNames() {
return METHODS;

Choose a reason for hiding this comment

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

if you never plan to add explicit method names here, it's safe to omit this method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good to know.


private Set<PsiAnnotation> needsPermissionAnnotations;

private Set<PsiAnnotation> onShowRationaleAnnotations;

Choose a reason for hiding this comment

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

final both

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

baz.append("public class Baz {\n");
baz.append("public void fooBar() {\n");
baz.append("}\n");
baz.append("}");

Choose a reason for hiding this comment

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

there's no need to use a StringBuilder here. Just concatenate strings directly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was a bit messy concatenating them together using + because of the code formatter.

// Needed for lint-tests and testutils 25.2.0 packages
maven {
url 'https://oss.sonatype.org/content/repositories/releases/'
}

Choose a reason for hiding this comment

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

use mavenCentral() instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted.

if (classes.length > 0 && classes[0].getName() != null) {
String qualifiedName = classes[0].getName();
if (qualifiedName.contains("PermissionsDispatcher") || qualifiedName
.contains("PermissionsDispatcher")) {

Choose a reason for hiding this comment

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

you got the exact same check twice in this if statement

Choose a reason for hiding this comment

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

forgot to remove this duplicated check?

@@ -24,3 +24,8 @@ jar {
attributes("Lint-Registry": "permissions.dispatcher.PermissionsDispatcherIssueRegistry")
}
}

// Enable test build status on stdout
tasks.matching { it instanceof Test }.all {
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed?


static List<String> generatedClassNames = new ArrayList<String>();

static List<String> METHODS = Collections.emptyList();
Copy link
Member

Choose a reason for hiding this comment

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

Well, it's a little bit not intuitive to add something to final list for me, so how about make this variable being just static and rename like normal variable?

return Collections.<Class<? extends PsiElement>>singletonList(PsiClass.class);
}

public CallNeedsPermissionDetector() {
Copy link
Member

Choose a reason for hiding this comment

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

Noted.

@eyedol eyedol force-pushed the 226-update-custom-rules-to-new-api branch from 3b620d8 to df04784 Compare December 6, 2016 12:40
@eyedol
Copy link
Contributor Author

eyedol commented Dec 6, 2016

Ported the last detector class. I'll appreciate a review @shiraji @hotchemi @felipecsl

Copy link
Member

@hotchemi hotchemi left a comment

Choose a reason for hiding this comment

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

Thx for your great work!

@@ -14,5 +14,7 @@ buildscript {
allprojects {
repositories {
jcenter()
// Needed for lint-tests and testutils 25.2.0 packages
Copy link
Member

Choose a reason for hiding this comment

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

This line is not needed:D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree :-D

@@ -16,6 +16,10 @@ android {
targetCompatibility JavaVersion.VERSION_1_6
}

lintOptions {
abortOnError false
Copy link
Member

Choose a reason for hiding this comment

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

Why this line is needed?

Choose a reason for hiding this comment

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

yeah this should definitely be removed :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hotchemi It was complaining earlier from a broken build. I had to set it as a workaround. It slid through by mistake. Removed.

@Override
protected List<Issue> getIssues() {
return ImmutableList.of(CallOnRequestPermissionsResultDetector.ISSUE);

Copy link
Member

Choose a reason for hiding this comment

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

Please remove this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an abstract method from LintDetectorTest that needs to be implemented.

if (hasRuntimePermissionAnnotation && !hasOnRequestPermissionResultCall) {
context.report(ISSUE, context.getLocation(classDeclaration), "Generated onRequestPermissionsResult method not called");
private static boolean checkMethodCall(PsiMethod method, PsiClass psiClass) {
// FIXME: I'm sure there is a better way of checking if the on onRequestPermissionsResult
Copy link
Member

Choose a reason for hiding this comment

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

Any idea for this?

Copy link

@felipecsl felipecsl left a comment

Choose a reason for hiding this comment

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

There's a bunch of comments that haven't been addressed yet, but making progress. Good work 👍

@@ -16,6 +16,10 @@ android {
targetCompatibility JavaVersion.VERSION_1_6
}

lintOptions {
abortOnError false

Choose a reason for hiding this comment

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

yeah this should definitely be removed :)

private final JavaContext context;

private Set<String> matchingAnnotationTypeNames;

Choose a reason for hiding this comment

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

i know you didnt change this line, but while you're at it, make it final

@eyedol eyedol force-pushed the 226-update-custom-rules-to-new-api branch from df04784 to 7fdcb6f Compare December 6, 2016 22:15
@eyedol
Copy link
Contributor Author

eyedol commented Dec 6, 2016

Thanks guys. I have fixed the comments.

Copy link

@felipecsl felipecsl left a comment

Choose a reason for hiding this comment

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

Still some stuff missing

private static class AnnotationChecker extends ForwardingAstVisitor {
@Override
public List<String> getApplicableMethodNames() {
return methods;

Choose a reason for hiding this comment

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

this still can be omitted

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I remove test breaks.

Copy link

@felipecsl felipecsl Dec 6, 2016

Choose a reason for hiding this comment

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

ok never mind then

if (classes.length > 0 && classes[0].getName() != null) {
String qualifiedName = classes[0].getName();
if (qualifiedName.contains("PermissionsDispatcher") || qualifiedName
.contains("PermissionsDispatcher")) {

Choose a reason for hiding this comment

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

forgot to remove this duplicated check?

@eyedol eyedol force-pushed the 226-update-custom-rules-to-new-api branch 2 times, most recently from 8f58687 to 1960b69 Compare December 6, 2016 22:54
- Bump lint library version
- Migrate `CallNeedsPermissionDector` to the new Psi API
- Migrate `NoCorrespondingNeedsPermissionDector` to the new Psi API
- Add maven repository for needed depenencies for test compiles
- Add test for the migrations
- Show test status on the console. Useful on CI
- Remove @VisibleOnTesting annotation detector default constructors.
- Hand create mock classes in the test class instead of loading them
from a separate package
- Migrate `CallOnRequestPermissionsResultDetector` to the new Psi API
- Upgrade android gradle plugin
- Pull dependencies from `mavencentral`
@eyedol eyedol force-pushed the 226-update-custom-rules-to-new-api branch from 1960b69 to 96df703 Compare December 7, 2016 00:58
@hotchemi
Copy link
Member

hotchemi commented Dec 7, 2016

@eyedol Thx!!! Can I merge?

@shiraji
Copy link
Member

shiraji commented Dec 7, 2016

what library-source.jar for?

@eyedol
Copy link
Contributor Author

eyedol commented Dec 7, 2016

@hotchemi It looks good to me. You can merge.

@shiraji good question. It came with my fork. See this

@shiraji
Copy link
Member

shiraji commented Dec 7, 2016

I think it hasn't updated it because there were no changes. LGTM! Thanks for your contribution!

@hotchemi
Copy link
Member

hotchemi commented Dec 7, 2016

@shiraji Well, actually we have shameful dependency relationship between each modules so that we have workaround...will be fixed on #27.
ref: https://github.com/hotchemi/PermissionsDispatcher/blob/master/library/build.gradle#L81

@hotchemi hotchemi merged commit 263bf45 into permissions-dispatcher:master Dec 7, 2016
@hotchemi
Copy link
Member

hotchemi commented Dec 7, 2016

@eyedol Really appreciate for your awesome work!!!!!
@felipecsl Thx for your joining to code review! I'm gonna release new ver and notice tonight(JST):D

@felipecsl
Copy link

Awesome, thank you very much guys!!

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.

Update custom lint rules to the new PSI API
4 participants