Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2008, 2010 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.tapestry5.internal.transform;
 
Recognizes the org.apache.tapestry5.annotations.InjectComponent annotation, and converts the field into a read-only field containing the component. The id of the component may be explicitly stated or will be determined from the field name.
 
 {
     private final class InjectedComponentFieldValueConduit extends ReadOnlyFieldValueConduit
     {
         private final ComponentResources resources;
         private final String fieldNamecomponentIdtype;
 
         private Component embedded;
 
         private InjectedComponentFieldValueConduit(final ComponentResources resourcesString fieldNameString type,
                 String componentId)
         {
             super(resourcesfieldName);
 
             this. = resources;
             this. = fieldName;
             this. = componentId;
             this. = type;
 
             resources.addPageLifecycleListener(new PageLifecycleAdapter()
             {
                 public void containingPageDidLoad()
                 {
                     load();
 
                     resources.removePageLifecycleListener(this);
                 };
             });
         }
 
         private void load()
         {
              = .getEmbeddedComponent();
 
             Class fieldType = .forName();
 
             if (!fieldType.isInstance())
                 throw new RuntimeException(
                         String
                                 .format(
                                         "Unable to inject component '%s' into field %s of component %s.  Class %s is not assignable to a field of type %s.",
                                         .getCompleteId(), .getClass()
                                                 .getName(), fieldType.getName()));
         }
 
         public Object get()
         {
             return ;
         }
     }
 
     private final ComponentClassCache classCache;
 
     public InjectComponentWorker(ComponentClassCache classCache)
     {
         this. = classCache;
     }
 
     public void transform(ClassTransformation transformationMutableComponentModel model)
     {
         for (TransformField field : transformation.matchFieldsWithAnnotation(InjectComponent.class))
         {
             InjectComponent annotation = field.getAnnotation(InjectComponent.class);
 
             field.claim(annotation);
            final String type = field.getType();
            final String componentId = getComponentId(fieldannotation);
            final String fieldName = field.getName();
            {
                public FieldValueConduit get(final ComponentResources resources)
                {
                    return new InjectedComponentFieldValueConduit(resourcesfieldNametypecomponentId);
                }
            };
            field.replaceAccess(provider);
        }
    }
    private String getComponentId(TransformField fieldInjectComponent annotation)
    {
        String id = annotation.value();
        if (InternalUtils.isNonBlank(id))
            return id;
        return InternalUtils.stripMemberName(field.getName());
    }
New to GrepCode? Check out our FAQ X