-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Initial implementation for resolving records #3606
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
509c646
Initial implementation for resolving records
MysterAitch ad29b3d
Inline some constants used only once.
MysterAitch c19fb1f
Implement solving of record superclass (`java.lang.Record`) and solvi…
MysterAitch 0c6a500
Merge remote-tracking branch 'javaparser/master' into 2446--solving_r…
MysterAitch 0bfe74b
Conditional test based on whether `java.lang.Record` is available via…
MysterAitch 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
87 changes: 87 additions & 0 deletions
87
...rc/main/java/com/github/javaparser/resolution/declarations/ResolvedRecordDeclaration.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,87 @@ | ||
/* | ||
* Copyright (C) 2007-2010 Júlio Vilmar Gesser. | ||
* Copyright (C) 2011, 2013-2021 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.resolution.declarations; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.resolution.types.ResolvedReferenceType; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Declaration of a Record (not a class or an interface or an enum). | ||
* | ||
* Note that it can be associated to a Node AST because anonymous class declarations return an incompatible | ||
* node type, compared to classic class declarations. | ||
* | ||
* @author Federico Tomassetti | ||
*/ | ||
public interface ResolvedRecordDeclaration extends ResolvedReferenceTypeDeclaration, | ||
ResolvedTypeParametrizable, HasAccessSpecifier, AssociableToAST<Node> { | ||
|
||
|
||
@Override | ||
default boolean isRecord() { | ||
return true; | ||
} | ||
|
||
@Override | ||
default ResolvedRecordDeclaration asRecord() { | ||
return this; | ||
} | ||
|
||
Optional<ResolvedReferenceType> getSuperClass(); | ||
|
||
/** | ||
* Get all superclasses, with all the type typeParametersValues expressed as functions of the type | ||
* typeParametersValues of this declaration. | ||
*/ | ||
List<ResolvedReferenceType> getAllSuperClasses(); | ||
|
||
/** | ||
* Return all the interfaces implemented directly by this class. | ||
* It does not include the interfaces implemented by superclasses or extended | ||
* by the interfaces implemented. | ||
*/ | ||
List<ResolvedReferenceType> getInterfaces(); | ||
|
||
/** | ||
* Return all the interfaces implemented by this class, either directly or indirectly, including the interfaces | ||
* extended by interfaces it implements. | ||
* <p> | ||
* Get all interfaces, with all the type typeParametersValues expressed as functions of the type | ||
* typeParametersValues of this declaration. | ||
*/ | ||
List<ResolvedReferenceType> getAllInterfaces(); | ||
|
||
/// | ||
/// Constructors | ||
/// | ||
|
||
/** | ||
* List of constructors available for the class. | ||
* This list should also include the default constructor. | ||
*/ | ||
@Override | ||
List<ResolvedConstructorDeclaration> getConstructors(); | ||
|
||
} |
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
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
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
129 changes: 129 additions & 0 deletions
129
...com/github/javaparser/symbolsolver/javaparsermodel/contexts/RecordDeclarationContext.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,129 @@ | ||
/* | ||
* Copyright (C) 2015-2016 Federico Tomassetti | ||
* Copyright (C) 2017-2020 The JavaParser Team. | ||
* | ||
* This file is part of JavaParser. | ||
* | ||
* JavaParser can be used either under the terms of | ||
* a) the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* b) the terms of the Apache License | ||
* | ||
* You should have received a copy of both licenses in LICENCE.LGPL and | ||
* LICENCE.APACHE. Please refer to those files for details. | ||
* | ||
* JavaParser is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
*/ | ||
|
||
package com.github.javaparser.symbolsolver.javaparsermodel.contexts; | ||
|
||
import com.github.javaparser.ast.Node; | ||
import com.github.javaparser.ast.body.RecordDeclaration; | ||
import com.github.javaparser.ast.type.TypeParameter; | ||
import com.github.javaparser.resolution.declarations.*; | ||
import com.github.javaparser.resolution.types.ResolvedType; | ||
import com.github.javaparser.resolution.types.ResolvedTypeVariable; | ||
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; | ||
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter; | ||
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; | ||
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; | ||
import com.github.javaparser.symbolsolver.model.resolution.Value; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* @author Federico Tomassetti | ||
* @author Roger Howell | ||
*/ | ||
public class RecordDeclarationContext extends AbstractJavaParserContext<RecordDeclaration> { | ||
|
||
private JavaParserTypeDeclarationAdapter javaParserTypeDeclarationAdapter; | ||
|
||
/// | ||
/// Constructors | ||
/// | ||
|
||
public RecordDeclarationContext(RecordDeclaration wrappedNode, TypeSolver typeSolver) { | ||
super(wrappedNode, typeSolver); | ||
this.javaParserTypeDeclarationAdapter = new JavaParserTypeDeclarationAdapter(wrappedNode, typeSolver, | ||
getDeclaration(), this); | ||
} | ||
|
||
/// | ||
/// Public methods | ||
/// | ||
|
||
@Override | ||
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name) { | ||
if (typeSolver == null) throw new IllegalArgumentException(); | ||
|
||
if (this.getDeclaration().hasVisibleField(name)) { | ||
return SymbolReference.solved(this.getDeclaration().getVisibleField(name)); | ||
} | ||
|
||
// then to parent | ||
return solveSymbolInParentContext(name); | ||
} | ||
|
||
@Override | ||
public Optional<Value> solveSymbolAsValue(String name) { | ||
if (typeSolver == null) throw new IllegalArgumentException(); | ||
|
||
if (this.getDeclaration().hasField(name)) { | ||
return Optional.of(Value.from(this.getDeclaration().getField(name))); | ||
} | ||
|
||
// then to parent | ||
return solveSymbolAsValueInParentContext(name); | ||
} | ||
|
||
@Override | ||
public Optional<ResolvedType> solveGenericType(String name) { | ||
// First check if the method-like declaration has type parameters defined. | ||
// For example: {@code public <T> boolean containsAll(Collection<T> c);} | ||
for (TypeParameter tp : wrappedNode.getTypeParameters()) { | ||
if (tp.getName().getId().equals(name)) { | ||
return Optional.of(new ResolvedTypeVariable(new JavaParserTypeParameter(tp, typeSolver))); | ||
} | ||
} | ||
|
||
// If no generic types on the method declaration, continue to solve as usual. | ||
return solveGenericTypeInParentContext(name); | ||
} | ||
|
||
@Override | ||
public SymbolReference<ResolvedTypeDeclaration> solveType(String name) { | ||
return javaParserTypeDeclarationAdapter.solveType(name); | ||
} | ||
|
||
@Override | ||
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) { | ||
return javaParserTypeDeclarationAdapter.solveMethod(name, argumentsTypes, staticOnly); | ||
} | ||
|
||
public SymbolReference<ResolvedConstructorDeclaration> solveConstructor(List<ResolvedType> argumentsTypes) { | ||
return javaParserTypeDeclarationAdapter.solveConstructor(argumentsTypes); | ||
} | ||
|
||
@Override | ||
public List<ResolvedFieldDeclaration> fieldsExposedToChild(Node child) { | ||
List<ResolvedFieldDeclaration> fields = new LinkedList<>(); | ||
fields.addAll(this.wrappedNode.resolve().getDeclaredFields()); | ||
this.wrappedNode.getImplementedTypes().forEach(i -> fields.addAll(i.resolve().asReferenceType().getAllFieldsVisibleToInheritors())); | ||
return fields; | ||
} | ||
|
||
/// | ||
/// Private methods | ||
/// | ||
|
||
private ResolvedReferenceTypeDeclaration getDeclaration() { | ||
return JavaParserFacade.get(typeSolver).getTypeDeclaration(this.wrappedNode); | ||
} | ||
} |
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
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.
I'm not sure that this commit really does anything.
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.
It doesn't do anything other than what's already in this PR. I found it easier to add these changes to a new branch manually instead of resolving conflicts, but also set MysterAitch as the author of that commit to avoid stealing his work.
I've been working on the https://github.com/johannescoetzee/javaparser/tree/johannes/record-resolution branch, although the changes there aren't the latest version I have