Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
* Add some toString to bindings
* Some support for method receiver type
* Some extra JLS version checks
* Improve support for parameterized types and wildcards
* add JavacBindingResolver.resolveAnnotation()
* Resolve lambda to methodBindings
  • Loading branch information
mickaelistria committed Jun 3, 2024
1 parent 1315ae5 commit ba7ec0b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCAnnotatedType;
import com.sun.tools.javac.tree.JCTree.JCAnnotation;
import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCLambda;
import com.sun.tools.javac.tree.JCTree.JCMemberReference;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
Expand Down Expand Up @@ -382,6 +384,19 @@ IMethodBinding resolveMethod(MethodDeclaration method) {
return null;
}

@Override
IMethodBinding resolveMethod(LambdaExpression lambda) {
resolve();
JCTree javacElement = this.converter.domToJavac.get(lambda);
if (javacElement instanceof JCLambda jcLambda) {
JavacTypeBinding typeBinding = this.bindings.getTypeBinding(jcLambda.type);
if (typeBinding != null && typeBinding.getDeclaredMethods().length == 1) {
return typeBinding.getDeclaredMethods()[0];
}
}
return null;
}

@Override
IMethodBinding resolveMethod(MethodReference methodReference) {
resolve();
Expand Down Expand Up @@ -725,4 +740,14 @@ ITypeBinding resolveWellKnownType(String typeName) {
}
return this.bindings.getTypeBinding(type);
}

@Override
IAnnotationBinding resolveAnnotation(Annotation annotation) {
resolve();
var javac = this.converter.domToJavac.get(annotation);
if (javac instanceof JCAnnotation jcAnnotation) {
return this.bindings.getAnnotationBinding(jcAnnotation.attribute, null);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -576,15 +576,19 @@ private TypeParameter convert(JCTypeParameter typeParameter) {
int end = typeParameter.pos + typeParameter.getName().length();
simpleName.setSourceRange(start, end - start);
ret.setName(simpleName);
int annotationsStart = start;
List bounds = typeParameter.bounds;
Iterator i = bounds.iterator();
List<JCExpression> bounds = typeParameter.getBounds();
Iterator<JCExpression> i = bounds.iterator();
while(i.hasNext()) {
JCTree t = (JCTree)i.next();
Type type = convertToType(t);
ret.typeBounds().add(type);
end = typeParameter.getEndPosition(this.javacCompilationUnit.endPositions);
}
if (typeParameter.getAnnotations() != null) {
typeParameter.getAnnotations().stream()
.map(this::convert)
.forEach(ret.modifiers()::add);
}
// org.eclipse.jdt.internal.compiler.ast.Annotation[] annotations = typeParameter.annotations;
// if (annotations != null) {
// if (annotations[0] != null)
Expand Down Expand Up @@ -795,6 +799,20 @@ private MethodDeclaration convertMethodDecl(JCMethodDecl javac, ASTNode parent)
// Compact constructor does not show the parameters even though javac finds them
javac.getParameters().stream().map(this::convertVariableDeclaration).forEach(res.parameters()::add);
}
if (javac.getReceiverParameter() != null) {
Type receiverType = convertToType(javac.getReceiverParameter().getType());
if (receiverType instanceof AnnotatableType annotable) {
javac.getReceiverParameter().getModifiers().getAnnotations().stream() //
.map(this::convert)
.forEach(annotable.annotations()::add);
}
if (receiverType != null) {
res.setReceiverType(receiverType);
}
if (javac.getReceiverParameter().getNameExpression() instanceof JCFieldAccess qualifiedName) {
res.setReceiverQualifier((SimpleName)toName(qualifiedName.getExpression()));
}
}

if( javac.getTypeParameters() != null ) {
Iterator<JCTypeParameter> i = javac.getTypeParameters().iterator();
Expand Down Expand Up @@ -921,7 +939,10 @@ private VariableDeclaration convertVariableDeclaration(JCVariableDecl javac) {
// the array dimensions are part of the type
if (javac.getType() != null) {
if( !(javac.getType() instanceof JCErroneous)) {
res.setType(convertToType(javac.getType()));
Type type = convertToType(javac.getType());
if (type != null) {
res.setType(type);
}
}
}
}
Expand Down Expand Up @@ -1408,6 +1429,7 @@ private Expression convertExpressionImpl(JCExpression javac) {
}
if (javac instanceof JCLambda jcLambda) {
LambdaExpression res = this.ast.newLambdaExpression();
commonSettings(res, javac);
jcLambda.getParameters().stream()
.filter(JCVariableDecl.class::isInstance)
.map(JCVariableDecl.class::cast)
Expand Down Expand Up @@ -2296,10 +2318,12 @@ Type convertToType(JCTree javac) {
return res;
}
if (javac instanceof JCAnnotatedType jcAnnotatedType) {
boolean createNameQualifiedType = jcAnnotatedType.getAnnotations() != null && jcAnnotatedType.getAnnotations().size() > 0;
Type res = null;
if( createNameQualifiedType && this.ast.apiLevel >= AST.JLS8_INTERNAL) {
JCExpression jcpe = jcAnnotatedType.underlyingType;
JCExpression jcpe = jcAnnotatedType.getUnderlyingType();
if( jcAnnotatedType.getAnnotations() != null //
&& !jcAnnotatedType.getAnnotations().isEmpty() //
&& this.ast.apiLevel >= AST.JLS8_INTERNAL
&& !(jcpe instanceof JCWildcard)) {
if( jcpe instanceof JCFieldAccess jcfa2) {
if( jcfa2.selected instanceof JCAnnotatedType || jcfa2.selected instanceof JCTypeApply) {
QualifiedType nameQualifiedType = new QualifiedType(this.ast);
Expand All @@ -2326,7 +2350,7 @@ Type convertToType(JCTree javac) {
annotatableType.annotations().add(convert(annotation));
}
} else if (res instanceof ArrayType arrayType) {
if (!arrayType.dimensions().isEmpty()) {
if (this.ast.apiLevel() >= AST.JLS8 && !arrayType.dimensions().isEmpty()) {
for (JCAnnotation annotation : jcAnnotatedType.getAnnotations()) {
((Dimension)arrayType.dimensions().get(0)).annotations().add(convert(annotation));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*******************************************************************************/
package org.eclipse.jdt.internal.javac.dom;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.dom.IAnnotationBinding;
Expand Down Expand Up @@ -85,7 +87,9 @@ public IJavaElement getJavaElement() {
@Override
public String getKey() {
StringBuilder builder = new StringBuilder();
builder.append(this.recipient.getKey());
if (this.recipient != null) {
builder.append(this.recipient.getKey());
}
builder.append('@');
builder.append(this.getAnnotationType().getKey());
return builder.toString();
Expand Down Expand Up @@ -118,4 +122,8 @@ public String getName() {
return getAnnotationType().getName();
}

@Override
public String toString() {
return '@' + getName() + '(' + Arrays.stream(getAllMemberValuePairs()).map(IMemberValuePairBinding::toString).collect(Collectors.joining(",")) + ')';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,8 @@ public boolean isDefault() {
return this.value == this.method.methodSymbol.defaultValue;
}

@Override
public String toString() {
return getName() + " = " + getValue().toString(); //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
*******************************************************************************/
package org.eclipse.jdt.internal.javac.dom;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
Expand Down Expand Up @@ -401,4 +403,31 @@ public String[] getParameterNames() {
.toArray(String[]::new);
}

@Override
public String toString() {
return modifiersAsString() + getReturnType().getName() + ' ' + getName().toString() + '('
+ Arrays.stream(getParameterTypes()).map(ITypeBinding::getName).collect(Collectors.joining(","))
+ ") ";
}

protected String modifiersAsString() {
String res = "";
int modifiers = getModifiers();
if (Modifier.isPublic(modifiers)) {
res += "public ";
}
if (Modifier.isProtected(modifiers)) {
res += "protected ";
}
if (Modifier.isPrivate(modifiers)) {
res += "private ";
}
if (Modifier.isStatic(modifiers)) {
res += "static ";
}
if (Modifier.isAbstract(modifiers)) {
res += "abstract ";
}
return res;
}
}

0 comments on commit ba7ec0b

Please sign in to comment.