summaryrefslogtreecommitdiffstats
path: root/java
diff options
context:
space:
mode:
authorkelvingoodson <kelvingoodson@13f79535-47bb-0310-9956-ffa450edef68>2009-07-10 15:04:38 +0000
committerkelvingoodson <kelvingoodson@13f79535-47bb-0310-9956-ffa450edef68>2009-07-10 15:04:38 +0000
commitc9d9db04cde0e918aca78a12ba9a47005fb4d176 (patch)
treecde26272c018536fae100af5cce730156983d1e0 /java
parentc8618dc4c7130599fadc315fd8338040a9c06544 (diff)
TUSCANY-3137 trap the annotation of static fields and methods with @Property or @Reference annotations.
Move some recently added property and reference tests to more appropriate location git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@792970 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
-rw-r--r--java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java9
-rw-r--r--java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java10
-rw-r--r--java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java26
-rw-r--r--java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java25
-rw-r--r--java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java73
-rw-r--r--java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java74
6 files changed, 166 insertions, 51 deletions
diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java
index 51e7164e0e..818e9ad048 100644
--- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java
+++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/AbstractPropertyProcessor.java
@@ -22,6 +22,7 @@ import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -72,6 +73,10 @@ public abstract class AbstractPropertyProcessor<A extends Annotation> extends Ba
if (!JavaIntrospectionHelper.isSetter(method)) {
throw new IllegalPropertyException("Annotated method is not a setter: " + method, method);
}
+
+ if(Modifier.isStatic(method.getModifiers())) {
+ throw new IllegalPropertyException("Static method " + method.getName() +" in class " + method.getDeclaringClass().getName() + " can not be annotated as a Property");
+ }
String name = getName(annotation);
if (name == null || "".equals(name)) {
@@ -120,6 +125,10 @@ public abstract class AbstractPropertyProcessor<A extends Annotation> extends Ba
if (annotation == null) {
return;
}
+
+ if(Modifier.isStatic(field.getModifiers())) {
+ throw new IllegalPropertyException("Static field " + field.getName() +" in class " + field.getDeclaringClass().getName() + " can not be annotated as a Property");
+ }
String name = getName(annotation);
if (name == null) {
diff --git a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java
index 1c0cc3595a..2374b6ba7c 100644
--- a/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java
+++ b/java/sca/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessor.java
@@ -24,6 +24,7 @@ import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
@@ -66,6 +67,11 @@ public class ReferenceProcessor extends BaseJavaClassVisitor {
if (!JavaIntrospectionHelper.isSetter(method)) {
throw new IllegalReferenceException("Annotated method is not a setter: " + method, method);
}
+
+ if(Modifier.isStatic(method.getModifiers())) {
+ throw new IllegalPropertyException("Static method " + method.getName() +" in class " + method.getDeclaringClass().getName() + " can not be annotated as a Reference");
+ }
+
String name = annotation.name();
if ("".equals(name)) {
name = JavaIntrospectionHelper.toPropertyName(method.getName());
@@ -102,6 +108,10 @@ public class ReferenceProcessor extends BaseJavaClassVisitor {
if (annotation == null) {
return;
}
+
+ if(Modifier.isStatic(field.getModifiers())) {
+ throw new IllegalReferenceException("Static field " + field.getName() +" in class " + field.getDeclaringClass().getName() + " can not be annotated as Reference");
+ }
String name = annotation.name();
if ("".equals(name)) {
name = field.getName();
diff --git a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java
index be90ba297e..83e26dff84 100644
--- a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java
+++ b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorPropertyTestCase.java
@@ -120,21 +120,7 @@ public class ConstructorPropertyTestCase extends AbstractProcessorTest {
// TODO multiplicity
// }
- @Test
- public void testClassWithBadMethodArgProperty() throws Exception {
- JavaImplementation type = javaImplementationFactory.createJavaImplementation();
- Method meth = BadFoo2.class.getMethod("BadFoo2Method", String.class);
-
- try {
- propertyProcessor.visitMethod(meth, type);
-
- fail();
- } catch (IllegalPropertyException e) {
- e.printStackTrace();
- System.out.println("Exception successfully received");
- }
- }
private static class Foo {
@@ -183,18 +169,6 @@ public class ConstructorPropertyTestCase extends AbstractProcessorTest {
}
- private static class BadFoo2 {
-
- @org.oasisopen.sca.annotation.Constructor()
- public BadFoo2(@Property(name = "myProp", required = true)String prop) {
- }
-
- /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/
- public void BadFoo2Method(@Property(name = "badMethodArgProp")String methArg)
- {}
-
-
- }
}
diff --git a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java
index 2aac962730..51e195d8d6 100644
--- a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java
+++ b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConstructorReferenceTestCase.java
@@ -118,20 +118,6 @@ public class ConstructorReferenceTestCase extends AbstractProcessorTest {
}
}
- @Test
- public void testClassWithBadMethodArgReference() throws Exception {
- JavaImplementation type = javaImplementationFactory.createJavaImplementation();
- Method meth = BadFoo2.class.getMethod("BadFoo2Method", String.class);
-
- try {
- referenceProcessor.visitMethod(meth, type);
-
- fail();
- } catch (IllegalReferenceException e) {
- e.printStackTrace();
- System.out.println("Exception successfully received");
- }
- }
// public void testMultiplicityRequired() throws Exception {
// TODO multiplicity
@@ -192,18 +178,7 @@ public class ConstructorReferenceTestCase extends AbstractProcessorTest {
}
- private static class BadFoo2 {
- @org.oasisopen.sca.annotation.Constructor()
- public BadFoo2(@Property(name = "myProp", required = true)String prop) {
- }
-
- /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/
- public void BadFoo2Method(@Reference(name = "badMethodArgRef")String methArg)
- {}
-
-
- }
}
diff --git a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java
index 99e28365da..1a8da7f1b3 100644
--- a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java
+++ b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/PropertyProcessorTestCase.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
@@ -185,6 +186,29 @@ public class PropertyProcessorTestCase {
}
}
+
+ private static class BadMethodProps {
+
+ @org.oasisopen.sca.annotation.Constructor()
+ public BadMethodProps(@Property(name = "myProp", required = true)String prop) {
+
+ }
+
+ /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/
+ public void BadMethod(@Property(name = "badMethodArgProp")String methArg)
+ {}
+
+
+ }
+
+ private static class BadStaticProps {
+
+ @Property(name="badstaticfield")static int stint;
+
+ @Property(name="badstaticfield")static void setStint(int theStint) {
+ stint = theStint;
+ }
+ }
private Class<?> getBaseType(JavaElementImpl element) {
return JavaIntrospectionHelper.getBaseType(element.getType(), element.getGenericType());
@@ -225,5 +249,54 @@ public class PropertyProcessorTestCase {
assertSame(String.class, getBaseType(type.getPropertyMembers().get(prop.getName())));
assertTrue(prop.isMany());
}
+
+ @Test
+ public void testRejectStaticFieldProperty() throws Exception {
+ try {
+ processor.visitField(BadStaticProps.class.getDeclaredField("stint"), type);
+ fail("Processor should not accept a static field with Property annotation");
+ }
+ catch (IllegalPropertyException e) {
+ // System.out.println("Caught expected exception");
+ }
+ catch (Exception e) {
+ fail("Wrong exception detected");
+ }
+ }
+
+ @Test
+ public void testRejectStaticMethodProperty() throws Exception {
+ try {
+ processor.visitMethod(BadStaticProps.class.getDeclaredMethod("setStint",int.class), type);
+ fail("Processor should not accept a static method with Property annotation");
+ }
+ catch (IllegalPropertyException e) {
+ // System.out.println("Caught expected exception");
+ }
+ catch (Exception e) {
+ fail("Wrong exception detected");
+ e.printStackTrace();
+ }
+
+ }
+
+ @Test
+ public void testClassWithBadMethodArgProperty() throws Exception {
+ Method meth = BadMethodProps.class.getMethod("BadMethod", String.class);
+
+ try {
+ processor.visitMethod(meth, type);
+
+ fail("Method with @Property annotated args should be rejected");
+ } catch (IllegalPropertyException e) {
+// e.printStackTrace();
+// System.out.println("Exception successfully received");
+ }
+ catch (Exception e) {
+ fail("Wrong exception received");
+ e.printStackTrace();
+ }
+
+ }
}
diff --git a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java
index 1c061bfd43..8e49674b7e 100644
--- a/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java
+++ b/java/sca/modules/implementation-java/src/test/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ReferenceProcessorTestCase.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
+import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
@@ -36,6 +37,7 @@ import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory;
import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
import org.junit.Before;
import org.junit.Test;
+import org.oasisopen.sca.annotation.Property;
import org.oasisopen.sca.annotation.Reference;
/**
@@ -121,6 +123,24 @@ public class ReferenceProcessorTestCase {
// expected
}
}
+
+ @Test
+ public void testClassWithBadMethodArgReference() throws Exception {
+ Method meth = BadMethAnn.class.getMethod("BadMethod", String.class);
+
+ try {
+ processor.visitMethod(meth, type);
+
+ fail("reference annotation on ordinary method arg should be rejected");
+ } catch (IllegalReferenceException e) {
+// e.printStackTrace();
+// System.out.println("Exception successfully received");
+ }
+ catch (Exception e) {
+ fail("Wrong exception detected");
+ e.printStackTrace();
+ }
+ }
@Before
public void setUp() throws Exception {
@@ -193,6 +213,29 @@ public class ReferenceProcessorTestCase {
}
}
+
+ private static class BadStaticRefs {
+
+ @Reference(name="badstaticfield")static int stint;
+
+ @Reference(name="badstaticfield")static void setStint(int theStint) {
+ stint = theStint;
+ }
+ }
+
+
+ private static class BadMethAnn {
+
+ @org.oasisopen.sca.annotation.Constructor()
+ public BadMethAnn(@Property(name = "myProp", required = true)String prop) {
+
+ }
+
+ /** Java can't tell that the @reference argument is disallowed by SCA, but the run time must reject it*/
+ public void BadMethod(@Reference(name = "badMethodArgRef")String methArg)
+ {}
+
+ }
@Test
public void testMultiplicity1ToN() throws Exception {
@@ -233,5 +276,36 @@ public class ReferenceProcessorTestCase {
assertEquals(Multiplicity.ZERO_N, ref.getMultiplicity());
// assertFalse(ref.isMustSupply());
}
+
+ @Test
+ public void testRejectStaticFieldReference() throws Exception {
+ try {
+ processor.visitField(BadStaticRefs.class.getDeclaredField("stint"), type);
+ fail("Processor should not accept a static field with Property annotation");
+ }
+ catch (IllegalReferenceException e) {
+ // System.out.println("Caught expected exception");
+ }
+ catch (Exception e) {
+ fail("Wrong exception detected");
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testRejectStaticMethodReference() throws Exception {
+ try {
+ processor.visitMethod(BadStaticRefs.class.getDeclaredMethod("setStint",int.class), type);
+ fail("Processor should not accept a static method with Property annotation");
+ }
+ catch (IllegalPropertyException e) {
+ // System.out.println("Caught expected exception");
+ }
+ catch (Exception e) {
+ fail("Wrong exception detected");
+ e.printStackTrace();
+ }
+
+ }
}