|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.geektimes.commons.reflect.util; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.lang.reflect.Proxy; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.function.Predicate; |
| 23 | + |
| 24 | +import static org.geektimes.commons.function.Predicates.and; |
| 25 | +import static org.geektimes.commons.reflect.util.ClassUtils.isFinal; |
| 26 | +import static org.geektimes.commons.reflect.util.ClassUtils.*; |
| 27 | +import static org.geektimes.commons.reflect.util.ConstructorUtils.hasNonPrivateConstructorWithoutParameters; |
| 28 | +import static org.geektimes.commons.reflect.util.MemberUtils.*; |
| 29 | +import static org.geektimes.commons.reflect.util.MethodUtils.OBJECT_METHOD_PREDICATE; |
| 30 | +import static org.geektimes.commons.reflect.util.MethodUtils.getAllDeclaredMethods; |
| 31 | + |
| 32 | +/** |
| 33 | + * The utilities class for {@link Proxy Proxy} |
| 34 | + * |
| 35 | + * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a> |
| 36 | + * @since 1.0.0 |
| 37 | + */ |
| 38 | +public abstract class ProxyUtils { |
| 39 | + |
| 40 | + /** |
| 41 | + * <ul> |
| 42 | + * <li>class has a non-private constructor with no parameters</li> |
| 43 | + * <li>class is not declared final</li> |
| 44 | + * <li>class does not have non-static, final methods with public, protected or default visibility</li> |
| 45 | + * <li>class is not primitive type</li> |
| 46 | + * <li>class is not array type</li> |
| 47 | + * </ul> |
| 48 | + * |
| 49 | + * @param type |
| 50 | + * @return |
| 51 | + */ |
| 52 | + public static boolean isProxyable(Class<?> type) { |
| 53 | + if (isArray(type)) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + if (isPrimitive(type)) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + if (isFinal(type)) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + if (!hasNonPrivateConstructorWithoutParameters(type)) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + Predicate<Method> predicate = and(NON_STATIC_METHOD_PREDICATE, FINAL_METHOD_PREDICATE, |
| 70 | + NON_PRIVATE_METHOD_PREDICATE, OBJECT_METHOD_PREDICATE.negate()); |
| 71 | + |
| 72 | + Set<Method> methods = getAllDeclaredMethods(type, predicate); |
| 73 | + |
| 74 | + if (!methods.isEmpty()) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + return true; |
| 79 | + } |
| 80 | +} |
0 commit comments