Comments (3)
Committed that way for now.
from crest.
Curious what pattern you're using that doesn't fit into "new Foo(String)" or the "Foo Foo.get(String)" patterns. Needing an editor should be the uncommon case.
On May 9, 2014, at 12:03 AM, Romain Manni-Bucau [email protected] wrote:
With crest you often need custom editors. When using xbean would be gret to get them loaded automatically. I used this code to do so (side note: I didnt check xbean was really optional so something to do before including it blindly - same loader system as for scanning could be used btw):
import java.beans.PropertyEditorManager;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ServiceLoader;import org.apache.xbean.finder.Annotated;
import org.apache.xbean.finder.AnnotationFinder;
import org.apache.xbean.finder.UrlSet;
import org.apache.xbean.finder.archive.Archive;
import org.apache.xbean.finder.archive.ClasspathArchive;
import org.apache.xbean.finder.archive.CompositeArchive;
import org.tomitribe.util.JarLocation;public class EditorLoader {
static {
try {
new XBeanLoader().load();
} catch (final Throwable skip) {
// no-op
}
for (final Editor editor : ServiceLoader.load(Editor.class)) // class MyEditor extends AbstractConverter implements Editor
{
try {
PropertyEditorManager.registerEditor(editor.value(), editor.getClass());
} catch (final Exception e) {
// no-op
}
}
}
public static void load() {
// no-op
}
private static Archive thisArchive() {
try {
final Class<?> reference = EditorLoader.class;
final File file = JarLocation.jarLocation(reference);
return ClasspathArchive.archive(reference.getClassLoader(), file.toURI().toURL());
} catch (final MalformedURLException e) {
throw new IllegalStateException(e);
}
}
private static Archive cpArchive() {
try {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
return new ClasspathArchive(
classLoader,
new UrlSet(classLoader).excludeJvm().exclude(classLoader.getParent()).getUrls());
} catch (final IOException e) {
throw new IllegalStateException(e);
}
}
// just allow lazy loading and avoid issues if XBean is not here
private static class XBeanLoader {
public void load() {
final AnnotationFinder finder = new AnnotationFinder(new CompositeArchive(thisArchive(), cpArchive())).enableFindSubclasses();
for (final Annotated<Class<?>> clazz : finder.findMetaAnnotatedClasses(Editor.class)) {
PropertyEditorManager.registerEditor(clazz.getAnnotation(Editor.class).value(), clazz.get());
}
}
}
}and I wrapped crest main:
// just a wrapper to get custom editors
public class Main {public static void main(final String[] args) throws Exception {
EditorLoader.load();
org.tomitribe.crest.Main.main(args);
}
private Main() {
// no-op
}
}
Then to write a custom editor you just need to either decorate it with @Editor(editorClassKey) or without XBean also implement Editor (to use ServiceLoader - note this part could be removed and if we have a EditorLoader just use it to list editor classes with @Editor annotation which would be easier and more consistent).—
Reply to this email directly or view it on GitHub.
from crest.
Well issue is more about Editors I want to override. Date or File ones for instance.
from crest.
Related Issues (20)
- org.tomitribe.crest.environments.Environment#ENVIRONMENT_THREAD_LOCAL shouldnt have a default
- Add a Mojo (mvn plugin) to generate all UNIx command scripts from found commands
- Null returned when using @Option and Bean Validation HOT 1
- Injected streams do not play work together with argument arrays
- PrintingOutput as alternative to StreamingOutput HOT 1
- Support 'Stream<String>' return types for commands
- Parameter values are converted to lowercase when passed on the command line HOT 6
- crest-maven-plugin: add java 11 support, upgrade ASM to >6 HOT 7
- Make the integration with GraalVM smoother HOT 1
- move helpprocessor in crest-processor
- Eliminate IllegalArgumentException "No interceptor for " issue with @CrestInterceptor HOT 2
- Support for descending sorts via "thefield>"
- Excluding fields from @Table output
- Support nested tables
- CDI integration
- Support TSV format in @Table output
- Support CSV format in @Table output
- Builder API for creating the CLI
- Help text shows command name and version
- Use MANIFEST.MF for Cli configuration, initially Name and Version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crest.