-
Notifications
You must be signed in to change notification settings - Fork 7
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
Skip serialization for elements in generated classes in Scanner #231
Open
nimakarimipour
wants to merge
6
commits into
master
Choose a base branch
from
nimak/skip-generated-class
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b3e23ea
skip generated elements
nimakarimipour 87fbc9d
update year
nimakarimipour a508bec
bug fix
nimakarimipour 778ad88
bug fix
nimakarimipour 7e88eff
rename
nimakarimipour b552938
refine code annotation info
nimakarimipour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...ator-scanner/src/main/java/edu/ucr/cs/riple/scanner/generatedcode/CodeAnnotationInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Nima Karimipour | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package edu.ucr.cs.riple.scanner.generatedcode; | ||
|
||
import static edu.ucr.cs.riple.scanner.SymbolUtil.hasDirectAnnotationWithSimpleName; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.cache.Cache; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.tools.javac.code.Symbol; | ||
import com.sun.tools.javac.util.Context; | ||
|
||
/** | ||
* Provides APIs for querying whether code is annotated for nullness checking, and for related | ||
* queries on what annotations are present on a class/method and/or on relevant enclosing scopes | ||
* (i.e. enclosing classes or methods). Makes use of caching internally for performance. | ||
* | ||
* <p>NOTE: THIS SOURCE FILE IS COPIED AND MODIFIED FROM UBER <a | ||
* href="https://github.com/uber/NullAway">NULLAWAY</a> SOURCE CODE | ||
*/ | ||
public final class CodeAnnotationInfo { | ||
|
||
private static final Context.Key<CodeAnnotationInfo> ANNOTATION_INFO_KEY = new Context.Key<>(); | ||
|
||
private static final int MAX_CLASS_CACHE_SIZE = 200; | ||
|
||
private final Cache<Symbol.ClassSymbol, Symbol.ClassSymbol> classCache = | ||
CacheBuilder.newBuilder().maximumSize(MAX_CLASS_CACHE_SIZE).build(); | ||
|
||
private CodeAnnotationInfo() {} | ||
|
||
/** | ||
* Get the CodeAnnotationInfo for the given javac context. We ensure there is one instance per | ||
* context (as opposed to using static fields) to avoid memory leaks. | ||
*/ | ||
public static CodeAnnotationInfo instance(Context context) { | ||
CodeAnnotationInfo annotationInfo = context.get(ANNOTATION_INFO_KEY); | ||
if (annotationInfo == null) { | ||
annotationInfo = new CodeAnnotationInfo(); | ||
context.put(ANNOTATION_INFO_KEY, annotationInfo); | ||
} | ||
return annotationInfo; | ||
} | ||
|
||
/** | ||
* Check if a symbol comes from generated code. | ||
* | ||
* @param symbol symbol for entity | ||
* @return true if symbol represents an entity contained in a class annotated with | ||
* {@code @Generated}; false otherwise | ||
*/ | ||
public boolean isInGeneratedClass(Symbol symbol) { | ||
Symbol.ClassSymbol classSymbol = | ||
symbol instanceof Symbol.ClassSymbol | ||
? (Symbol.ClassSymbol) symbol | ||
: ASTHelpers.enclosingClass(symbol); | ||
if (classSymbol == null) { | ||
return false; | ||
} | ||
Symbol.ClassSymbol outermostClassSymbol = get(classSymbol); | ||
return hasDirectAnnotationWithSimpleName(outermostClassSymbol, "Generated"); | ||
} | ||
|
||
/** | ||
* Retrieve the (outermostClass, isNullMarked) record for a given class symbol. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this copy-pasted comment is wrong, since the code around null-markedness has been removed |
||
* | ||
* <p>This method is recursive, using the cache on the way up and populating it on the way down. | ||
* | ||
* @param classSymbol The class to query, possibly an inner class | ||
* @return A record including the outermost class in which the given class is nested, as well as | ||
* boolean flag noting whether it should be treated as nullness-annotated, taking into account | ||
* annotations on enclosing classes, the containing package, and other NullAway configuration | ||
* like annotated packages | ||
*/ | ||
private Symbol.ClassSymbol get(Symbol.ClassSymbol classSymbol) { | ||
Symbol.ClassSymbol record = classCache.getIfPresent(classSymbol); | ||
if (record != null) { | ||
return record; | ||
} | ||
if (classSymbol.getNestingKind().isNested()) { | ||
Symbol owner = classSymbol.owner; | ||
Preconditions.checkNotNull(owner, "Symbol.owner should only be null for modules!"); | ||
Symbol.ClassSymbol enclosingClass = ASTHelpers.enclosingClass(classSymbol); | ||
// enclosingClass can be null in weird cases like for array methods | ||
if (enclosingClass != null) { | ||
record = get(enclosingClass); | ||
} | ||
} | ||
if (record == null) { | ||
// We are already at the outermost class (we can find), so let's create a record for it | ||
record = classSymbol; | ||
} | ||
classCache.put(classSymbol, record); | ||
return record; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment needs an update?