Skip to content

Allow protected member access of java.lang.Object#2183

Merged
suztomo merged 2 commits intomasterfrom
object_methods
Aug 12, 2021
Merged

Allow protected member access of java.lang.Object#2183
suztomo merged 2 commits intomasterfrom
object_methods

Conversation

@suztomo
Copy link
Copy Markdown
Contributor

@suztomo suztomo commented Aug 12, 2021

Allow protected member access of java.lang.Object, because all objects are subclasses of java.lang.Object.

Fixes #2177. Before this change Linkage Checker would throw errors on the methods of java.lang.Object:

java.lang.Object's method finalize() is not accessible;
  referenced by 6 class files
    groovy.lang.GroovyLogTestCase (org.codehaus.groovy:groovy-all:2.3.6)
    groovy.util.GroovyShellTestCase (org.codehaus.groovy:groovy-all:2.3.6)
    groovy.util.JavadocAssertionTestSuite (org.codehaus.groovy:groovy-all:2.3.6)
    org.codehaus.groovy.tools.shell.CommandsMultiCompleter (org.codehaus.groovy:groovy-all:2.3.6)

However, the member is "protected" (https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#finalize()) and java.lang.Object is the super class of all Java classes. These errors are false positives. This PR fixes this problem.

@google-cla google-cla bot added the cla: yes label Aug 12, 2021
@suztomo suztomo requested a review from a team August 12, 2021 17:40
if (ClassDumper.classesInSamePackage(targetClass.getClassName(), sourceClassName)) {
return true;
}
if (Object.class.getName().equals(targetClass.getClassName())) {
Copy link
Copy Markdown
Contributor

@chanseokoh chanseokoh Aug 12, 2021

Choose a reason for hiding this comment

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

Does it also need ... && member.notPrivate()?

Copy link
Copy Markdown
Contributor

@chanseokoh chanseokoh Aug 12, 2021

Choose a reason for hiding this comment

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

Also, should we ensure targetClass is sourceClass or a subclass of it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Line 508 checks whether this member is protected or not. I don't think a method can have protected and private.

Line 519 checks whether it's subclass or not. Now I think this condition should be in ClassDumper.isClassSubClassOf. Updating this.

Copy link
Copy Markdown
Contributor Author

@suztomo suztomo Aug 12, 2021

Choose a reason for hiding this comment

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

I changed my mind. Line 519 requires to load java.lang.Object. But the current code (c883b01) doesn't require it and it's obvious that all objects are subclass of java.lang.Object. So I keep the code as it is.

For other superclasses, line 519 checks the class hierarchy relationship.

Copy link
Copy Markdown
Contributor

@chanseokoh chanseokoh Aug 12, 2021

Choose a reason for hiding this comment

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

Line 508 checks whether this member is protected or not.

Ah, I missed it.

Also, should we ensure targetClass is sourceClass or a subclass of it?

Maybe I misunderstood the context, but what I mean above is this:

  Object obj = new Object();
  Runnable runnable = new Runnable() {
    @Override
    public void run() {
      // sourceClass = runnable
      // targetClass = obj
      // runnable cannot call obj.clone() as it's protected
      obj.clone();  // doesn't compile
    }
  };

Or, is it that I'm out of the context?

Copy link
Copy Markdown
Contributor Author

@suztomo suztomo Aug 12, 2021

Choose a reason for hiding this comment

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

Thank you for the example. That's a new perspective to me. Unfortunately Linkage Checker cannot determine such cases. In this example below, the two references are the same to Linkage Checker:
Screen Shot 2021-08-12 at 15 30 16

As Linkage Checker deal with class files in JAR files, we can assume the code related to the java.lang.Object methods are already passed the compilation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cool, that's fair!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Created an issue for this potential false negatives.

#2184

Copy link
Copy Markdown
Contributor Author

@suztomo suztomo left a comment

Choose a reason for hiding this comment

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

@chanseokoh PTAL.

if (ClassDumper.classesInSamePackage(targetClass.getClassName(), sourceClassName)) {
return true;
}
if (Object.class.getName().equals(targetClass.getClassName())) {
Copy link
Copy Markdown
Contributor Author

@suztomo suztomo Aug 12, 2021

Choose a reason for hiding this comment

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

I changed my mind. Line 519 requires to load java.lang.Object. But the current code (c883b01) doesn't require it and it's obvious that all objects are subclass of java.lang.Object. So I keep the code as it is.

For other superclasses, line 519 checks the class hierarchy relationship.

@suztomo suztomo requested a review from chanseokoh August 12, 2021 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty Groovy file generating linkage errors on Object's finalize() method

2 participants