This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit afa1d4a3e36a9d51b86bf57a0104aa7555f4a951 Author: Andrea Cosentino <anco...@gmail.com> AuthorDate: Mon Jun 17 15:23:02 2019 +0200 Fixed CS for Camel-maven-package-plugin --- .../camel/maven/packaging/EndpointDslMojo.java | 137 ++++++------- .../camel/maven/packaging/generics/ClassUtil.java | 88 ++++----- .../maven/packaging/generics/GenericsUtil.java | 214 +++++++++++---------- .../generics/OwbGenericArrayTypeImpl.java | 35 ++-- .../generics/OwbParametrizedTypeImpl.java | 49 +++-- .../packaging/generics/OwbTypeVariableImpl.java | 138 +++++-------- .../packaging/generics/OwbWildcardTypeImpl.java | 30 ++- 7 files changed, 308 insertions(+), 383 deletions(-) diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointDslMojo.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointDslMojo.java index 238ec4f..8d6e9c3 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointDslMojo.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/EndpointDslMojo.java @@ -124,7 +124,7 @@ public class EndpointDslMojo extends AbstractMojo { } Map<File, Supplier<String>> files = PackageHelper.findJsonFiles(buildDir, p -> p.isDirectory() || p.getName().endsWith(".json")).stream() - .collect(Collectors.toMap(Function.identity(), s -> cache(() -> loadJson(s)))); + .collect(Collectors.toMap(Function.identity(), s -> cache(() -> loadJson(s)))); executeComponent(files); } @@ -139,6 +139,7 @@ public class EndpointDslMojo extends AbstractMojo { private static <T> Supplier<T> cache(Supplier<T> supplier) { return new Supplier<T>() { T value; + @Override public T get() { if (value == null) { @@ -171,7 +172,8 @@ public class EndpointDslMojo extends AbstractMojo { Map<String, List<ComponentModel>> grModels = allModels.stream().collect(Collectors.groupingBy(ComponentModel::getJavaType)); for (String componentClass : grModels.keySet()) { List<ComponentModel> compModels = grModels.get(componentClass); - ComponentModel model = compModels.get(0); // They should be equivalent + ComponentModel model = compModels.get(0); // They should be + // equivalent List<String> aliases = compModels.stream().map(ComponentModel::getScheme).sorted().collect(Collectors.toList()); String overrideComponentName = null; @@ -214,9 +216,7 @@ public class EndpointDslMojo extends AbstractMojo { JavaClass producerClass = null; JavaClass advancedProducerClass = null; - if (!realEndpointClass.getAnnotation(UriEndpoint.class).producerOnly() - && !realEndpointClass.getAnnotation(UriEndpoint.class).consumerOnly()) - { + if (!realEndpointClass.getAnnotation(UriEndpoint.class).producerOnly() && !realEndpointClass.getAnnotation(UriEndpoint.class).consumerOnly()) { String consumerName = builderName.replace("Endpoint", "EndpointConsumer"); consumerClass = javaClass.addNestedType().setPublic().setClass(false); consumerClass.setName(consumerName); @@ -231,10 +231,10 @@ public class EndpointDslMojo extends AbstractMojo { generateDummyClass(advancedConsumerClass.getCanonicalName()); advancedConsumerClass.getJavaDoc().setText("Advanced builder for endpoint consumers for the " + model.getTitle() + " component."); - consumerClass.addMethod().setName("advanced").setReturnType(loadClass(advancedConsumerClass.getCanonicalName())) - .setDefault().setBody("return (Advanced" + consumerName + ") this;"); - advancedConsumerClass.addMethod().setName("basic").setReturnType(loadClass(consumerClass.getCanonicalName())) - .setDefault().setBody("return (" + consumerName + ") this;"); + consumerClass.addMethod().setName("advanced").setReturnType(loadClass(advancedConsumerClass.getCanonicalName())).setDefault() + .setBody("return (Advanced" + consumerName + ") this;"); + advancedConsumerClass.addMethod().setName("basic").setReturnType(loadClass(consumerClass.getCanonicalName())).setDefault() + .setBody("return (" + consumerName + ") this;"); } String producerName = builderName.replace("Endpoint", "EndpointProducer"); @@ -251,10 +251,10 @@ public class EndpointDslMojo extends AbstractMojo { generateDummyClass(advancedProducerClass.getCanonicalName()); advancedProducerClass.getJavaDoc().setText("Advanced builder for endpoint producers for the " + model.getTitle() + " component."); - producerClass.addMethod().setName("advanced").setReturnType(loadClass(advancedProducerClass.getCanonicalName())) - .setDefault().setBody("return (Advanced" + producerName + ") this;"); - advancedProducerClass.addMethod().setName("basic").setReturnType(loadClass(producerClass.getCanonicalName())) - .setDefault().setBody("return (" + producerName + ") this;"); + producerClass.addMethod().setName("advanced").setReturnType(loadClass(advancedProducerClass.getCanonicalName())).setDefault() + .setBody("return (Advanced" + producerName + ") this;"); + advancedProducerClass.addMethod().setName("basic").setReturnType(loadClass(producerClass.getCanonicalName())).setDefault() + .setBody("return (" + producerName + ") this;"); } } @@ -286,10 +286,9 @@ public class EndpointDslMojo extends AbstractMojo { generateDummyClass(advancedBuilderClass.getCanonicalName()); advancedBuilderClass.getJavaDoc().setText("Advanced builder for endpoint for the " + model.getTitle() + " component."); - builderClass.addMethod().setName("advanced").setReturnType(loadClass(advancedBuilderClass.getCanonicalName())) - .setDefault().setBody("return (Advanced" + builderName + ") this;"); - advancedBuilderClass.addMethod().setName("basic").setReturnType(loadClass(builderClass.getCanonicalName())) - .setDefault().setBody("return (" + builderName + ") this;"); + builderClass.addMethod().setName("advanced").setReturnType(loadClass(advancedBuilderClass.getCanonicalName())).setDefault() + .setBody("return (Advanced" + builderName + ") this;"); + advancedBuilderClass.addMethod().setName("basic").setReturnType(loadClass(builderClass.getCanonicalName())).setDefault().setBody("return (" + builderName + ") this;"); } generateDummyClass(packageName + ".T"); @@ -300,8 +299,7 @@ public class EndpointDslMojo extends AbstractMojo { } javaClass.getJavaDoc().setText(doc); - javaClass.addAnnotation(Generated.class.getName()) - .setStringValue("value", EndpointDslMojo.class.getName()); + javaClass.addAnnotation(Generated.class.getName()).setStringValue("value", EndpointDslMojo.class.getName()); for (EndpointOptionModel option : model.getEndpointOptions()) { @@ -346,11 +344,9 @@ public class EndpointDslMojo extends AbstractMojo { if (target == null) { continue; } - Method fluent = target.addMethod().setDefault().setName(option.getName()) - .setReturnType(new GenericType(loadClass(target.getCanonicalName()))) - .addParameter(isPrimitive(ogtype.toString()) ? ogtype : gtype, option.getName()) - .setBody("setProperty(\"" + option.getName() + "\", " + option.getName() + ");\n" + - "return this;\n"); + Method fluent = target.addMethod().setDefault().setName(option.getName()).setReturnType(new GenericType(loadClass(target.getCanonicalName()))) + .addParameter(isPrimitive(ogtype.toString()) ? ogtype : gtype, option.getName()) + .setBody("setProperty(\"" + option.getName() + "\", " + option.getName() + ");\n" + "return this;\n"); if ("true".equals(option.getDeprecated())) { fluent.addAnnotation(Deprecated.class); } @@ -359,19 +355,15 @@ public class EndpointDslMojo extends AbstractMojo { if (!desc.endsWith(".")) { desc += "."; } - desc += "\nThe option is a <code>" + ogtype.toString() - .replaceAll("<", "<") - .replaceAll(">", ">") + "</code> type."; + desc += "\nThe option is a <code>" + ogtype.toString().replaceAll("<", "<").replaceAll(">", ">") + "</code> type."; desc += "\n@group " + option.getGroup(); fluent.getJavaDoc().setFullText(desc); } if (ogtype.getRawClass() != String.class) { - fluent = target.addMethod().setDefault().setName(option.getName()) - .setReturnType(new GenericType(loadClass(target.getCanonicalName()))) - .addParameter(new GenericType(String.class), option.getName()) - .setBody("setProperty(\"" + option.getName() + "\", " + option.getName() + ");\n" + - "return this;\n"); + fluent = target.addMethod().setDefault().setName(option.getName()).setReturnType(new GenericType(loadClass(target.getCanonicalName()))) + .addParameter(new GenericType(String.class), option.getName()) + .setBody("setProperty(\"" + option.getName() + "\", " + option.getName() + ");\n" + "return this;\n"); if ("true".equals(option.getDeprecated())) { fluent.addAnnotation(Deprecated.class); } @@ -380,9 +372,7 @@ public class EndpointDslMojo extends AbstractMojo { if (!desc.endsWith(".")) { desc += "."; } - desc += "\nThe option will be converted to a <code>" + ogtype.toString() - .replaceAll("<", "<") - .replaceAll(">", ">") + "</code> type."; + desc += "\nThe option will be converted to a <code>" + ogtype.toString().replaceAll("<", "<").replaceAll(">", ">") + "</code> type."; desc += "\n@group " + option.getGroup(); fluent.getJavaDoc().setFullText(desc); } @@ -392,22 +382,12 @@ public class EndpointDslMojo extends AbstractMojo { javaClass.removeImport("T"); - Method method = javaClass.addMethod() - .setDefault() - .setName(methodName) - .addParameter(String.class, "path") - .setReturnType(new GenericType(loadClass(builderClass.getCanonicalName()))) - .setBody( - "class " + builderName + "Impl extends AbstractEndpointBuilder implements " + builderName + ", Advanced" + builderName + " {\n" + - " public " + builderName + "Impl(String path) {\n" + - " super(\"" + model.getScheme() + "\", path);\n" + - " }\n" + - "}\n" + - "return new " + builderName + "Impl(path);\n"); - method.getJavaDoc().setText( - (StringHelper.isEmpty(model.getDescription()) ? "" : model.getDescription() + " ") - + "Creates a builder to build endpoints for the " + model.getTitle() + " component."); - + Method method = javaClass.addMethod().setDefault().setName(methodName).addParameter(String.class, "path") + .setReturnType(new GenericType(loadClass(builderClass.getCanonicalName()))) + .setBody("class " + builderName + "Impl extends AbstractEndpointBuilder implements " + builderName + ", Advanced" + builderName + " {\n" + " public " + builderName + + "Impl(String path) {\n" + " super(\"" + model.getScheme() + "\", path);\n" + " }\n" + "}\n" + "return new " + builderName + "Impl(path);\n"); + method.getJavaDoc().setText((StringHelper.isEmpty(model.getDescription()) ? "" : model.getDescription() + " ") + "Creates a builder to build endpoints for the " + + model.getTitle() + " component."); String fileName = packageName.replaceAll("\\.", "\\/") + "/" + builderName + "Factory.java"; writeSourceIfChanged(javaClass, fileName, false); @@ -418,12 +398,12 @@ public class EndpointDslMojo extends AbstractMojo { String methodName = builderName.replace("EndpointBuilder", ""); methodName = methodName.substring(0, 1).toLowerCase() + methodName.substring(1); switch (type) { - case "org.apache.camel.component.rest.RestComponent": - return "restEndpoint"; - case "org.apache.camel.component.beanclass.ClassComponent": - return "classEndpoint"; - default: - return methodName; + case "org.apache.camel.component.rest.RestComponent": + return "restEndpoint"; + case "org.apache.camel.component.beanclass.ClassComponent": + return "classEndpoint"; + default: + return methodName; } } @@ -434,12 +414,12 @@ public class EndpointDslMojo extends AbstractMojo { // HACKS // switch (type) { - case "org.apache.camel.component.atmosphere.websocket.WebsocketComponent": - return "AtmosphereWebsocketEndpointBuilder"; - case "org.apache.camel.component.zookeepermaster.MasterComponent": - return "ZooKeeperMasterEndpointBuilder"; - default: - return name; + case "org.apache.camel.component.atmosphere.websocket.WebsocketComponent": + return "AtmosphereWebsocketEndpointBuilder"; + case "org.apache.camel.component.zookeepermaster.MasterComponent": + return "ZooKeeperMasterEndpointBuilder"; + default: + return name; } } @@ -449,14 +429,14 @@ public class EndpointDslMojo extends AbstractMojo { // HACKS // switch (type) { - case "org.apache.camel.component.disruptor.vm.DisruptorVmComponent": - return "org.apache.camel.component.disruptor.DisruptorEndpoint"; - case "org.apache.camel.component.etcd.EtcdComponent": - return "org.apache.camel.component.etcd.AbstractEtcdPollingEndpoint"; - case "org.apache.camel.websocket.jsr356.JSR356WebSocketComponent": - return "org.apache.camel.websocket.jsr356.JSR356Endpoint"; - default: - return endpointName; + case "org.apache.camel.component.disruptor.vm.DisruptorVmComponent": + return "org.apache.camel.component.disruptor.DisruptorEndpoint"; + case "org.apache.camel.component.etcd.EtcdComponent": + return "org.apache.camel.component.etcd.AbstractEtcdPollingEndpoint"; + case "org.apache.camel.websocket.jsr356.JSR356WebSocketComponent": + return "org.apache.camel.websocket.jsr356.JSR356Endpoint"; + default: + return endpointName; } } @@ -522,7 +502,6 @@ public class EndpointDslMojo extends AbstractMojo { return optionClass; } - private GenericType getType(JavaClass javaClass, Map<String, JavaClass> enumClasses, String enums, String type) { type = type.trim(); // Check if this is an array @@ -572,14 +551,11 @@ public class EndpointDslMojo extends AbstractMojo { } JavaClass enumClass = enumClasses.get(enumClassName); if (enumClass == null) { - enumClass = javaClass.addNestedType().setPackagePrivate() - .setName(enumClassName).setEnum(true); + enumClass = javaClass.addNestedType().setPackagePrivate().setName(enumClassName).setEnum(true); enumClass.getJavaDoc().setText("Proxy enum for <code>" + type + "</code> enum."); enumClasses.put(enumClassName, enumClass); for (Object value : loadClass(type).getEnumConstants()) { - enumClass.addValue(value.toString() - .replace('.', '_') - .replace('-', '_')); + enumClass.addValue(value.toString().replace('.', '_').replace('-', '_')); } } type = javaClass.getPackage() + "." + javaClass.getName() + "$" + enumClassName; @@ -604,9 +580,7 @@ public class EndpointDslMojo extends AbstractMojo { if (nextComma < 0) { params.add(string.substring(start)); return params.toArray(new String[0]); - } else if ((nextOpen < 0 || nextComma < nextOpen) - && (nextClose < 0 || nextComma < nextClose) - && opened == 0) { + } else if ((nextOpen < 0 || nextComma < nextOpen) && (nextClose < 0 || nextComma < nextClose) && opened == 0) { params.add(string.substring(start, nextComma)); start = cur = nextComma + 1; } else if (nextOpen < 0) { @@ -627,8 +601,7 @@ public class EndpointDslMojo extends AbstractMojo { } private boolean isCamelCoreType(String type) { - return type.startsWith("java.") - || type.matches("org\\.apache\\.camel\\.(spi\\.)?([A-Za-z]+)"); + return type.startsWith("java.") || type.matches("org\\.apache\\.camel\\.(spi\\.)?([A-Za-z]+)"); } private Class generateDummyClass(String clazzName) { diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/ClassUtil.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/ClassUtil.java index 7c1d0ea..341c4e1 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/ClassUtil.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/ClassUtil.java @@ -1,20 +1,18 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * 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. + * 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 org.apache.camel.maven.packaging.generics; @@ -28,7 +26,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; - /** * Utility classes with respect to the class operations. * @@ -62,10 +59,10 @@ public final class ClassUtil { } public static boolean isSame(Type type1, Type type2) { - if ((type1 instanceof Class) && ((Class<?>) type1).isPrimitive()) { + if ((type1 instanceof Class) && ((Class<?>)type1).isPrimitive()) { type1 = PRIMITIVE_TO_WRAPPERS_MAP.get(type1); } - if ((type2 instanceof Class) && ((Class<?>) type2).isPrimitive()) { + if ((type2 instanceof Class) && ((Class<?>)type2).isPrimitive()) { type2 = PRIMITIVE_TO_WRAPPERS_MAP.get(type2); } return type1 == type2; @@ -87,12 +84,10 @@ public final class ClassUtil { * @param type class or parametrized type * @return */ - public static Class<?> getClass(Type type) - { + public static Class<?> getClass(Type type) { return getClazz(type); } - /** * Returns true if type is an instance of <code>ParameterizedType</code> * else otherwise. @@ -100,46 +95,38 @@ public final class ClassUtil { * @param type type of the artifact * @return true if type is an instance of <code>ParameterizedType</code> */ - public static boolean isParametrizedType(Type type) - { + public static boolean isParametrizedType(Type type) { return type instanceof ParameterizedType; } - + /** - * Returns true if type is an instance of <code>WildcardType</code> - * else otherwise. + * Returns true if type is an instance of <code>WildcardType</code> else + * otherwise. * * @param type type of the artifact * @return true if type is an instance of <code>WildcardType</code> - */ - public static boolean isWildCardType(Type type) - { + */ + public static boolean isWildCardType(Type type) { return type instanceof WildcardType; } - /** - * Returns true if rhs is assignable type - * to the lhs, false otherwise. + * Returns true if rhs is assignable type to the lhs, false otherwise. * * @param lhs left hand side class * @param rhs right hand side class * @return true if rhs is assignable to lhs */ - public static boolean isClassAssignableFrom(Class<?> lhs, Class<?> rhs) - { - if(lhs.isPrimitive()) - { + public static boolean isClassAssignableFrom(Class<?> lhs, Class<?> rhs) { + if (lhs.isPrimitive()) { lhs = getPrimitiveWrapper(lhs); } - - if(rhs.isPrimitive()) - { + + if (rhs.isPrimitive()) { rhs = getPrimitiveWrapper(rhs); } - if (lhs.isAssignableFrom(rhs)) - { + if (lhs.isAssignableFrom(rhs)) { return true; } @@ -154,15 +141,15 @@ public final class ClassUtil { */ public static Class<?> getClazz(Type type) { if (type instanceof ParameterizedType) { - ParameterizedType pt = (ParameterizedType) type; - return (Class<?>) pt.getRawType(); + ParameterizedType pt = (ParameterizedType)type; + return (Class<?>)pt.getRawType(); } else if (type instanceof Class) { - return (Class<?>) type; + return (Class<?>)type; } else if (type instanceof GenericArrayType) { - GenericArrayType arrayType = (GenericArrayType) type; + GenericArrayType arrayType = (GenericArrayType)type; return Array.newInstance(getClazz(arrayType.getGenericComponentType()), 0).getClass(); } else if (type instanceof WildcardType) { - WildcardType wildcardType = (WildcardType) type; + WildcardType wildcardType = (WildcardType)type; Type[] bounds = wildcardType.getUpperBounds(); if (bounds.length > 1) { throw new IllegalArgumentException("Illegal use of wild card type with more than one upper bound: " + wildcardType); @@ -172,7 +159,7 @@ public final class ClassUtil { return getClass(bounds[0]); } } else if (type instanceof TypeVariable) { - TypeVariable<?> typeVariable = (TypeVariable<?>) type; + TypeVariable<?> typeVariable = (TypeVariable<?>)type; if (typeVariable.getBounds().length > 1) { throw new IllegalArgumentException("Illegal use of type variable with more than one bound: " + typeVariable); } else { @@ -188,7 +175,6 @@ public final class ClassUtil { } } - public static boolean isRawClassEquals(Type ipType, Type apiType) { Class ipClass = getRawPrimitiveType(ipType); Class apiClass = getRawPrimitiveType(apiType); @@ -203,14 +189,14 @@ public final class ClassUtil { private static Class getRawPrimitiveType(Type type) { if (type instanceof Class) { - if (((Class) type).isPrimitive()) { - return getPrimitiveWrapper((Class) type); + if (((Class)type).isPrimitive()) { + return getPrimitiveWrapper((Class)type); } - return (Class) type; + return (Class)type; } if (type instanceof ParameterizedType) { - return getRawPrimitiveType(((ParameterizedType) type).getRawType()); + return getRawPrimitiveType(((ParameterizedType)type).getRawType()); } return null; diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/GenericsUtil.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/GenericsUtil.java index ec68bfb..38e4828 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/GenericsUtil.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/GenericsUtil.java @@ -1,24 +1,21 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * 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. + * 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 org.apache.camel.maven.packaging.generics; - import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -40,12 +37,19 @@ import java.util.Set; * Utility classes for generic type operations. */ public final class GenericsUtil { + + /* + * Private constructor + */ + private GenericsUtil() { + } + public static boolean satisfiesDependency(boolean isDelegateOrEvent, boolean isProducer, Type injectionPointType, Type beanType) { if (beanType instanceof TypeVariable || beanType instanceof WildcardType || beanType instanceof GenericArrayType) { return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType); } else { - Type injectionPointRawType = injectionPointType instanceof ParameterizedType ? ((ParameterizedType) injectionPointType).getRawType() : injectionPointType; - Type beanRawType = beanType instanceof ParameterizedType ? ((ParameterizedType) beanType).getRawType() : beanType; + Type injectionPointRawType = injectionPointType instanceof ParameterizedType ? ((ParameterizedType)injectionPointType).getRawType() : injectionPointType; + Type beanRawType = beanType instanceof ParameterizedType ? ((ParameterizedType)beanType).getRawType() : beanType; if (ClassUtil.isSame(injectionPointRawType, beanRawType)) { return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType); @@ -59,13 +63,13 @@ public final class GenericsUtil { if (beanType instanceof TypeVariable || beanType instanceof WildcardType || beanType instanceof GenericArrayType) { return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, beanType); } else { - Type injectionPointRawType = injectionPointType instanceof ParameterizedType ? ((ParameterizedType) injectionPointType).getRawType() : injectionPointType; - Type beanRawType = beanType instanceof ParameterizedType ? ((ParameterizedType) beanType).getRawType() : beanType; + Type injectionPointRawType = injectionPointType instanceof ParameterizedType ? ((ParameterizedType)injectionPointType).getRawType() : injectionPointType; + Type beanRawType = beanType instanceof ParameterizedType ? ((ParameterizedType)beanType).getRawType() : beanType; if (ClassUtil.isSame(injectionPointRawType, beanRawType)) { return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointRawType, beanRawType); } else { - Class bean = (Class) beanType; + Class bean = (Class)beanType; if (bean.getSuperclass() != null && ClassUtil.isRawClassEquals(injectionPointType, bean.getSuperclass())) { return true; } @@ -91,16 +95,15 @@ public final class GenericsUtil { */ public static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, Type requiredType, Type beanType) { if (requiredType instanceof Class) { - return isAssignableFrom(isDelegateOrEvent, (Class<?>) requiredType, beanType); + return isAssignableFrom(isDelegateOrEvent, (Class<?>)requiredType, beanType); } else if (requiredType instanceof ParameterizedType) { - return isAssignableFrom(isDelegateOrEvent, isProducer, (ParameterizedType) requiredType, beanType); + return isAssignableFrom(isDelegateOrEvent, isProducer, (ParameterizedType)requiredType, beanType); } else if (requiredType instanceof TypeVariable) { - return isAssignableFrom(isDelegateOrEvent, (TypeVariable<?>) requiredType, beanType); + return isAssignableFrom(isDelegateOrEvent, (TypeVariable<?>)requiredType, beanType); } else if (requiredType instanceof GenericArrayType) { - return Class.class.isInstance(beanType) && Class.class.cast(beanType).isArray() - && isAssignableFrom(isDelegateOrEvent, (GenericArrayType) requiredType, beanType); + return Class.class.isInstance(beanType) && Class.class.cast(beanType).isArray() && isAssignableFrom(isDelegateOrEvent, (GenericArrayType)requiredType, beanType); } else if (requiredType instanceof WildcardType) { - return isAssignableFrom(isDelegateOrEvent, (WildcardType) requiredType, beanType); + return isAssignableFrom(isDelegateOrEvent, (WildcardType)requiredType, beanType); } else { throw new IllegalArgumentException("Unsupported type " + requiredType.getClass()); } @@ -108,15 +111,15 @@ public final class GenericsUtil { private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, Type beanType) { if (beanType instanceof Class) { - return isAssignableFrom(injectionPointType, (Class<?>) beanType); + return isAssignableFrom(injectionPointType, (Class<?>)beanType); } else if (beanType instanceof TypeVariable) { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (TypeVariable<?>) beanType); + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (TypeVariable<?>)beanType); } else if (beanType instanceof ParameterizedType) { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (ParameterizedType) beanType); + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (ParameterizedType)beanType); } else if (beanType instanceof GenericArrayType) { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (GenericArrayType) beanType); + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (GenericArrayType)beanType); } else if (beanType instanceof WildcardType) { - return isAssignableFrom(isDelegateOrEvent, (Type) injectionPointType, (WildcardType) beanType); + return isAssignableFrom(isDelegateOrEvent, (Type)injectionPointType, (WildcardType)beanType); } else { throw new IllegalArgumentException("Unsupported type " + injectionPointType.getClass()); } @@ -136,17 +139,20 @@ public final class GenericsUtil { } /** - * CDI Spec. 5.2.4: "A parameterized bean type is considered assignable to a raw required type - * if the raw generics are identical and all type parameters of the bean type are either unbounded type variables or java.lang.Object." + * CDI Spec. 5.2.4: "A parameterized bean type is considered assignable to a + * raw required type if the raw generics are identical and all type + * parameters of the bean type are either unbounded type variables or + * java.lang.Object." */ private static boolean isAssignableFrom(boolean isDelegateOrEvent, Class<?> injectionPointType, ParameterizedType beanType) { if (beanType.getRawType() != injectionPointType) { - return false; //raw generics don't match + return false; // raw generics don't match } if (isDelegateOrEvent) { // for delegate and events we match 'in reverse' kind off - // @Observes ProcessInjectionPoint<?, Instance> does also match Instance<SomeBean> + // @Observes ProcessInjectionPoint<?, Instance> does also match + // Instance<SomeBean> return isAssignableFrom(true, injectionPointType, beanType.getRawType()); } @@ -155,12 +161,12 @@ public final class GenericsUtil { continue; } if (!(typeArgument instanceof TypeVariable)) { - return false; //neither object nor type variable + return false; // neither object nor type variable } - TypeVariable<?> typeVariable = (TypeVariable<?>) typeArgument; + TypeVariable<?> typeVariable = (TypeVariable<?>)typeArgument; for (Type bounds : typeVariable.getBounds()) { if (bounds != Object.class) { - return false; //bound type variable + return false; // bound type variable } } } @@ -187,13 +193,13 @@ public final class GenericsUtil { private static boolean isAssignableFrom(boolean isDelegateOrEvent, boolean isProducer, ParameterizedType injectionPointType, Type beanType) { if (beanType instanceof Class) { - return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, (Class<?>) beanType); + return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, (Class<?>)beanType); } else if (beanType instanceof TypeVariable) { - return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, (TypeVariable<?>) beanType); + return isAssignableFrom(isDelegateOrEvent, isProducer, injectionPointType, (TypeVariable<?>)beanType); } else if (beanType instanceof ParameterizedType) { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (ParameterizedType) beanType); + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (ParameterizedType)beanType); } else if (beanType instanceof WildcardType) { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (WildcardType) beanType); + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (WildcardType)beanType); } else if (beanType instanceof GenericArrayType) { return false; } else { @@ -256,12 +262,12 @@ public final class GenericsUtil { Type injectionPointTypeArgument = injectionPointTypeArguments[i]; Type beanTypeArgument = beanTypeArguments[i]; - // for this special case it's actually an 'assignable to', thus we swap the params, see CDI-389 - // but this special rule does not apply to Delegate injection points... - if (swapParams && - (injectionPointTypeArgument instanceof Class || injectionPointTypeArgument instanceof TypeVariable) && - beanTypeArgument instanceof TypeVariable) { - final Type[] bounds = ((TypeVariable<?>) beanTypeArgument).getBounds(); + // for this special case it's actually an 'assignable to', thus we + // swap the params, see CDI-389 + // but this special rule does not apply to Delegate injection + // points... + if (swapParams && (injectionPointTypeArgument instanceof Class || injectionPointTypeArgument instanceof TypeVariable) && beanTypeArgument instanceof TypeVariable) { + final Type[] bounds = ((TypeVariable<?>)beanTypeArgument).getBounds(); final boolean isNotBound = isNotBound(bounds); if (!isNotBound) { for (final Type upperBound : bounds) { @@ -273,7 +279,8 @@ public final class GenericsUtil { } else if (swapParams && injectionPointTypeArgument instanceof TypeVariable) { return false; } else if (isDelegateOrEvent && injectionPointTypeArgument instanceof Class && beanTypeArgument instanceof Class) { - // if no wildcard type was given then we require a real exact match. + // if no wildcard type was given then we require a real exact + // match. return injectionPointTypeArgument.equals(beanTypeArgument); } else if (!isAssignableFrom(isDelegateOrEvent, false, injectionPointTypeArgument, beanTypeArgument)) { @@ -296,7 +303,8 @@ public final class GenericsUtil { return true; } - // rules are a bit different when in an array so we handle ParameterizedType manually (not reusing isAssignableFrom) + // rules are a bit different when in an array so we handle ParameterizedType + // manually (not reusing isAssignableFrom) private static boolean isAssignableFrom(boolean isDelegateOrEvent, GenericArrayType injectionPointType, Type beanType) { final Type genericComponentType = injectionPointType.getGenericComponentType(); final Class componentType = Class.class.cast(beanType).getComponentType(); @@ -311,7 +319,7 @@ public final class GenericsUtil { private static boolean isAssignableFrom(boolean isDelegateOrEvent, WildcardType injectionPointType, Type beanType) { if (beanType instanceof TypeVariable) { - return isAssignableFrom(isDelegateOrEvent, injectionPointType, (TypeVariable<?>) beanType); + return isAssignableFrom(isDelegateOrEvent, injectionPointType, (TypeVariable<?>)beanType); } for (Type bounds : injectionPointType.getLowerBounds()) { if (!isAssignableFrom(isDelegateOrEvent, false, beanType, bounds)) { @@ -322,10 +330,8 @@ public final class GenericsUtil { Set<Type> beanTypeClosure = getTypeClosure(beanType); boolean isAssignable = false; for (Type beanSupertype : beanTypeClosure) { - if (isAssignableFrom(isDelegateOrEvent, false, bounds, beanSupertype) - || (Class.class.isInstance(bounds) - && ParameterizedType.class.isInstance(beanSupertype) - && bounds == ParameterizedType.class.cast(beanSupertype).getRawType())) { + if (isAssignableFrom(isDelegateOrEvent, false, bounds, beanSupertype) || (Class.class.isInstance(bounds) && ParameterizedType.class.isInstance(beanSupertype) + && bounds == ParameterizedType.class.cast(beanSupertype).getRawType())) { isAssignable = true; break; } @@ -359,7 +365,8 @@ public final class GenericsUtil { } /** - * @return <tt>true</tt>, if the specified type declaration contains an unresolved type variable. + * @return <tt>true</tt>, if the specified type declaration contains an + * unresolved type variable. */ public static boolean containsTypeVariable(Type type) { if (type instanceof Class) { @@ -367,13 +374,13 @@ public final class GenericsUtil { } else if (type instanceof TypeVariable) { return true; } else if (type instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType) type; + ParameterizedType parameterizedType = (ParameterizedType)type; return containTypeVariable(parameterizedType.getActualTypeArguments()); } else if (type instanceof WildcardType) { - WildcardType wildcardType = (WildcardType) type; + WildcardType wildcardType = (WildcardType)type; return containTypeVariable(wildcardType.getUpperBounds()) || containTypeVariable(wildcardType.getLowerBounds()); } else if (type instanceof GenericArrayType) { - GenericArrayType arrayType = (GenericArrayType) type; + GenericArrayType arrayType = (GenericArrayType)type; return containsTypeVariable(arrayType.getGenericComponentType()); } else { throw new IllegalArgumentException("Unsupported type " + type.getClass().getName()); @@ -397,7 +404,7 @@ public final class GenericsUtil { /** * @param type to check * @return {@code true} if the given type contains a {@link WildcardType} - * {@code false} otherwise + * {@code false} otherwise */ public static boolean containsWildcardType(Type type) { if (!(type instanceof ParameterizedType)) { @@ -419,37 +426,41 @@ public final class GenericsUtil { return false; } - /** - * Resolves the actual type of the specified field for the type hierarchy specified by the given subclass + * Resolves the actual type of the specified field for the type hierarchy + * specified by the given subclass */ public static Type resolveType(Class<?> subclass, Field field) { return resolveType(field.getGenericType(), subclass, newSeenList()); } /** - * Resolves the actual return type of the specified method for the type hierarchy specified by the given subclass + * Resolves the actual return type of the specified method for the type + * hierarchy specified by the given subclass */ public static Type resolveReturnType(Class<?> subclass, Method method) { return resolveType(method.getGenericReturnType(), subclass, newSeenList()); } /** - * Resolves the actual parameter generics of the specified constructor for the type hierarchy specified by the given subclass + * Resolves the actual parameter generics of the specified constructor for + * the type hierarchy specified by the given subclass */ public static Type[] resolveParameterTypes(Class<?> subclass, Constructor<?> constructor) { return resolveTypes(constructor.getGenericParameterTypes(), subclass); } /** - * Resolves the actual parameter generics of the specified method for the type hierarchy specified by the given subclass + * Resolves the actual parameter generics of the specified method for the + * type hierarchy specified by the given subclass */ public static Type[] resolveParameterTypes(Class<?> subclass, Method method) { return resolveTypes(method.getGenericParameterTypes(), subclass); } /** - * Resolves the actual type of the specified type for the type hierarchy specified by the given subclass + * Resolves the actual type of the specified type for the type hierarchy + * specified by the given subclass */ public static Type resolveType(Type type, Class<?> subclass, Member member) { return resolveType(type, subclass, newSeenList()); @@ -463,13 +474,14 @@ public final class GenericsUtil { if (type instanceof Class) { return type; } else if (type instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType) type; + ParameterizedType parameterizedType = (ParameterizedType)type; Type[] resolvedTypeArguments; if (Enum.class.equals(parameterizedType.getRawType())) { - // Enums derive from themselves, which would create an infinite loop + // Enums derive from themselves, which would create an infinite + // loop // we directly escape the loop if we detect this. - resolvedTypeArguments = new Type[]{new OwbWildcardTypeImpl(new Type[]{Enum.class}, ClassUtil.NO_TYPES)}; + resolvedTypeArguments = new Type[] {new OwbWildcardTypeImpl(new Type[] {Enum.class}, ClassUtil.NO_TYPES)}; } else { resolvedTypeArguments = resolveTypes(parameterizedType.getActualTypeArguments(), actualType, seen); @@ -477,15 +489,15 @@ public final class GenericsUtil { return new OwbParametrizedTypeImpl(parameterizedType.getOwnerType(), parameterizedType.getRawType(), resolvedTypeArguments); } else if (type instanceof TypeVariable) { - TypeVariable<?> variable = (TypeVariable<?>) type; + TypeVariable<?> variable = (TypeVariable<?>)type; return resolveTypeVariable(variable, actualType, seen); } else if (type instanceof WildcardType) { - WildcardType wildcardType = (WildcardType) type; + WildcardType wildcardType = (WildcardType)type; Type[] upperBounds = resolveTypes(wildcardType.getUpperBounds(), actualType, seen); Type[] lowerBounds = resolveTypes(wildcardType.getLowerBounds(), actualType, seen); return new OwbWildcardTypeImpl(upperBounds, lowerBounds); } else if (type instanceof GenericArrayType) { - GenericArrayType arrayType = (GenericArrayType) type; + GenericArrayType arrayType = (GenericArrayType)type; return createArrayType(resolveType(arrayType.getGenericComponentType(), actualType, seen)); } else { throw new IllegalArgumentException("Unsupported type " + type.getClass().getName()); @@ -496,8 +508,8 @@ public final class GenericsUtil { Type[] resolvedTypeArguments = new Type[types.length]; for (int i = 0; i < types.length; i++) { final Type type = resolveType(types[i], actualType, seen); - if (type != null) // means a stackoverflow was avoided, just keep what we have - { + if (type != null) { // means a stackoverflow was avoided, just keep + // what we have resolvedTypeArguments[i] = type; } } @@ -534,7 +546,8 @@ public final class GenericsUtil { * } * </code> * <p> - * To get the type closure of T in the context of Bar (which is {Number.class, Object.class}), you have to call this method like + * To get the type closure of T in the context of Bar (which is + * {Number.class, Object.class}), you have to call this method like * </p> * <code> * GenericUtil.getTypeClosure(Foo.class.getDeclaredField("t").getType(), Bar.class, Foo.class); @@ -551,14 +564,15 @@ public final class GenericsUtil { * } * </code> * <p> - * To get the type closure of Bar<T> in the context of Foo<Number> (which are besides Object.class the <tt>ParameterizedType</tt>s Bar<Number> and Foo<Number>), - * you have to call this method like + * To get the type closure of Bar<T> in the context of Foo<Number> (which + * are besides Object.class the <tt>ParameterizedType</tt>s Bar<Number> and + * Foo<Number>), you have to call this method like * </p> * <code> * GenericUtil.getTypeClosure(Foo.class, new TypeLiteral<Foo<Number>>() {}.getType(), Bar.class); * </code> * - * @param type the type to get the closure for + * @param type the type to get the closure for * @param actualType the context to bind type variables * @return the type closure */ @@ -602,7 +616,7 @@ public final class GenericsUtil { public static boolean hasTypeParameters(Type type) { if (type instanceof Class) { - Class<?> classType = (Class<?>) type; + Class<?> classType = (Class<?>)type; return classType.getTypeParameters().length > 0; } return false; @@ -610,9 +624,9 @@ public final class GenericsUtil { public static ParameterizedType getParameterizedType(Type type) { if (type instanceof ParameterizedType) { - return (ParameterizedType) type; + return (ParameterizedType)type; } else if (type instanceof Class) { - Class<?> classType = (Class<?>) type; + Class<?> classType = (Class<?>)type; return new OwbParametrizedTypeImpl(classType.getDeclaringClass(), classType, classType.getTypeParameters()); } else { throw new IllegalArgumentException(type.getClass().getSimpleName() + " is not supported"); @@ -625,20 +639,20 @@ public final class GenericsUtil { static <T> Class<T> getRawType(Type type, Type actualType) { if (type instanceof Class) { - return (Class<T>) type; + return (Class<T>)type; } else if (type instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType) type; + ParameterizedType parameterizedType = (ParameterizedType)type; return getRawType(parameterizedType.getRawType(), actualType); } else if (type instanceof TypeVariable) { - TypeVariable<?> typeVariable = (TypeVariable<?>) type; + TypeVariable<?> typeVariable = (TypeVariable<?>)type; Type mostSpecificType = getMostSpecificType(getRawTypes(typeVariable.getBounds(), actualType), typeVariable.getBounds()); return getRawType(mostSpecificType, actualType); } else if (type instanceof WildcardType) { - WildcardType wildcardType = (WildcardType) type; + WildcardType wildcardType = (WildcardType)type; Type mostSpecificType = getMostSpecificType(getRawTypes(wildcardType.getUpperBounds(), actualType), wildcardType.getUpperBounds()); return getRawType(mostSpecificType, actualType); } else if (type instanceof GenericArrayType) { - GenericArrayType arrayType = (GenericArrayType) type; + GenericArrayType arrayType = (GenericArrayType)type; return getRawType(createArrayType(getRawType(arrayType.getGenericComponentType(), actualType)), actualType); } else { throw new IllegalArgumentException("Unsupported type " + type.getClass().getName()); @@ -692,14 +706,13 @@ public final class GenericsUtil { Type[] typeArguments = resolveTypeArguments(directSubclass, actualType); Type directSubtype = new OwbParametrizedTypeImpl(directSubclass.getDeclaringClass(), directSubclass, typeArguments); return resolveTypeVariable(variable, directSubtype, seen); - } else // if (declaringClass.isAssignableFrom(actualClass)) - { + } else { // if (declaringClass.isAssignableFrom(actualClass)) Type genericSuperclass = getGenericSuperclass(actualClass, declaringClass); if (genericSuperclass == null) { return variable; } else if (genericSuperclass instanceof Class) { // special handling for type erasure - Class<?> superclass = (Class<?>) genericSuperclass; + Class<?> superclass = (Class<?>)genericSuperclass; genericSuperclass = new OwbParametrizedTypeImpl(superclass.getDeclaringClass(), superclass, getRawTypes(superclass.getTypeParameters())); } else { ParameterizedType genericSupertype = getParameterizedType(genericSuperclass); @@ -708,7 +721,7 @@ public final class GenericsUtil { } Type resolvedType = resolveTypeVariable(variable, genericSuperclass, seen); if (resolvedType instanceof TypeVariable) { - TypeVariable<?> resolvedTypeVariable = (TypeVariable<?>) resolvedType; + TypeVariable<?> resolvedTypeVariable = (TypeVariable<?>)resolvedType; TypeVariable<?>[] typeParameters = actualClass.getTypeParameters(); for (int i = 0; i < typeParameters.length; i++) { if (typeParameters[i].getName().equals(resolvedTypeVariable.getName())) { @@ -723,16 +736,15 @@ public final class GenericsUtil { private static Class<?> getDeclaringClass(GenericDeclaration declaration) { if (declaration instanceof Class) { - return (Class<?>) declaration; + return (Class<?>)declaration; } else if (declaration instanceof Member) { - return ((Member) declaration).getDeclaringClass(); + return ((Member)declaration).getDeclaringClass(); } else { throw new IllegalArgumentException("Unsupported type " + declaration.getClass()); } } - private static Type resolveTypeVariable(TypeVariable<?> variable, GenericDeclaration declaration, ParameterizedType type, - Collection<TypeVariable<?>> seen) { + private static Type resolveTypeVariable(TypeVariable<?> variable, GenericDeclaration declaration, ParameterizedType type, Collection<TypeVariable<?>> seen) { int index = getIndex(declaration, variable); if (declaration instanceof Class) { if (index >= 0) { @@ -759,7 +771,7 @@ public final class GenericsUtil { Type[] typeParameters = declaration.getTypeParameters(); for (int i = 0; i < typeParameters.length; i++) { if (typeParameters[i] instanceof TypeVariable) { - TypeVariable<?> variableArgument = (TypeVariable<?>) typeParameters[i]; + TypeVariable<?> variableArgument = (TypeVariable<?>)typeParameters[i]; if (variableArgument.getName().equals(variable.getName())) { return i; } @@ -772,7 +784,7 @@ public final class GenericsUtil { Type[] actualTypeArguments = type.getActualTypeArguments(); for (int i = 0; i < actualTypeArguments.length; i++) { if (actualTypeArguments[i] instanceof TypeVariable) { - TypeVariable<?> variableArgument = (TypeVariable<?>) actualTypeArguments[i]; + TypeVariable<?> variableArgument = (TypeVariable<?>)actualTypeArguments[i]; if (variableArgument.getName().equals(variable.getName())) { return i; } @@ -819,7 +831,7 @@ public final class GenericsUtil { private static Type[] resolveTypeArguments(Class<?> subclass, Type supertype) { if (supertype instanceof ParameterizedType) { - ParameterizedType parameterizedSupertype = (ParameterizedType) supertype; + ParameterizedType parameterizedSupertype = (ParameterizedType)supertype; return resolveTypeArguments(subclass, parameterizedSupertype); } else { return subclass.getTypeParameters(); @@ -831,7 +843,7 @@ public final class GenericsUtil { if (!(genericSuperclass instanceof ParameterizedType)) { return subclass.getTypeParameters(); } - ParameterizedType parameterizedSuperclass = (ParameterizedType) genericSuperclass; + ParameterizedType parameterizedSuperclass = (ParameterizedType)genericSuperclass; Type[] typeParameters = subclass.getTypeParameters(); Type[] actualTypeArguments = parameterizedSupertype.getActualTypeArguments(); return resolveTypeArguments(parameterizedSuperclass, typeParameters, actualTypeArguments); @@ -851,7 +863,7 @@ public final class GenericsUtil { private static Type resolveTypeArgument(ParameterizedType parameterizedType, Type typeParameter, Type[] actualTypeArguments) { if (typeParameter instanceof TypeVariable) { - TypeVariable<?> variable = (TypeVariable<?>) typeParameter; + TypeVariable<?> variable = (TypeVariable<?>)typeParameter; int index = getIndex(parameterizedType, variable); if (index == -1) { return typeParameter; @@ -859,7 +871,7 @@ public final class GenericsUtil { return actualTypeArguments[index]; } } else if (typeParameter instanceof GenericArrayType) { - GenericArrayType array = (GenericArrayType) typeParameter; + GenericArrayType array = (GenericArrayType)typeParameter; return createArrayType(resolveTypeArgument(parameterizedType, array.getGenericComponentType(), actualTypeArguments)); } else { return typeParameter; @@ -868,7 +880,7 @@ public final class GenericsUtil { private static Type createArrayType(Type componentType) { if (componentType instanceof Class) { - return Array.newInstance((Class<?>) componentType, 0).getClass(); + return Array.newInstance((Class<?>)componentType, 0).getClass(); } else { return new OwbGenericArrayTypeImpl(componentType); } diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbGenericArrayTypeImpl.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbGenericArrayTypeImpl.java index 2b3722b..59c24e0 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbGenericArrayTypeImpl.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbGenericArrayTypeImpl.java @@ -1,27 +1,24 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * 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. + * 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 org.apache.camel.maven.packaging.generics; import java.lang.reflect.GenericArrayType; import java.lang.reflect.Type; - public class OwbGenericArrayTypeImpl implements GenericArrayType { private Type componentType; @@ -35,7 +32,8 @@ public class OwbGenericArrayTypeImpl implements GenericArrayType { return componentType; } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override @@ -43,7 +41,8 @@ public class OwbGenericArrayTypeImpl implements GenericArrayType { return componentType.hashCode(); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override @@ -51,7 +50,7 @@ public class OwbGenericArrayTypeImpl implements GenericArrayType { if (this == obj) { return true; } else if (obj instanceof GenericArrayType) { - return ((GenericArrayType) obj).getGenericComponentType().equals(componentType); + return ((GenericArrayType)obj).getGenericComponentType().equals(componentType); } else { return false; } diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbParametrizedTypeImpl.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbParametrizedTypeImpl.java index 2cf3a95..555d094 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbParametrizedTypeImpl.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbParametrizedTypeImpl.java @@ -1,20 +1,18 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * 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. + * 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 org.apache.camel.maven.packaging.generics; @@ -25,7 +23,8 @@ import java.util.Arrays; /** * Custom parametrized type implementation. * - * @version $Rev: 1621935 $ $Date: 2014-09-02 09:07:32 +0200 (Tue, 02 Sep 2014) $ + * @version $Rev: 1621935 $ $Date: 2014-09-02 09:07:32 +0200 (Tue, 02 Sep 2014) + * $ */ public class OwbParametrizedTypeImpl implements ParameterizedType { /** @@ -47,7 +46,7 @@ public class OwbParametrizedTypeImpl implements ParameterizedType { * New instance. * * @param owner owner - * @param raw raw + * @param raw raw */ public OwbParametrizedTypeImpl(Type owner, Type raw, Type... types) { this.owner = owner; @@ -70,8 +69,8 @@ public class OwbParametrizedTypeImpl implements ParameterizedType { return rawType; } - - /* (non-Javadoc) + /* + * (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override @@ -79,7 +78,8 @@ public class OwbParametrizedTypeImpl implements ParameterizedType { return Arrays.hashCode(types) ^ (owner == null ? 0 : owner.hashCode()) ^ (rawType == null ? 0 : rawType.hashCode()); } - /* (non-Javadoc) + /* + * (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override @@ -87,12 +87,11 @@ public class OwbParametrizedTypeImpl implements ParameterizedType { if (this == obj) { return true; } else if (obj instanceof ParameterizedType) { - ParameterizedType that = (ParameterizedType) obj; + ParameterizedType that = (ParameterizedType)obj; Type thatOwnerType = that.getOwnerType(); Type thatRawType = that.getRawType(); - return (owner == null ? thatOwnerType == null : owner.equals(thatOwnerType)) - && (rawType == null ? thatRawType == null : rawType.equals(thatRawType)) - && Arrays.equals(types, that.getActualTypeArguments()); + return (owner == null ? thatOwnerType == null : owner.equals(thatOwnerType)) && (rawType == null ? thatRawType == null : rawType.equals(thatRawType)) + && Arrays.equals(types, that.getActualTypeArguments()); } else { return false; } @@ -101,14 +100,14 @@ public class OwbParametrizedTypeImpl implements ParameterizedType { public String toString() { StringBuilder buffer = new StringBuilder(); - buffer.append(((Class<?>) rawType).getName()); + buffer.append(((Class<?>)rawType).getName()); Type[] actualTypes = getActualTypeArguments(); if (actualTypes.length > 0) { buffer.append("<"); int length = actualTypes.length; for (int i = 0; i < length; i++) { if (actualTypes[i] instanceof Class) { - buffer.append(((Class<?>) actualTypes[i]).getSimpleName()); + buffer.append(((Class<?>)actualTypes[i]).getSimpleName()); } else { buffer.append(actualTypes[i].toString()); } diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbTypeVariableImpl.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbTypeVariableImpl.java index 7abd222..f3c73eb 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbTypeVariableImpl.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbTypeVariableImpl.java @@ -1,20 +1,18 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * 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. + * 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 org.apache.camel.maven.packaging.generics; @@ -26,150 +24,110 @@ import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.Arrays; - -public class OwbTypeVariableImpl -{ - private static final Class<?>[] TYPE_VARIABLE_TYPES = new Class<?>[]{TypeVariable.class}; +// CHECKSTYLE:OFF +public class OwbTypeVariableImpl { + private static final Class<?>[] TYPE_VARIABLE_TYPES = new Class<?>[] {TypeVariable.class}; /** - * Java TypeVariable is different in various JDK versions. Thus it is not possible to e.g. - * write a custom TypeVariable which works in either Java7 and Java8 as they introduced - * new methods in Java8 which have return generics which only exist in Java8 :( - * - * As workaround we dynamically crate a proxy to wrap this and do the delegation manually. - * This is of course slower, but as we do not use it often it might not have much impact. + * Java TypeVariable is different in various JDK versions. Thus it is not + * possible to e.g. write a custom TypeVariable which works in either Java7 + * and Java8 as they introduced new methods in Java8 which have return + * generics which only exist in Java8 :( As workaround we dynamically crate + * a proxy to wrap this and do the delegation manually. This is of course + * slower, but as we do not use it often it might not have much impact. * * @param typeVariable * @param bounds * @return the typeVariable with the defined bounds. */ - public static TypeVariable createTypeVariable(TypeVariable typeVariable, Type... bounds) - { - TypeVariable tv = (TypeVariable) Proxy.newProxyInstance(OwbTypeVariableImpl.class.getClassLoader(), TYPE_VARIABLE_TYPES, - new OwbTypeVariableInvocationHandler(typeVariable, bounds)); + public static TypeVariable createTypeVariable(TypeVariable typeVariable, Type... bounds) { + TypeVariable tv = (TypeVariable)Proxy.newProxyInstance(OwbTypeVariableImpl.class.getClassLoader(), TYPE_VARIABLE_TYPES, + new OwbTypeVariableInvocationHandler(typeVariable, bounds)); return tv; } - - - public static class OwbTypeVariableInvocationHandler implements InvocationHandler - { + public static class OwbTypeVariableInvocationHandler implements InvocationHandler { private String name; private GenericDeclaration genericDeclaration; private Type[] bounds; - - public OwbTypeVariableInvocationHandler(TypeVariable typeVariable, Type... bounds) - { + public OwbTypeVariableInvocationHandler(TypeVariable typeVariable, Type... bounds) { name = typeVariable.getName(); genericDeclaration = typeVariable.getGenericDeclaration(); - if (bounds == null || bounds.length == 0) - { + if (bounds == null || bounds.length == 0) { this.bounds = typeVariable.getBounds(); - } - else - { + } else { this.bounds = bounds; } } - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable - { + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); - if ("equals".equals(methodName)) - { + if ("equals".equals(methodName)) { return typeVariableEquals(args[0]); - } - else if ("hashCode".equals(methodName)) - { + } else if ("hashCode".equals(methodName)) { return typeVariableHashCode(); - } - else if ("toString".equals(methodName)) - { + } else if ("toString".equals(methodName)) { return typeVariableToString(); - } - else if ("getName".equals(methodName)) - { + } else if ("getName".equals(methodName)) { return getName(); - } - else if ("getGenericDeclaration".equals(methodName)) - { + } else if ("getGenericDeclaration".equals(methodName)) { return getGenericDeclaration(); - } - else if ("getBounds".equals(methodName)) - { + } else if ("getBounds".equals(methodName)) { return getBounds(); } - // new method from java8... return null; } /** method from TypeVariable */ - public String getName() - { + public String getName() { return name; } /** method from TypeVariable */ - public GenericDeclaration getGenericDeclaration() - { + public GenericDeclaration getGenericDeclaration() { return genericDeclaration; } /** method from TypeVariable */ - public Type[] getBounds() - { + public Type[] getBounds() { return bounds.clone(); } /** method from TypeVariable */ - public int typeVariableHashCode() - { + public int typeVariableHashCode() { return Arrays.hashCode(bounds) ^ name.hashCode() ^ genericDeclaration.hashCode(); } /** method from TypeVariable */ - public boolean typeVariableEquals(Object object) - { - if (this == object) - { + public boolean typeVariableEquals(Object object) { + if (this == object) { return true; - } - else if (object instanceof TypeVariable) - { + } else if (object instanceof TypeVariable) { TypeVariable<?> that = (TypeVariable<?>)object; return name.equals(that.getName()) && genericDeclaration.equals(that.getGenericDeclaration()) && Arrays.equals(bounds, that.getBounds()); - } - else - { + } else { return false; } } /** method from TypeVariable */ - public String typeVariableToString() - { + public String typeVariableToString() { StringBuilder buffer = new StringBuilder(); buffer.append(name); - if (bounds.length > 0) - { + if (bounds.length > 0) { buffer.append(" extends "); boolean first = true; - for (Type bound: bounds) - { - if (first) - { + for (Type bound : bounds) { + if (first) { first = false; - } - else - { + } else { buffer.append(','); } buffer.append(' ').append(bound); diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbWildcardTypeImpl.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbWildcardTypeImpl.java index aebcfd0..e8a1c5b 100644 --- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbWildcardTypeImpl.java +++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/generics/OwbWildcardTypeImpl.java @@ -1,20 +1,18 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * 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. + * 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 org.apache.camel.maven.packaging.generics; @@ -54,7 +52,7 @@ public class OwbWildcardTypeImpl implements WildcardType { } buffer.append(' '); if (upperBound instanceof Class) { - buffer.append(((Class<?>) upperBound).getSimpleName()); + buffer.append(((Class<?>)upperBound).getSimpleName()); } else { buffer.append(upperBound); } @@ -71,7 +69,7 @@ public class OwbWildcardTypeImpl implements WildcardType { } buffer.append(' '); if (lowerBound instanceof Class) { - buffer.append(((Class<?>) lowerBound).getSimpleName()); + buffer.append(((Class<?>)lowerBound).getSimpleName()); } else { buffer.append(lowerBound); }