private Instantiator createInstantiatorForClass()

in tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ComponentInstantiatorSourceImpl.java [381:470]


    private Instantiator createInstantiatorForClass(final String className)
    {
        return tracker.invoke(String.format("Creating instantiator for component class %s", className),
                new Invokable<Instantiator>()
                {
                    public Instantiator invoke()
                    {
                        
                        // Force the creation of the class (and the transformation of the class). This will first
                        // trigger transformations of any base classes.
                        
                        OPEN_INSTANTIATORS.get().add(className);
                        
                        componentDependencyRegistry.disableInvalidations();
                        
                        // Make sure the dependencies have been processed in case
                        // there was some invalidation going on and they're not there.
                        
                        if (multipleClassLoaders)
                        {

                            final Set<String> dependencies = new HashSet<>();
                            dependencies.addAll(
                                    componentDependencyRegistry.getDependencies(className, DependencyType.USAGE));
                            dependencies.addAll(
                                    componentDependencyRegistry.getDependencies(className, DependencyType.SUPERCLASS));
                            for (String dependency : dependencies)
                            {
                                if (!OPEN_INSTANTIATORS.get().contains(dependency))
                                {
                                    if (multipleClassLoaders)
                                    {
                                        getInstantiator(dependency);
                                    }
                                    else
                                    {
                                        createInstantiatorForClass(dependency);
                                    }
                                }
                            }
                        }
                            
                        PageClassLoaderContext context;
                        try
                        {
                            context = pageClassLoaderContextManager.get(className);
                        }
                        finally
                        {
                            componentDependencyRegistry.enableInvalidations();
                        }
                        
                        ClassInstantiator<Component> plasticInstantiator;
                        try 
                        {
                            plasticInstantiator = context.getPlasticManager().getClassInstantiator(className);
                            if (multipleClassLoaders)
                            {
                                context.getPlasticManager().getClassLoader().loadClass(className);
                            }
                        } catch (Exception e) {
//                            System.out.println(pageClassLoaderContextManager.getRoot().toRecursiveString());
                            throw new RuntimeException(e);
                        }
                        final ComponentModel model = classToModel.get(className);
                        
                        OPEN_INSTANTIATORS.get().remove(className);

                        return new Instantiator()
                        {
                            public Component newInstance(InternalComponentResources resources)
                            {
                                return plasticInstantiator.with(ComponentResources.class, resources)
                                        .with(InternalComponentResources.class, resources).newInstance();
                            }

                            public ComponentModel getModel()
                            {
                                return model;
                            }

                            @Override
                            public String toString()
                            {
                                return String.format("[Instantiator[%s:%s]", className, context);
                            }
                        };
                    }
                });
    }