Author: tchemit Date: 2008-08-10 16:40:49 +0000 (Sun, 10 Aug 2008) New Revision: 828 Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionAnnotationProcessing.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProvider.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderAnnotation.java trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java Log: simplify code : all is fully generated now : nothing has to be configured via the annotation. just placed it :) suppression name field in ActionProvider Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionAnnotationProcessing.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionAnnotationProcessing.java 2008-08-10 15:18:09 UTC (rev 827) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionAnnotationProcessing.java 2008-08-10 16:40:49 UTC (rev 828) @@ -64,11 +64,11 @@ * @author chemit */ public class ActionAnnotationProcessing extends AbstractProcessor { - protected ActionProviderAnnotation providerConfig; + //protected ActionProviderAnnotation providerConfig; protected String providerDeclarationLocation = "META-INF/services/" + ActionProvider.class.getName(); - protected String actionsFileLocation = "META-INF/jaxx-%1$s-actions.properties"; + protected String actionsFileLocation = ActionProviderFromProperties.actionsFileLocation; protected boolean verbose; @@ -80,6 +80,10 @@ protected TypeElement baseActionElement; + protected String providerFQN; + + protected String baseFQN; + @Override public synchronized void init(ProcessingEnvironment processingEnv) { super.init(processingEnv); @@ -107,9 +111,16 @@ //fixme CheckbaseActionElement is assigned from MyAbstractAction //if (!baseActionElement.getSuperclass()) ... - providerConfig = baseActionElement.getAnnotation(ActionProviderAnnotation.class); + //providerConfig = baseActionElement.getAnnotation(ActionProviderAnnotation.class); - actionsFileLocation = String.format(actionsFileLocation, providerConfig.name()); + baseFQN = baseActionElement.asType().toString(); + int index = baseFQN.lastIndexOf("."); + String baseSimpleName = baseFQN.substring(index+1); + String packageName = baseFQN.substring(0, index+1); + + providerFQN = packageName + baseSimpleName + "Provider"; + printInfo("providerFQN "+packageName+","+baseSimpleName+","+providerFQN); + actionsFileLocation = String.format(actionsFileLocation, baseSimpleName); continue; } @@ -135,7 +146,7 @@ if ((roundEnv.processingOver())) { printDebug("round is over " + roundEnv); try { - if (providerConfig != null) { + if (baseActionElement != null) { // found the base action class to be compiled, so we have to write provider things... writeProviderClass(); writeProviderServiceDeclaration(); @@ -184,25 +195,6 @@ return f.getAbsolutePath().substring(index); } - /** - * @throws ClassNotFoundException ifclass not find - * @deprecated Can not use anylonger this class, beacause at compile time we are not garanted to have - * some actions classes compiled, so can not use {@link Class#forName(String)} method. - */ - @Deprecated - protected void findProviderConfigFromCompiledClass() throws ClassNotFoundException { - if (!actions.isEmpty()) { - Class<?> klazz = Class.forName(processedClass.get(0)); - providerConfig = klazz.getAnnotation(ActionProviderAnnotation.class); - while (klazz != null && providerConfig == null) { - klazz = klazz.getSuperclass(); - if (klazz != null) { - providerConfig = klazz.getAnnotation(ActionProviderAnnotation.class); - } - } - } - } - protected boolean registerActionsForClass(TypeMirror annotationType, Element e) { boolean doTreate = false; for (AnnotationMirror mirror : e.getAnnotationMirrors()) { @@ -228,24 +220,23 @@ protected void writeProviderClass() throws IOException, NotFoundException, CannotCompileException, ClassNotFoundException { OutputStream outputStream = null; try { - String fqn = providerConfig.fqn(); - + ClassPool pool = ClassPool.getDefault(); pool.appendClassPath(new LoaderClassPath(ActionProviderFromProperties.class.getClassLoader())); CtClass superClass = pool.get(ActionProviderFromProperties.class.getName()); - CtClass clazz = pool.makeClass(fqn); + CtClass clazz = pool.makeClass(providerFQN); // define the base action class in javassist pool to make possible compilation - pool.makeClass(baseActionElement.asType().toString()); + pool.makeClass(baseFQN); clazz.setSuperclass(superClass); // add constructor CtConstructor constructor = new CtConstructor(null, clazz); - constructor.setBody("super(\"" + providerConfig.name() + "\"," + baseActionElement.asType().toString() + ".class,\"/" + actionsFileLocation + "\");"); + constructor.setBody("super( " + baseFQN + ".class);"); clazz.addConstructor(constructor); byte[] byteCode = clazz.toBytecode(); - JavaFileObject fo = processingEnv.getFiler().createClassFile(fqn); + JavaFileObject fo = processingEnv.getFiler().createClassFile(providerFQN); printInfo("writing " + fo.toUri()); outputStream = fo.openOutputStream(); outputStream.write(byteCode); @@ -264,7 +255,7 @@ w = new BufferedWriter(fo.openWriter()); w.append("# generated by ").append(getClass().getName()).append("\n").toString(); w.append("#").append(new java.util.Date().toString()).append("\n").toString(); - w.append(providerConfig.fqn()); + w.append(providerFQN); } finally { if (w != null) w.close(); @@ -345,23 +336,9 @@ continue; } - /*Class<ActionNameHelper.ActionNameProvider> klazz = (Class<ActionNameHelper.ActionNameProvider>) entry.getValue().accept(getAnnotationValueExtractor(), null); - if (ActionNameHelper.CompileActionNameProvider.class.isAssignableFrom(klazz)) { - // means there is a compile time names provider - try { - result = klazz.newInstance().getActionCommands(); - break; - } catch (InstantiationException e) { - throw new RuntimeException(e); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - if (ActionNameHelper.RuntimeActionNameProvider.class.isAssignableFrom(klazz)) {*/ // means there is a runtime names provider result = new String[]{":" + classname}; break; - //} } if ("actionCommand".equals(name)) { result = new String[]{(String) entry.getValue().accept(getAnnotationValueExtractor(), null)}; Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProvider.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProvider.java 2008-08-10 15:18:09 UTC (rev 827) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProvider.java 2008-08-10 16:40:49 UTC (rev 828) @@ -23,9 +23,6 @@ */ public interface ActionProvider<A extends MyAbstractAction> { - /** @return the name of the provider */ - String getName(); - /** @return the base classe of provided actions */ Class<A> getBaseClass(); Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderAnnotation.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderAnnotation.java 2008-08-10 15:18:09 UTC (rev 827) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderAnnotation.java 2008-08-10 16:40:49 UTC (rev 828) @@ -31,11 +31,4 @@ @Target(ElementType.TYPE) public @interface ActionProviderAnnotation { - - /** @return le nom du provider */ - String name(); - - /** @return le nom qualifie duprovider a generer */ - String fqn(); - } Modified: trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java =================================================================== --- trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java 2008-08-10 15:18:09 UTC (rev 827) +++ trunk/lutinjaxx/jaxx-swing-action/src/main/java/org/codelutin/jaxx/action/ActionProviderFromProperties.java 2008-08-10 16:40:49 UTC (rev 828) @@ -30,30 +30,24 @@ /** default prefix for an entryin mapping file. */ protected static final String ACTION_KEY_PREFIX = "action."; + protected static final String actionsFileLocation = "META-INF/jaxx-%1$s-actions.properties"; + protected String propertiesPath; protected static Log log = LogFactory.getLog(ActionProviderFromProperties.class); - protected String name; - protected Class<A> baseClass; protected Map<String, Class<? extends A>> actions; - protected ActionProviderFromProperties(String name, Class<A> baseClass, String propertiesPath) { + protected ActionProviderFromProperties(Class<A> baseClass) { - this.name = name; this.baseClass = baseClass; - - this.propertiesPath = propertiesPath; + this.propertiesPath = "/" + String.format(actionsFileLocation, baseClass.getSimpleName()); this.actions = initCache(); } - public String getName() { - return name; - } - public Class<A> getBaseClass() { return baseClass; } @@ -64,7 +58,7 @@ @Override public String toString() { - return super.toString() + "<name:" + name + ", baseClass:" + baseClass.getSimpleName() + ">"; + return super.toString() + "<baseClass:" + baseClass.getSimpleName() + ">"; } protected void clearCache() { @@ -90,13 +84,16 @@ try { inputStream = getClass().getResourceAsStream(propertiesPath); if (inputStream == null) { - throw new NullPointerException("could not find action file " + propertiesPath); + //throw new NullPointerException("could not find action file " + propertiesPath); + // actually, there is nothing to load, this is not an error + } else { + log.info("load " + propertiesPath); + properties.load(inputStream); } - log.info("load " + propertiesPath); - properties.load(inputStream); } catch (IOException e) { - log.warn(_("jaxx.error.load.actions.file", e.getMessage())); - throw new RuntimeException(_("jaxx.error.load.actions.file", e.getMessage())); + String message = _("jaxx.error.load.actions.file", e.getMessage()); + log.warn(message); + throw new RuntimeException(message); } finally { if (inputStream != null) { try { @@ -128,12 +125,8 @@ } log.debug("found action <" + actionKey + " : " + implCass + ">"); cache.put(actionKey, implCass); - } catch (ClassNotFoundException e) { + } catch (Exception e) { throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); - } catch (IllegalAccessException e) { - throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); - } catch (InstantiationException e) { - throw new RuntimeException(_("jaxx.error.load.actions.class", key, qfn), e); } }