-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SLICE-128 making SliceLookUpTag type attribute value String #109
base: master
Are you sure you want to change the base?
Changes from 7 commits
48f58b4
ee07da0
314679a
4b910c0
46e22c6
572e333
0c74dd9
391e3d0
c676d8b
3b39c6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,35 @@ public static <T> T getFromCurrentPath(final PageContext pageContext, final Clas | |
return getFromCurrentPath(request, injectorsRepository, requestContextProvider, type, appName); | ||
} | ||
|
||
/** | ||
* A helper method that returns the {@link Class} object, resolving it via it's Canonical Name. | ||
* | ||
* @param pageContext allows to access request context | ||
* @param type canonical name of the modal object, whose {@link Class} object needs to be returned | ||
* @return {@link Class} object pertaining to {@code type} as it's canonical name | ||
* @throws ClassNotFoundException if the class was not found | ||
*/ | ||
public static Class<?> getClassFromType(final PageContext pageContext, final String type) throws ClassNotFoundException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mmajchrzak I've also refactored this method to use ModelProvider class to get it's Class object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm.. By introducing of this method you're fetching the model twice during tag execution. Refactor it please, so that modelProvider#get(String, Resource) is used but model is read only once. |
||
final SlingHttpServletRequest request = SliceTagUtils.slingRequestFrom(pageContext); | ||
final InjectorsRepository injectorsRepository = SliceTagUtils.injectorsRepositoryFrom(pageContext); | ||
|
||
final String injectorName = getInjectorName(request, null, injectorsRepository); | ||
|
||
final InjectorWithContext injector = injectorsRepository.getInjector(injectorName); | ||
if (injector == null) { | ||
throw new IllegalStateException("Guice injector not found: " + injectorName); | ||
} | ||
injector.pushContextProvider(contextProviderFrom(pageContext)); | ||
|
||
final ModelProvider modelProvider = injector.getInstance(ModelProvider.class); | ||
|
||
try { | ||
return modelProvider.get(type, request.getResource()).getClass(); | ||
} finally { | ||
injector.popContextProvider(); | ||
} | ||
} | ||
|
||
/** | ||
* A helper method that returns a model of the Sling resource related to given request | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/*- | ||
* #%L | ||
* Slice - Core Tests | ||
* %% | ||
* Copyright (C) 2012 Cognifide Limited | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
|
||
package com.cognifide.slice.api.tag | ||
|
||
import com.cognifide.slice.test.setup.BaseSetup | ||
import org.apache.sling.api.scripting.SlingBindings | ||
import org.apache.sling.api.scripting.SlingScriptHelper | ||
import org.apache.sling.commons.classloader.DynamicClassLoaderManager | ||
import org.junit.Assert | ||
|
||
import javax.servlet.ServletRequest | ||
import javax.servlet.jsp.PageContext | ||
import java.lang.reflect.Method | ||
|
||
/** | ||
* @author Shashi Bhushan | ||
* Date: 23.08.16 | ||
*/ | ||
class SliceTagUtilsTest extends BaseSetup { | ||
|
||
def "Get Class object, given the String type"() { | ||
|
||
given: | ||
final PageContext pageContext = Mock(PageContext){ | ||
getRequest()>> Mock(ServletRequest){ | ||
getAttribute(_ as String) >> Mock(SlingBindings) { | ||
getSling() >> Mock(SlingScriptHelper) { | ||
getService(_ as Class) >> Mock(DynamicClassLoaderManager) { | ||
getDynamicClassLoader() >> Mock(ClassLoader) { | ||
loadClass(_ as String) >> Class.forName(classList.className) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
when: | ||
Class classObject = SliceTagUtils.getClassFromType(pageContext, classList.className); | ||
|
||
then: "classObject should not be null and should be equal to expected Class" | ||
Assert.assertNotNull(classObject) | ||
Assert.assertEquals(classList.classObject, classObject); | ||
|
||
where: | ||
classList << [ | ||
[ | ||
className : "com.cognifide.slice.api.tag.SliceTagUtils", | ||
classObject : SliceTagUtils.class | ||
] | ||
] | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing the signature of this method forces you to export the package in version 5.0.0. We're not quite there to release Slice 5. Maybe we can add just new method - the should allow this package to be in version 4.4.0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mmajchrzak i'm not able to overload setType with String argument here. I think overloading is not supported in tag lib. do you want me to create a new attribute altogether ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we keep this method with changed signature, then this code will be released in Slice 5. I'm not sure about overloading in taglib neither - this needs checking. To keep backward compatibility we could also think about introducing a new parameter, but I'd definitely prefer overloading if it's possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've did some searching and trials and errors with code as well, so far what I've got is overloading is not possible for taglibs, also mentioned on this blog.
I've also did a POC on having a separate 'name' attribute. It works for sure, but is not without it's own repercussions. What if both type and name attribute are used(and say for an edge case, both have different class values), which one should be preferred is the mystery?
Leaving it up to you, should i keep looking for a way to overload the taglib attribute setter or go with a new attribute.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd double check the overloading - the blog post you referenced is mentioning JavaBeans not taglibs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have a look at this documentation
Setting Deferred Value Attributes and Deferred Method Attributes section in particular.
It mentions that i can have __setAttr(Object obj) __ and then check instanceOf obj.
I can do a test run of this change, but firstly what are your views on this change ?
Will the major version of Slice needs to be incremented, if i update only the setter signature and not the actual attribute type ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately yes, because the plugin which controls the version number looks at the method signatures. From my point of view, if we can't keep the current setter method, I'd postpone the implementation until the Slice 5, which is allowed to be break the API.