From 90ef955a6d0343e022cf34736c63e4ec80b0c0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Mon, 5 May 2014 22:40:11 +0300 Subject: [PATCH] Use SimpleTypeVisitorAdapter to convert IntersectionType instances --- .../mysema/query/apt/ExtendedTypeFactory.java | 14 +---- .../query/apt/SimpleTypeVisitorAdapter.java | 56 +++++++++++++++++++ .../com/mysema/query/apt/TypeExtractor.java | 9 +-- 3 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 querydsl-apt/src/main/java/com/mysema/query/apt/SimpleTypeVisitorAdapter.java diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/ExtendedTypeFactory.java b/querydsl-apt/src/main/java/com/mysema/query/apt/ExtendedTypeFactory.java index 901017e54..0e7153d65 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/ExtendedTypeFactory.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/ExtendedTypeFactory.java @@ -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 visitor = new SimpleTypeVisitor6() { + private final TypeVisitor visitor = new SimpleTypeVisitorAdapter() { @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, Boolean> keyBuilder = new SimpleTypeVisitor6, Boolean>() { + private final TypeVisitor, Boolean> keyBuilder = new SimpleTypeVisitorAdapter, Boolean>() { private final List defaultValue = Collections.singletonList("Object"); @@ -250,10 +244,6 @@ public final class ExtendedTypeFactory { return defaultValue; } - public List visitUnknown(TypeMirror t, Boolean p) { - return visitBase(t); - } - }; public ExtendedTypeFactory( diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/SimpleTypeVisitorAdapter.java b/querydsl-apt/src/main/java/com/mysema/query/apt/SimpleTypeVisitorAdapter.java new file mode 100644 index 000000000..b78cecee7 --- /dev/null +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/SimpleTypeVisitorAdapter.java @@ -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 + * @param

+ */ +public class SimpleTypeVisitorAdapter extends SimpleTypeVisitor6 { + + 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 bounds = (List) 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); + } + } + +} diff --git a/querydsl-apt/src/main/java/com/mysema/query/apt/TypeExtractor.java b/querydsl-apt/src/main/java/com/mysema/query/apt/TypeExtractor.java index 66fc2c384..1c0d4e86d 100644 --- a/querydsl-apt/src/main/java/com/mysema/query/apt/TypeExtractor.java +++ b/querydsl-apt/src/main/java/com/mysema/query/apt/TypeExtractor.java @@ -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 { +public class TypeExtractor extends SimpleTypeVisitorAdapter { private final boolean skipEnum; @@ -112,10 +111,4 @@ public class TypeExtractor extends SimpleTypeVisitor6 { return null; } - @Override - public TypeElement visitUnknown(TypeMirror t, Void p) { - return null; - } - - }