Skip to content

Commit

Permalink
#Issue eclipse-jdt#565 - initial creation of intersection type
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkrzywanski committed Jul 13, 2024
1 parent 6a3225b commit 90eafb0
Showing 1 changed file with 49 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.*;
import javax.lang.model.util.SimpleTypeVisitor6;
import javax.lang.model.util.SimpleTypeVisitor8;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -57,83 +55,52 @@ public static boolean status() {
}


private final TypeVisitor<List<String>, Boolean> keyBuilder = new SimpleTypeVisitor6<List<String>, Boolean>() {

private final List<String> defaultValue = Collections.singletonList("Object");


private static final Class<?> intersectionTypeClass;

private static final Method getBoundsMethod;

static {
Class<?> availableClass;
Method availableMethod;
try {
availableClass = Class.forName("javax.lang.model.type.IntersectionType");
availableMethod = availableClass.getMethod("getBounds");
} catch (Exception e) {
// Not using Java 8
availableClass = null;
availableMethod = null;
}
intersectionTypeClass = availableClass;
getBoundsMethod = availableMethod;
}


private List<String> visitBase(TypeMirror t) {
List<String> rv = new ArrayList<String>();
String name = t.toString();
if (name.contains("<")) {
name = name.substring(0, name.indexOf('<'));
}
rv.add(name);
return rv;
}

@Override
public List<String> visitDeclared(DeclaredType t, Boolean p) {
List<String> rv = visitBase(t);
for (TypeMirror arg : t.getTypeArguments()) {
if (p) {
rv.addAll(visit(arg, false));
} else {
rv.add(arg.toString());
}
}
return rv;
}

@Override
public List<String> visitTypeVariable(TypeVariable t, Boolean p) {
List<String> rv = visitBase(t);
if (t.getUpperBound() != null) {
rv.addAll(visit(t.getUpperBound(), p));
}
if (t.getLowerBound() != null) {
rv.addAll(visit(t.getLowerBound(), p));
}
return rv;
}

@Override
public List<String> visitUnknown(TypeMirror t, Boolean p) {
if (intersectionTypeClass != null && intersectionTypeClass.isInstance(t)) {
try {
List<TypeMirror> bounds = (List<TypeMirror>) getBoundsMethod.invoke(t);
return bounds.get(0).accept(this, p);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e.getMessage(), e);
}
} else {
return super.visitUnknown(t, p);
}
}

@Override
public List<String> visitNull(NullType t, Boolean p) {
return defaultValue;
}
};
private final TypeVisitor<List<String>, Boolean> keyBuilder = new SimpleTypeVisitor8<>() {
private final List<String> defaultValue = Collections.singletonList("Object");

private List<String> visitBase(TypeMirror t) {
List<String> rv = new ArrayList<>();
String name = t.toString();
if (name.contains("<")) {
name = name.substring(0, name.indexOf('<'));
}
rv.add(name);
return rv;
}

@Override
public List<String> visitDeclared(DeclaredType t, Boolean p) {
List<String> rv = visitBase(t);
for (TypeMirror arg : t.getTypeArguments()) {
if (p) {
rv.addAll(visit(arg, false));
} else {
rv.add(arg.toString());
}
}
return rv;
}

@Override
public List<String> visitTypeVariable(TypeVariable t, Boolean p) {
List<String> rv = visitBase(t);
if (t.getUpperBound() != null) {
rv.addAll(visit(t.getUpperBound(), p));
}
if (t.getLowerBound() != null) {
rv.addAll(visit(t.getLowerBound(), p));
}
return rv;
}

@Override
public List<String> visitIntersection(IntersectionType t, Boolean p) {
return t.getBounds().get(0).accept(this, p);
}

@Override
public List<String> visitNull(NullType t, Boolean p) {
return defaultValue;
}
};
}

0 comments on commit 90eafb0

Please sign in to comment.