You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by st...@apache.org on 2014/09/17 19:20:20 UTC
svn commit: r1625710 - in /openwebbeans/trunk/webbeans-impl/src:
main/java/org/apache/webbeans/util/ test/java/org/apache/webbeans/test/util/
Author: struberg
Date: Wed Sep 17 17:20:20 2014
New Revision: 1625710
URL: http://svn.apache.org/r1625710
Log:
OWB-1008 check abstract methods in Decorators must be from from the decorated type
txs to Reinhard Sandtner (rsandtner) for the patch!
Added:
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/AbstractClassWithAbstractMethod.java (with props)
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/ClassUtilTest.java
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java?rev=1625710&r1=1625709&r2=1625710&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java Wed Sep 17 17:20:20 2014
@@ -31,6 +31,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -349,6 +350,61 @@ public final class ClassUtil
}
/**
+ * collect all abstract methods for the given class if the given class
+ * is {@link Modifier#ABSTRACT}
+ *
+ * @param clazz {@link Class} to check
+ *
+ * @return {@link List} with all abstract methods of the given class or an empty list
+ * if the given class is not abstract or doesn't contain an abstract method
+ */
+ public static List<Method> getAbstractMethods(Class<?> clazz)
+ {
+ if (!Modifier.isAbstract(clazz.getModifiers()))
+ {
+ return Collections.emptyList();
+ }
+
+ List<Method> methods = getNonPrivateMethods(clazz, true);
+ if (!methods.isEmpty())
+ {
+ Iterator<Method> iterator = methods.iterator();
+ while (iterator.hasNext())
+ {
+ if (!Modifier.isAbstract(iterator.next().getModifiers()))
+ {
+ iterator.remove();
+ }
+ }
+ }
+
+ return methods;
+ }
+
+ /**
+ * Checks, if the given {@link Class} declares the {@link Method} with the
+ * given name und parameterTypes.
+ *
+ * @param clazz to check
+ * @param name of the method
+ * @param parameterTypes of the method
+ *
+ * @return {@code} true if the given class contains a method with the given name and parameterTypes,
+ * otherwise {@code false}
+ */
+ public static boolean isMethodDeclared(Class<?> clazz, String name, Class<?>... parameterTypes)
+ {
+ try
+ {
+ return clazz.getMethod(name, parameterTypes) != null;
+ }
+ catch (NoSuchMethodException e)
+ {
+ return false;
+ }
+ }
+
+ /**
* Check if the method is already defined in a subclass
* @param subclassMethods
* @param superclassMethod
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=1625710&r1=1625709&r2=1625710&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java Wed Sep 17 17:20:20 2014
@@ -1656,6 +1656,32 @@ public final class WebBeansUtil
throw new DefinitionException("injected bean parameter must be " + rawType);
}
}
+
+ if (isDecorator)
+ {
+ List<Method> abstractMethods = ClassUtil.getAbstractMethods(bean.getBeanClass());
+ if (!abstractMethods.isEmpty())
+ {
+ Set<Type> types = ((javax.enterprise.inject.spi.Decorator) bean).getDecoratedTypes();
+ for (Method abstractMethod : abstractMethods)
+ {
+ boolean methodDeclared = false;
+ for (Type type : types)
+ {
+ if (ClassUtil.isMethodDeclared(ClassUtil.getClass(type), abstractMethod.getName(), abstractMethod.getParameterTypes()))
+ {
+ methodDeclared = true;
+ break;
+ }
+ }
+
+ if (!methodDeclared)
+ {
+ throw new DefinitionException("Decorator must not declare abstract methods which is not declared in any Decorated type.");
+ }
+ }
+ }
+ }
}
if (!isInterceptor && injectionPoint.getAnnotated().isAnnotationPresent(Intercepted.class))
Added: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/AbstractClassWithAbstractMethod.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/AbstractClassWithAbstractMethod.java?rev=1625710&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/AbstractClassWithAbstractMethod.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/AbstractClassWithAbstractMethod.java Wed Sep 17 17:20:20 2014
@@ -0,0 +1,42 @@
+/*
+ * 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
+ *
+ * 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.webbeans.test.util;
+
+/**
+ * Class is needed to test ClassUtil.getAbstractMethods
+ */
+public abstract class AbstractClassWithAbstractMethod
+{
+
+ public abstract String getPublicString();
+
+ protected abstract int getProtectedInt();
+
+
+ public void doSomething()
+ {
+
+ }
+
+ public void doSomethingElse()
+ {
+
+ }
+
+}
Propchange: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/AbstractClassWithAbstractMethod.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/ClassUtilTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/ClassUtilTest.java?rev=1625710&r1=1625709&r2=1625710&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/ClassUtilTest.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/test/java/org/apache/webbeans/test/util/ClassUtilTest.java Wed Sep 17 17:20:20 2014
@@ -68,6 +68,34 @@ public class ClassUtilTest {
Assert.assertFalse(isOverridden(MyOtherPackageSubClass.class, "privateMethod"));
}
+ @Test
+ public void testGetAbstractMethods() throws Exception
+ {
+ List<Method> methods = ClassUtil.getAbstractMethods(AbstractClassWithAbstractMethod.class);
+ Assert.assertNotNull(methods);
+ Assert.assertEquals(2, methods.size());
+
+ for (Method method : methods)
+ {
+ String name = method.getName();
+ if (!name.equals("getPublicString") && !name.equals("getProtectedInt"))
+ {
+ Assert.fail("unexpected Method " + name);
+ }
+ }
+
+ Assert.assertTrue("returned methods must be empty", ClassUtil.getAbstractMethods(MySubClass.class).isEmpty());
+ }
+
+ @Test
+ public void testIsMethodDeclared() throws Exception
+ {
+ Class<AbstractClassWithAbstractMethod> clazz = AbstractClassWithAbstractMethod.class;
+
+ Assert.assertTrue(ClassUtil.isMethodDeclared(clazz, "doSomething"));
+ Assert.assertFalse(ClassUtil.isMethodDeclared(clazz, "notExistingMethod"));
+ }
+
private boolean isOverridden(Class subClass, String methodName) throws Exception
{
Method superClassMethod = MySuperClass.class.getDeclaredMethod(methodName);