Giter Site home page Giter Site logo

EditorLoader in crest main about crest HOT 3 CLOSED

rmannibucau avatar rmannibucau commented on July 17, 2024
EditorLoader in crest main

from crest.

Comments (3)

jeanouii avatar jeanouii commented on July 17, 2024

Committed that way for now.

from crest.

dblevins avatar dblevins commented on July 17, 2024

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.

rmannibucau avatar rmannibucau commented on July 17, 2024

Well issue is more about Editors I want to override. Date or File ones for instance.

from crest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.