Use SimpleTypeVisitorAdapter to convert IntersectionType instances

This commit is contained in:
Timo Westkämper 2014-05-05 22:40:11 +03:00
parent d720e70f22
commit 90ef955a6d
3 changed files with 59 additions and 20 deletions

View File

@ -19,7 +19,6 @@ import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.*;
import javax.lang.model.util.SimpleTypeVisitor6;
import java.lang.annotation.Annotation;
import java.util.*;
@ -53,7 +52,7 @@ public final class ExtendedTypeFactory {
private boolean doubleIndexEntities = true;
private final TypeVisitor<Type, Boolean> visitor = new SimpleTypeVisitor6<Type, Boolean>() {
private final TypeVisitor<Type, Boolean> visitor = new SimpleTypeVisitorAdapter<Type, Boolean>() {
@Override
public Type visitPrimitive(PrimitiveType primitiveType, Boolean p) {
@ -158,16 +157,11 @@ public final class ExtendedTypeFactory {
return defaultType;
}
@Override
public Type visitUnknown(TypeMirror t, Boolean p) {
return defaultType;
}
};
// TODO : return TypeMirror instead ?!?
private final TypeVisitor<List<String>, Boolean> keyBuilder = new SimpleTypeVisitor6<List<String>, Boolean>() {
private final TypeVisitor<List<String>, Boolean> keyBuilder = new SimpleTypeVisitorAdapter<List<String>, Boolean>() {
private final List<String> defaultValue = Collections.singletonList("Object");
@ -250,10 +244,6 @@ public final class ExtendedTypeFactory {
return defaultValue;
}
public List<String> visitUnknown(TypeMirror t, Boolean p) {
return visitBase(t);
}
};
public ExtendedTypeFactory(

View File

@ -0,0 +1,56 @@
/*
* Copyright 2014, Mysema Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.query.apt;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.SimpleTypeVisitor6;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
/**
* Converts Java 8 IntersectionType instances into their first bound when visiting
*
* @param <R>
* @param <P>
*/
public class SimpleTypeVisitorAdapter<R, P> extends SimpleTypeVisitor6<R, P> {
private static Class<?> IntersectionTypeClass;
private static Method getBoundsMethod;
static {
try {
IntersectionTypeClass = Class.forName("javax.lang.model.type.IntersectionType");
getBoundsMethod = IntersectionTypeClass.getMethod("getBounds");
} catch (Exception e) {}
}
public R visitUnknown(TypeMirror t, P 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 e) {
throw new RuntimeException(e.getMessage(), e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e.getMessage(), e);
}
} else {
return super.visitUnknown(t, p);
}
}
}

View File

@ -15,7 +15,6 @@ package com.mysema.query.apt;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.*;
import javax.lang.model.util.SimpleTypeVisitor6;
/**
* TypeExtractor is a visitor implementation which a concrete type given a general {@link TypeElement}
@ -23,7 +22,7 @@ import javax.lang.model.util.SimpleTypeVisitor6;
* @author tiwe
*
*/
public class TypeExtractor extends SimpleTypeVisitor6<TypeElement, Void> {
public class TypeExtractor extends SimpleTypeVisitorAdapter<TypeElement, Void> {
private final boolean skipEnum;
@ -112,10 +111,4 @@ public class TypeExtractor extends SimpleTypeVisitor6<TypeElement, Void> {
return null;
}
@Override
public TypeElement visitUnknown(TypeMirror t, Void p) {
return null;
}
}