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

Initial implementation for resolving records #3606

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion javaparser-core-serialization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
</licenses>

<properties>
<java.version>1.8</java.version>
<build.timestamp>${maven.build.timestamp}</build.timestamp>
</properties>

Expand Down
1 change: 0 additions & 1 deletion javaparser-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
</licenses>

<properties>
<java.version>1.8</java.version>
<build.timestamp>${maven.build.timestamp}</build.timestamp>
</properties>

Expand Down
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();

}
Copy link
Collaborator

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.

Copy link
Contributor

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

Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
public interface ResolvedReferenceTypeDeclaration extends ResolvedTypeDeclaration,
ResolvedTypeParametrizable {

String JAVA_LANG_ENUM = java.lang.Enum.class.getCanonicalName();
String JAVA_LANG_OBJECT = java.lang.Object.class.getCanonicalName();

@Override
default ResolvedReferenceTypeDeclaration asReferenceType() {
return this;
Expand Down Expand Up @@ -347,7 +344,7 @@ default boolean isJavaLangObject() {
return this.isClass()
&& !isAnonymousClass()
&& hasName() // Consider anonymous classes
&& getQualifiedName().equals(JAVA_LANG_OBJECT);
&& getQualifiedName().equals(Object.class.getCanonicalName());
}

/**
Expand All @@ -356,7 +353,7 @@ && hasName() // Consider anonymous classes
*/
default boolean isJavaLangEnum() {
return this.isEnum()
&& getQualifiedName().equals(JAVA_LANG_ENUM);
&& getQualifiedName().equals(Enum.class.getCanonicalName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Set;

/**
* A declaration of a type. It could be a primitive type, an enum, a class, an interface
* A declaration of a type. It could be a primitive type, an enum, a class, an interface, a record,
* or a type variable.
* It cannot be an annotation or an array.
*
Expand Down Expand Up @@ -76,7 +76,7 @@ default boolean hasInternalType(String name) {

/**
* Is this the declaration of a class?
* Note that an Enum is not considered a Class in this case.
* Note that an Enum and Record is not considered a Class in this case.
*/
default boolean isClass() {
return false;
Expand All @@ -89,6 +89,13 @@ default boolean isInterface() {
return false;
}

/**
* Is this the declaration of a record?
*/
default boolean isRecord() {
return false;
}

/**
* Is this the declaration of an enum?
*/
Expand Down Expand Up @@ -157,6 +164,13 @@ default ResolvedInterfaceDeclaration asInterface() {
throw new UnsupportedOperationException(String.format("%s is not an interface", this));
}

/**
* Return this as a RecordDeclaration or throw UnsupportedOperationException.
*/
default ResolvedRecordDeclaration asRecord() {
throw new UnsupportedOperationException(String.format("%s is not a record", this));
}

/**
* Return this as a EnumDeclaration or throw UnsupportedOperationException.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
*/
public abstract class ResolvedReferenceType implements ResolvedType,
ResolvedTypeParametrized, ResolvedTypeParameterValueProvider {

protected static String JAVA_LANG_ENUM = java.lang.Enum.class.getCanonicalName();
protected static String JAVA_LANG_OBJECT = java.lang.Object.class.getCanonicalName();

//
// Fields
Expand Down Expand Up @@ -552,7 +549,7 @@ private static List<ResolvedType> deriveParams(ResolvedReferenceTypeDeclaration
public boolean isJavaLangObject() {
return this.isReferenceType()
&& hasName() // Consider anonymous classes
&& getQualifiedName().equals(JAVA_LANG_OBJECT);
&& getQualifiedName().equals(Object.class.getCanonicalName());
}

/**
Expand All @@ -562,7 +559,7 @@ && hasName() // Consider anonymous classes
public boolean isJavaLangEnum() {
return this.isReferenceType()
&& hasName() // Consider anonymous classes
&& getQualifiedName().equals(JAVA_LANG_ENUM);
&& getQualifiedName().equals(Enum.class.getCanonicalName());
}


Expand Down
1 change: 0 additions & 1 deletion javaparser-symbol-solver-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
</licenses>

<properties>
<java.version>1.8</java.version>
<build.timestamp>${maven.build.timestamp}</build.timestamp>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public <T> T resolveDeclaration(Node node, Class<T> resultClass) {
return resultClass.cast(resolved);
}
}
if (node instanceof RecordDeclaration) {
ResolvedReferenceTypeDeclaration resolved = JavaParserFactory.toTypeDeclaration(node, typeSolver);
if (resultClass.isInstance(resolved)) {
return resultClass.cast(resolved);
}
}
if (node instanceof EnumDeclaration) {
ResolvedReferenceTypeDeclaration resolved = JavaParserFactory.toTypeDeclaration(node, typeSolver);
if (resultClass.isInstance(resolved)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserRecordDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter;
import com.github.javaparser.symbolsolver.javaparsermodel.declarators.FieldSymbolDeclarator;
import com.github.javaparser.symbolsolver.javaparsermodel.declarators.NoSymbolDeclarator;
Expand Down Expand Up @@ -91,6 +92,8 @@ public static Context getContext(Node node, TypeSolver typeSolver) {
return new MethodReferenceExprContext((MethodReferenceExpr) node, typeSolver);
} else if (node instanceof EnumDeclaration) {
return new EnumDeclarationContext((EnumDeclaration) node, typeSolver);
} else if (node instanceof RecordDeclaration) {
return new RecordDeclarationContext((RecordDeclaration) node, typeSolver);
} else if (node instanceof FieldAccessExpr) {
return new FieldAccessContext((FieldAccessExpr) node, typeSolver);
} else if (node instanceof SwitchEntry) {
Expand Down Expand Up @@ -176,6 +179,9 @@ public static ResolvedReferenceTypeDeclaration toTypeDeclaration(Node node, Type
}
return new JavaParserClassDeclaration((ClassOrInterfaceDeclaration) node, typeSolver);
}
if (node instanceof RecordDeclaration) {
return new JavaParserRecordDeclaration((RecordDeclaration) node, typeSolver);
}
if (node instanceof TypeParameter) {
return new JavaParserTypeParameter((TypeParameter) node, typeSolver);
}
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ static String getClassName(String base, Node container) {
} else {
return b + "." + cn;
}
} else if (container instanceof com.github.javaparser.ast.body.RecordDeclaration) {
String b = getClassName(base, container.getParentNode().orElse(null));
String cn = ((com.github.javaparser.ast.body.RecordDeclaration) container).getName().getId();
if (b.isEmpty()) {
return cn;
} else {
return b + "." + cn;
}
} else if (container instanceof com.github.javaparser.ast.body.EnumDeclaration) {
String b = getClassName(base, container.getParentNode().orElse(null));
String cn = ((com.github.javaparser.ast.body.EnumDeclaration) container).getName().getId();
Expand Down
Loading