New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FORGE-1863: Create a GenerateHashcodeEquals command inside the dev-tools... #470
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
173 changes: 173 additions & 0 deletions
173
...ols-java/src/main/java/org/jboss/forge/addon/devtools/java/JavaEqualsHashcodeCommand.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.devtools.java; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
|
||
import org.jboss.forge.addon.facets.constraints.FacetConstraint; | ||
import org.jboss.forge.addon.parser.java.beans.ProjectOperations; | ||
import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet; | ||
import org.jboss.forge.addon.parser.java.resources.JavaResource; | ||
import org.jboss.forge.addon.projects.Project; | ||
import org.jboss.forge.addon.projects.ProjectFactory; | ||
import org.jboss.forge.addon.projects.facets.ResourcesFacet; | ||
import org.jboss.forge.addon.projects.ui.AbstractProjectCommand; | ||
import org.jboss.forge.addon.ui.context.UIBuilder; | ||
import org.jboss.forge.addon.ui.context.UIContext; | ||
import org.jboss.forge.addon.ui.context.UIExecutionContext; | ||
import org.jboss.forge.addon.ui.facets.HintsFacet; | ||
import org.jboss.forge.addon.ui.hints.InputType; | ||
import org.jboss.forge.addon.ui.input.InputComponentFactory; | ||
import org.jboss.forge.addon.ui.input.UIPrompt; | ||
import org.jboss.forge.addon.ui.input.UISelectMany; | ||
import org.jboss.forge.addon.ui.input.UISelectOne; | ||
import org.jboss.forge.addon.ui.metadata.UICommandMetadata; | ||
import org.jboss.forge.addon.ui.result.Result; | ||
import org.jboss.forge.addon.ui.result.Results; | ||
import org.jboss.forge.addon.ui.util.Categories; | ||
import org.jboss.forge.addon.ui.util.Metadata; | ||
import org.jboss.forge.furnace.Furnace; | ||
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer; | ||
import org.jboss.forge.roaster.model.source.FieldSource; | ||
import org.jboss.forge.roaster.model.source.JavaClassSource; | ||
import org.jboss.forge.roaster.model.util.Refactory; | ||
|
||
/** | ||
* Command generating the Equals and HashCode commands. | ||
* | ||
* @author <a href="mailto:mbriskar@gmail.com">Matej Briškár</a> | ||
*/ | ||
@FacetConstraint({ JavaSourceFacet.class, ResourcesFacet.class }) | ||
public class JavaEqualsHashcodeCommand extends AbstractProjectCommand | ||
{ | ||
|
||
private UISelectOne<JavaResource> targetClass; | ||
|
||
private UISelectMany<String> fields; | ||
|
||
private InputComponentFactory inputFactory; | ||
|
||
private ProjectFactory projectFactory; | ||
|
||
private ProjectOperations projectOperations; | ||
|
||
public JavaEqualsHashcodeCommand() | ||
{ | ||
Furnace furnace = SimpleContainer.getFurnace(this.getClass().getClassLoader()); | ||
this.inputFactory = furnace.getAddonRegistry().getServices(InputComponentFactory.class).get(); | ||
this.projectFactory = furnace.getAddonRegistry().getServices(ProjectFactory.class).get(); | ||
this.projectOperations = furnace.getAddonRegistry().getServices(ProjectOperations.class).get(); | ||
} | ||
|
||
@Override | ||
public UICommandMetadata getMetadata(UIContext context) | ||
{ | ||
return Metadata.forCommand(getClass()).name("Java: Generate Equals and HashCode") | ||
.description("Generates equals and hashcode for the given class") | ||
.category(Categories.create("Java")); | ||
} | ||
|
||
@Override | ||
public void initializeUI(UIBuilder builder) throws Exception | ||
{ | ||
UIContext uiContext = builder.getUIContext(); | ||
Project project = getSelectedProject(uiContext); | ||
targetClass = inputFactory.createSelectOne("targetClass", JavaResource.class); | ||
targetClass.setDescription("The class where the field will be created"); | ||
targetClass.setRequired(true); | ||
targetClass.setValueChoices(projectOperations.getProjectClasses(project)); | ||
|
||
fields = inputFactory.createSelectMany("fields", String.class); | ||
fields.setDescription("Fields, which should be used in the hashCode/equals method generation"); | ||
fields.setRequired(true); | ||
|
||
fields.setValueChoices(new Callable<Iterable<String>>() | ||
{ | ||
@Override | ||
public Iterable<String> call() throws Exception | ||
{ | ||
List<String> strings = new ArrayList<>(); | ||
if(!fields.isEnabled()) { | ||
return strings; | ||
} | ||
JavaResource javaResource = targetClass.getValue(); | ||
JavaClassSource targetClass = javaResource.getJavaType(); | ||
List<FieldSource<JavaClassSource>> fields = targetClass.getFields(); | ||
for (FieldSource<JavaClassSource> field : fields) | ||
{ | ||
strings.add(field.getName()); | ||
} | ||
return strings; | ||
} | ||
}); | ||
fields.setEnabled(new Callable<Boolean>() | ||
{ | ||
@Override | ||
public Boolean call() | ||
{ | ||
return (targetClass.getValue() != null); | ||
} | ||
}); | ||
builder.add(targetClass).add(fields); | ||
} | ||
|
||
@Override | ||
public Result execute(UIExecutionContext context) throws Exception | ||
{ | ||
JavaResource javaResource = targetClass.getValue(); | ||
JavaClassSource targetClass = javaResource.getJavaType(); | ||
List<FieldSource<JavaClassSource>> selectedFields = new ArrayList<>(); | ||
for(String fieldString : fields.getValue()) { | ||
selectedFields.add(targetClass.getField(fieldString)); | ||
} | ||
UIPrompt prompt = context.getPrompt(); | ||
if(targetClass.hasMethodSignature("equals", Object.class)) { | ||
if(prompt.promptBoolean("Class already has an equals method. Would you like it to be overwritten?")) { | ||
Refactory.createEquals(targetClass, selectedFields.toArray(new FieldSource<?>[selectedFields.size()])); | ||
} | ||
} else { | ||
Refactory.createEquals(targetClass, selectedFields.toArray(new FieldSource<?>[selectedFields.size()])); | ||
} | ||
if(targetClass.hasMethodSignature("hashcode")) { | ||
if(prompt.promptBoolean("Class already has a hashcode method. Would you like it to be overwritten?")) { | ||
Refactory.createHashCode(targetClass, selectedFields.toArray(new FieldSource<?>[selectedFields.size()])); | ||
} | ||
} else { | ||
Refactory.createHashCode(targetClass, selectedFields.toArray(new FieldSource<?>[selectedFields.size()])); | ||
} | ||
setCurrentWorkingResource(context, targetClass); | ||
return Results.success("Command for generation ended successfully"); | ||
|
||
} | ||
|
||
private void setCurrentWorkingResource(UIExecutionContext context, JavaClassSource javaClass) | ||
throws FileNotFoundException | ||
{ | ||
Project selectedProject = getSelectedProject(context); | ||
if (selectedProject != null) | ||
{ | ||
JavaSourceFacet facet = selectedProject.getFacet(JavaSourceFacet.class); | ||
facet.saveJavaSource(javaClass); | ||
} | ||
context.getUIContext().setSelection(javaClass); | ||
} | ||
|
||
@Override | ||
protected boolean isProjectRequired() | ||
{ | ||
return true; | ||
} | ||
|
||
@Override | ||
protected ProjectFactory getProjectFactory() | ||
{ | ||
return projectFactory; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...ava/src/main/resources/META-INF/services/org.jboss.forge.furnace.container.simple.Service
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
org.jboss.forge.addon.devtools.java.RegisterAsServiceCommand | ||
org.jboss.forge.addon.devtools.java.RegisterAsServiceCommand | ||
org.jboss.forge.addon.devtools.java.JavaEqualsHashcodeCommand | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
/* | ||
* Copyright 2013 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed under the Eclipse Public License version 1.0, available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.jboss.forge.addon.devtools.java; | ||
|
||
import javax.inject.Inject; | ||
|
@@ -14,7 +20,7 @@ | |
import org.junit.runner.RunWith; | ||
|
||
@RunWith(Arquillian.class) | ||
public class RegisterAsServiceCommandTest | ||
public class DevToolsPresenceTest | ||
{ | ||
@Deployment | ||
@Dependencies({ | ||
|
@@ -34,11 +40,17 @@ public static ForgeArchive getDeployment() | |
|
||
@Inject | ||
private RegisterAsServiceCommand command; | ||
|
||
@Inject | ||
private JavaEqualsHashcodeCommand ehCommand; | ||
|
||
@Test | ||
public void testCommandInjection() throws Exception | ||
{ | ||
Assert.assertNotNull(command); | ||
Assert.assertNotNull(command.toString()); | ||
|
||
Assert.assertNotNull(ehCommand); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent. |
||
Assert.assertNotNull(ehCommand.toString()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent!