You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2008/05/14 02:40:35 UTC
svn commit: r656062 - in /tapestry/tapestry5/trunk/tapestry-core/src:
main/java/org/apache/tapestry/internal/transform/ test/app1/
test/java/org/apache/tapestry/integration/
test/java/org/apache/tapestry/integration/app1/pages/
test/resources/org/apach...
Author: hlship
Date: Tue May 13 17:40:34 2008
New Revision: 656062
URL: http://svn.apache.org/viewvc?rev=656062&view=rev
Log:
TAPESTRY-2370: When injecting a component into a field and the type is not a match the exception report is not sufficiently helpful
Added:
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.java
tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.tml
Modified:
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/transform/InjectComponentWorker.java
tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Start.tml
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java
Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/transform/InjectComponentWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/transform/InjectComponentWorker.java?rev=656062&r1=656061&r2=656062&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/transform/InjectComponentWorker.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry/internal/transform/InjectComponentWorker.java Tue May 13 17:40:34 2008
@@ -14,8 +14,10 @@
package org.apache.tapestry.internal.transform;
+import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.InjectComponent;
import org.apache.tapestry.ioc.internal.util.InternalUtils;
+import org.apache.tapestry.ioc.util.BodyBuilder;
import org.apache.tapestry.model.MutableComponentModel;
import org.apache.tapestry.services.ClassTransformation;
import org.apache.tapestry.services.ComponentClassTransformWorker;
@@ -45,14 +47,30 @@
transformation.makeReadOnly(fieldName);
- String body = String.format(
+ BodyBuilder builder = new BodyBuilder().addln("try").begin();
+
+ builder.addln(
"%s = (%s) %s.getEmbeddedComponent(\"%s\");",
fieldName,
type,
resourcesFieldName,
componentId);
+ builder.end();
+ builder.addln("catch (ClassCastException ex)").begin();
+ builder.addln("throw new RuntimeException(%s.formatMessage(%s, \"%s\", \"%s\", \"%s\"), ex);",
+ getClass().getName(), resourcesFieldName, fieldName, type, componentId);
+ builder.end();
- transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, body);
+ transformation.extendMethod(TransformConstants.CONTAINING_PAGE_DID_LOAD_SIGNATURE, builder.toString());
}
}
+
+ public static String formatMessage(ComponentResources resources, String fieldName, String fieldType,
+ String componentId)
+ {
+ return String.format(
+ "Unable to inject component '%s' into field %s of component %s. Class %s is not assignable to a field of type %s.",
+ componentId, fieldName, resources.getCompleteId(),
+ resources.getEmbeddedComponent(componentId).getClass().getName(), fieldType);
+ }
}
Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Start.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Start.tml?rev=656062&r1=656061&r2=656062&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Start.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/Start.tml Tue May 13 17:40:34 2008
@@ -37,6 +37,12 @@
</li>
<li>
+ <a href="injectcomponentmismatch">InjectComponentMismatch</a>
+ -- check error reporting when @InjectComponent
+ doesn't match actual field type
+ </li>
+
+ <li>
<a href="recursivedemo">Recursive Demo</a>
-- check for handling of recursive components
</li>
Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java?rev=656062&r1=656061&r2=656062&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/IntegrationTests.java Tue May 13 17:40:34 2008
@@ -164,6 +164,16 @@
}
@Test
+ public void inject_component_failure() throws Exception
+ {
+ start("InjectComponentMismatch");
+
+ assertTextPresent(
+ "Unable to inject component 'form' into field form of component InjectComponentMismatch. Class org.apache.tapestry.corelib.components.BeanEditForm is not assignable to a field of type org.apache.tapestry.corelib.components.Form.",
+ "ClassCastException");
+ }
+
+ @Test
public void injection() throws Exception
{
start("Inject Demo");
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.java?rev=656062&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.java Tue May 13 17:40:34 2008
@@ -0,0 +1,24 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.integration.app1.pages;
+
+import org.apache.tapestry.annotations.InjectComponent;
+import org.apache.tapestry.corelib.components.Form;
+
+public class InjectComponentMismatch
+{
+ @InjectComponent
+ private Form form;
+}
Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.tml?rev=656062&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry/integration/app1/pages/InjectComponentMismatch.tml Tue May 13 17:40:34 2008
@@ -0,0 +1,5 @@
+<html t:type="Border" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
+
+ <t:beaneditform t:id="form" object="this"/>
+
+</html>