public ResourcesInvoker( Class<?> bundleClass ) { this.bundleClass = bundleClass; this.bundleName = calcBundleName( bundleClass ); this.bundles = new ConcurrentHashMap<>(); }
protected final String getPattern( final Method method ) { String pattern = getBundlePattern( method ); if( pattern == null ) { pattern = getAnnotationPattern( method ); if( pattern == null || Resource.DEFAULT_TEXT.equals( pattern ) ) { pattern = getDefaultPattern( method ); } } return pattern; }
@Override public Object invoke( final Object proxy, final Method method, final Object[] args ) throws Throwable { return getText( method, args ); }
protected final String getBundlePattern( final Method method ) { String pattern = null; final ResourceBundle bundle = findBundle(); if( bundle != null && bundle.containsKey( method.getName() ) ) { pattern = bundle.getString( method.getName() ); } return pattern; }
protected final String getText( final Method method, final Object[] args ) { String pattern = getPattern( method ); MessageFormat messageFormat = new MessageFormat(pattern, Locale.ROOT ); return messageFormat.format(args); }
@SuppressWarnings( "unchecked" ) public static <T> T get( Class<T> clazz ) { Object proxy = proxies.get( clazz ); if( proxy == null ) { Resources anno = clazz.getAnnotation( Resources.class ); if( anno == null ) { throw new IllegalArgumentException( clazz.getName() + " missing @" + Resources.class.getCanonicalName() ); } ResourcesInvoker invoker = new ResourcesInvoker( clazz ); proxy = Proxy.newProxyInstance( clazz.getClassLoader(), new Class[]{ clazz }, invoker ); proxies.put( clazz, proxy ); } return (T)proxy; }
protected final ResourceBundle findBundle() { final Locale locale = Locale.getDefault(); ResourceBundle bundle = bundles.get( locale ); if( bundle == MISSING_BUNDLE ) { bundle = null; } else if ( bundle == null ) { try { bundle = ResourceBundle.getBundle( getBundleName(), locale, bundleClass.getClassLoader() ); bundles.put( locale, bundle ); } catch( MissingResourceException e ) { bundles.put( locale, MISSING_BUNDLE ); } } return bundle; }
protected static String getDefaultPattern(final Method method ) { final String prefix = method.getName(); String suffix; final int params = method.getParameterTypes().length; switch( params ) { case( 0 ) : suffix = ""; break; case( 1 ) : suffix = "(\"{0}\")"; break; case( 2 ) : suffix = "(\"{0}\",\"{1}\")"; break; case( 3 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\")"; break; case( 4 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\")"; break; case( 5 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\")"; break; case( 6 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\")"; break; case( 7 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\")"; break; case( 8 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\")"; break; case( 9 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\")"; break; case( 10 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\")"; break; default : suffix = createDefaultPatternSuffix( params ); } return prefix + suffix; }
protected final String getBundlePattern( final Method method ) { String pattern = null; final ResourceBundle bundle = findBundle(); if( bundle != null && bundle.containsKey( method.getName() ) ) { pattern = bundle.getString( method.getName() ); } return pattern; }
protected final String getText( final Method method, final Object[] args ) { String pattern = getPattern( method ); MessageFormat messageFormat = new MessageFormat(pattern, Locale.ROOT ); return messageFormat.format(args); }
@SuppressWarnings( "unchecked" ) public static <T> T get( Class<T> clazz ) { Object proxy = proxies.get( clazz ); if( proxy == null ) { Resources anno = clazz.getAnnotation( Resources.class ); if( anno == null ) { throw new IllegalArgumentException( clazz.getName() + " missing @" + Resources.class.getCanonicalName() ); } ResourcesInvoker invoker = new ResourcesInvoker( clazz ); proxy = Proxy.newProxyInstance( clazz.getClassLoader(), new Class[]{ clazz }, invoker ); proxies.put( clazz, proxy ); } return (T)proxy; }
protected final ResourceBundle findBundle() { final Locale locale = Locale.getDefault(); ResourceBundle bundle = bundles.get( locale ); if( MISSING_BUNDLE.equals(bundle) ) { bundle = null; } else if ( bundle == null ) { try { bundle = ResourceBundle.getBundle( getBundleName(), locale, bundleClass.getClassLoader() ); bundles.put( locale, bundle ); } catch( MissingResourceException e ) { bundles.put( locale, MISSING_BUNDLE ); } } return bundle; }
protected static final String getDefaultPattern( final Method method ) { final String prefix = method.getName(); String suffix; final int params = method.getParameterTypes().length; switch( params ) { case( 0 ) : suffix = ""; break; case( 1 ) : suffix = "(\"{0}\")"; break; case( 2 ) : suffix = "(\"{0}\",\"{1}\")"; break; case( 3 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\")"; break; case( 4 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\")"; break; case( 5 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\")"; break; case( 6 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\")"; break; case( 7 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\")"; break; case( 8 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\")"; break; case( 9 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\")"; break; case( 10 ) : suffix = "(\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\")"; break; default : suffix = createDefaultPatternSuffix( params ); } return prefix + suffix; }
protected final String getPattern( final Method method ) { String pattern = getBundlePattern( method ); if( pattern == null ) { pattern = getAnnotationPattern( method ); if( pattern == null || Resource.DEFAULT_TEXT.equals( pattern ) ) { pattern = getDefaultPattern( method ); } } return pattern; }
public ResourcesInvoker( Class<?> bundleClass ) { this.bundleClass = bundleClass; this.bundleName = calcBundleName( bundleClass ); this.bundles = new ConcurrentHashMap<>(); }
@Override public Object invoke( final Object proxy, final Method method, final Object[] args ) throws Throwable { return getText( method, args ); }