LiferayObjectWrapper extends DefaultObjectWrapper { @Override protected TemplateModel handleUnknownType(Object object) { ModelFactory modelFactory = null; if (object instanceof Enumeration) { modelFactory = _ENUMERATION_MODEL_FACTORY; } else if (object instanceof Node) { modelFactory = _NODE_MODEL_FACTORY; } else if (object instanceof ResourceBundle) { modelFactory = _RESOURCE_BUNDLE_MODEL_FACTORY; } else { modelFactory = _STRING_MODEL_FACTORY; } _modelFactories.put(object.getClass(), modelFactory); return modelFactory.create(object, this); } LiferayObjectWrapper(); @Override TemplateModel wrap(Object object);  }
@Test public void testHandleUnknownType() throws Exception { LiferayObjectWrapper liferayObjectWrapper = new LiferayObjectWrapper(); Enumeration<String> enumeration = Collections.enumeration( Collections.singletonList("testElement")); assertTemplateModel( "testElement", enumerationModel -> enumerationModel.next(), EnumerationModel.class.cast( liferayObjectWrapper.handleUnknownType(enumeration))); _assertModelFactoryCache( "_ENUMERATION_MODEL_FACTORY", enumeration.getClass()); Node node = (Node)ProxyUtil.newProxyInstance( LiferayObjectWrapper.class.getClassLoader(), new Class<?>[] {Node.class, Element.class}, (proxy, method, args) -> { String methodName = method.getName(); if (methodName.equals("getNodeType")) { return Node.ELEMENT_NODE; } return null; }); TemplateModel templateModel = liferayObjectWrapper.handleUnknownType( node); Assert.assertTrue( "org.w3c.dom.Node should be handled as NodeModel", templateModel instanceof NodeModel); NodeModel nodeModel = (NodeModel)templateModel; Assert.assertSame(node, nodeModel.getNode()); Assert.assertEquals("element", nodeModel.getNodeType()); _assertModelFactoryCache("_NODE_MODEL_FACTORY", node.getClass()); ResourceBundle resourceBundle = new ResourceBundle() { @Override public Enumeration<String> getKeys() { return null; } @Override protected Object handleGetObject(String key) { return key; } }; assertTemplateModel( resourceBundle.toString(), resourceBundleModel -> resourceBundleModel.getBundle(), ResourceBundleModel.class.cast( liferayObjectWrapper.handleUnknownType(resourceBundle))); _assertModelFactoryCache( "_RESOURCE_BUNDLE_MODEL_FACTORY", resourceBundle.getClass()); assertTemplateModel( "1.0", stringModel -> stringModel.getAsString(), StringModel.class.cast( liferayObjectWrapper.handleUnknownType( liferayObjectWrapper.handleUnknownType( new Version("1.0"))))); _assertModelFactoryCache("_STRING_MODEL_FACTORY", Version.class); }