Skip to content

Commit

Permalink
Add implementations for JavacMemberValuePairBinding
Browse files Browse the repository at this point in the history
Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 authored and akurtakov committed Aug 16, 2024
1 parent f0b686f commit 7cebb04
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Optional;
import java.util.Queue;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.javac.dom.JavacAnnotationBinding;
Expand All @@ -24,6 +25,7 @@
import org.eclipse.jdt.internal.javac.dom.JavacTypeBinding;
import org.eclipse.jdt.internal.javac.dom.JavacVariableBinding;

import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
Expand Down Expand Up @@ -329,4 +331,39 @@ private java.util.List<TypeSymbol> getTypeArguments(final MethodInvocation metho
.collect(Collectors.toList());
}

/**
* Returns the constant value or the binding that a Javac attribute represents.
*
* See a detailed explanation of the returned value: {@link org.eclipse.jdt.core.dom.IMethodBinding#getDefaultValue()}
*
* @param attribute the javac attribute
* @return the constant value or the binding that a Javac attribute represents
*/
public Object getValueFromAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
if (attribute instanceof Attribute.Constant constant) {
return constant.value;
} else if (attribute instanceof Attribute.Class clazz) {
return new JavacTypeBinding(clazz.classType.tsym, this, null);
} else if (attribute instanceof Attribute.Enum enumm) {
return new JavacVariableBinding(enumm.value, this);
} else if (attribute instanceof Attribute.Array array) {
return Stream.of(array.values) //
.map(nestedAttr -> {
if (attribute instanceof Attribute.Constant constant) {
return constant.value;
} else if (attribute instanceof Attribute.Class clazz) {
return new JavacTypeBinding(clazz.classType.tsym, this, null);
} else if (attribute instanceof Attribute.Enum enumerable) {
return new JavacVariableBinding(enumerable.value, this);
}
throw new IllegalArgumentException("Unexpected attribute type: " + nestedAttr.getClass().getCanonicalName());
}) //
.toArray(Object[]::new);
}
throw new IllegalArgumentException("Unexpected attribute type: " + attribute.getClass().getCanonicalName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ public class JavacMemberValuePairBinding implements IMemberValuePairBinding {

public final JavacMethodBinding method;
public final Attribute value;
private final JavacBindingResolver resolver;

public JavacMemberValuePairBinding(MethodSymbol key, Attribute value, JavacBindingResolver resolver) {
this.method = new JavacMethodBinding(key, resolver, null);
this.value = value;
this.resolver = resolver;
}

@Override
Expand All @@ -54,8 +56,7 @@ public boolean isDeprecated() {

@Override
public boolean isRecovered() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'isRecovered'");
return this.value instanceof Attribute.Error;
}

@Override
Expand All @@ -70,8 +71,9 @@ public IJavaElement getJavaElement() {

@Override
public String getKey() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getKey'");
// as of writing, not yet implemented for ECJ
// @see org.eclipse.jdt.core.dom.MemberValuePairBinding.getKey
return null;
}

@Override
Expand All @@ -92,13 +94,12 @@ public IMethodBinding getMethodBinding() {

@Override
public Object getValue() {
throw new UnsupportedOperationException("Unimplemented method 'getValue'");
return this.resolver.getValueFromAttribute(this.value);
}

@Override
public boolean isDefault() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'isDefault'");
return this.value == this.method.methodSymbol.defaultValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -210,28 +210,7 @@ public IBinding getDeclaringMember() {

@Override
public Object getDefaultValue() {
Attribute attribute = this.methodSymbol.defaultValue;
if (attribute instanceof Attribute.Constant constant) {
return constant.value;
} else if (attribute instanceof Attribute.Class clazz) {
return new JavacTypeBinding(clazz.classType.tsym, this.resolver, null);
} else if (attribute instanceof Attribute.Enum enumm) {
return new JavacVariableBinding(enumm.value, this.resolver);
} else if (attribute instanceof Attribute.Array array) {
return Stream.of(array.values) //
.map(nestedAttr -> {
if (attribute instanceof Attribute.Constant constant) {
return constant.value;
} else if (attribute instanceof Attribute.Class clazz) {
return new JavacTypeBinding(clazz.classType.tsym, this.resolver, null);
} else if (attribute instanceof Attribute.Enum enumerable) {
return new JavacVariableBinding(enumerable.value, this.resolver);
}
throw new IllegalArgumentException("Unexpected attribute type: " + nestedAttr.getClass().getCanonicalName());
}) //
.toArray(Object[]::new);
}
throw new IllegalArgumentException("Unexpected attribute type: " + attribute.getClass().getCanonicalName());
return this.resolver.getValueFromAttribute(this.methodSymbol.defaultValue);
}

@Override
Expand Down

0 comments on commit 7cebb04

Please sign in to comment.