Created
September 25, 2014 20:53
-
-
Save gastaldi/20612117fb0daec85a0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.george.app; | |
| import java.io.FileNotFoundException; | |
| import java.util.logging.Logger; | |
| import org.jboss.forge.addon.parser.java.resources.JavaResource; | |
| import org.jboss.forge.addon.ui.command.AbstractCommandExecutionListener; | |
| import org.jboss.forge.addon.ui.command.UICommand; | |
| import org.jboss.forge.addon.ui.context.UIContext; | |
| import org.jboss.forge.addon.ui.context.UIExecutionContext; | |
| import org.jboss.forge.addon.ui.result.Result; | |
| import org.jboss.forge.roaster.model.JavaType; | |
| import org.jboss.forge.roaster.model.source.JavaClassSource; | |
| public class AppendLoggingCommandListener extends | |
| AbstractCommandExecutionListener { | |
| @Override | |
| public void postCommandExecuted(UICommand command, | |
| UIExecutionContext context, Result result) { | |
| UIContext uiContext = context.getUIContext(); | |
| for (Object selection : uiContext.getSelection()) { | |
| if (selection instanceof JavaResource) { | |
| JavaResource javaResource = (JavaResource) selection; | |
| JavaType<?> javaType; | |
| try { | |
| javaType = javaResource.getJavaType(); | |
| if (javaType.isClass()) { | |
| JavaClassSource javaSource = (JavaClassSource) javaType; | |
| if (!hasLogging(javaSource)) { | |
| addLogging(javaResource); | |
| } | |
| } | |
| } catch (FileNotFoundException e) { | |
| } | |
| } | |
| } | |
| } | |
| private boolean hasLogging(JavaClassSource javaSource) { | |
| return javaSource.hasField("LOG"); | |
| } | |
| private void addLogging(JavaResource javaResource) | |
| throws FileNotFoundException { | |
| JavaClassSource javaSource = javaResource.getJavaType(); | |
| javaSource | |
| .addField() | |
| .setPrivate() | |
| .setStatic(true) | |
| .setFinal(true) | |
| .setType(Logger.class) | |
| .setName("LOG") | |
| .setLiteralInitializer( | |
| String.format("Logger.getLogger(%s.class.getName())", | |
| javaSource.getQualifiedName())); | |
| javaResource.setContents(javaSource); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment