-
Sub-task
-
Resolution: Done
-
Major
-
2.19.0.Final
-
None
The new commands are straight forward, we just create a new artifact (mostly classes in the Java EE addon). add commands are meant to add code to existing code. So all the add}} commands have a targetClass attribute. The targetClass points to the current class, or can point to a different one.
The algorithm to get the targetClass is very different from command to command (see below). It would be very helpfull to find a generic design pattern that could be applicable to most of the add commands.
JavaEqualsHashcodeCommand
@Override public void initializeUI(UIBuilder builder) throws Exception { (...) UISelection<Object> initialSelection = uiContext.getInitialSelection(); if (initialSelection.get() instanceof JavaResource) { targetClass.setValue((JavaResource) initialSelection.get()); } targetClass.setValueChoices(projectOperations.getProjectClasses(project)); @Override public Result execute(UIExecutionContext context) throws Exception { (...) if (targetClass.hasMethodSignature("equals", Object.class)) { if (prompt.promptBoolean("Class already has an equals method. Would you like it to be overwritten?"))
CDIAddInjectionPointCommand
private void setupTargetClasses(UIContext uiContext) { UISelection<FileResource<?>> selection = uiContext.getInitialSelection(); Project project = getSelectedProject(uiContext); final List<JavaResource> classes = cdiOperations.getProjectInjectionPointBeans(project); targetClass.setValueChoices(classes); int idx = -1; if (!selection.isEmpty()) { idx = classes.indexOf(selection.get()); } if (idx == -1) { idx = classes.size() - 1; } if (idx != -1) { targetClass.setDefaultValue(classes.get(idx)); } }
CDIAddObserverMethodCommand
private void setupTargetClass(UIContext uiContext, Project project) { UISelection<Resource<?>> resource = uiContext.getInitialSelection(); if (resource.get() instanceof JavaResource) { targetClass.setDefaultValue((JavaResource) resource.get()); } targetClass.setValueChoices(projectOperations.getProjectClasses(project)); }
FacesNewValidatorMethodCommand
public void initializeUI(UIBuilder builder) throws Exception { Object selection = builder.getUIContext().getInitialSelection().get(); if (selection instanceof JavaResource) { targetClass.setDefaultValue((JavaResource) selection); } builder.add(targetClass).add(named); } @Override public void validate(UIValidationContext validator) { try { JavaClassSource source = targetClass.getValue().reify(JavaResource.class).getJavaType(); (...) } catch (FileNotFoundException e) { validator.addValidationError(targetClass, "Target Java class not found."); } }
- relates to
-
FORGE-2331 Refactor New and Add commands
- Open