When I was writing Java, I sometimes wrote a test that needed to instantiate such a class.
@Value
public class Sample {
private final Integer a;
private final String b;
private final SomeClass c;
}
There are also some objects nested inside the SomeClass
, and just creating an instance of this class is quite exhausting. In addition, it is necessary to assign an initial value to each field, and I was thinking about an initial value that is not related to the test (it can be tested if there is an object) for each type. Since the same troubles were raised by the team members, I decided to do something with the IntelliJ plug-in using ʻIntention Action` this time.
To be honest, the details are all in the Official Reference, so I think you should read it. This is just an article I tried.
A convenient action that can be called on the IntelliJ Editor. The shortcut for Mac is Option (⌥) + Enter. I think it's Alt + Enter on Windows. More information can be found at Official Help.
A plugin that copies the description of instantiation that puts the initial value for each type in the class field to the clipboard. For example
@Value
public class Sample {
private final Integer a;
private final String b;
private final SomeClass c;
}
If you run it in the class of
new Sample(0, "", new SomeClass())
And copy the texto initialization instance to the clipboard.
I uploaded it to GitHub. There are various corrections.
Create Action you want to execute in Java with the name "~ Action".
public class CopyAction extends PsiElementBaseIntentionAction {
private static final String NEW_LINE;
private static final Map<String, String> initialValueLUT = new HashMap<>();
static {
initialValueLUT.put("boolean", "false");
initialValueLUT.put("Boolean", "false");
initialValueLUT.put("int", "0");
initialValueLUT.put("byte", "(byte)0");
initialValueLUT.put("Byte", "(byte)0");
initialValueLUT.put("Integer", "0");
initialValueLUT.put("String", "\"\"");
initialValueLUT.put("BigDecimal", "new BigDecimal(\"0\")");
initialValueLUT.put("Long", "0L");
initialValueLUT.put("long", "0L");
initialValueLUT.put("short", "(short)0");
initialValueLUT.put("Short", "(short)0");
initialValueLUT.put("Date", "new Date()");
initialValueLUT.put("float", "0.0F");
initialValueLUT.put("Float", "0.0F");
initialValueLUT.put("double", "0.0D");
initialValueLUT.put("Double", "0.0D");
initialValueLUT.put("Character", "\'\'");
initialValueLUT.put("char", "\'\'");
initialValueLUT.put("LocalDateTime", "LocalDateTime.now()");
initialValueLUT.put("LocalDate", "LocalDate.now()");
initialValueLUT.put("List", "[]");
NEW_LINE = System.getProperty("line.separator");
}
@Override
public void invoke(@NotNull Project project, Editor editor,
@NotNull PsiElement element) throws IncorrectOperationException {
if (element.getParent() instanceof PsiClass) {
final PsiClass psiClass = ((PsiClass) element.getParent());
final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
final String clipBoardText = makeText(psiClass.getName(), psiClass.getAllFields());
final StringSelection selection = new StringSelection(clipBoardText);
clipboard.setContents(selection, selection);
}
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor,
@NotNull PsiElement element) {
return true;
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return "";
}
@NotNull
@Override
public String getText() { return "Add Field to ClipBoard";}
private String makeText(final String className, final PsiField[] psiFields) {
String clipBoardText = "new " + className + "(" + NEW_LINE;
clipBoardText += Arrays.stream(psiFields)
.map(p -> "/* " + p.getName() + " */ " + Optional.ofNullable(initialValueLUT
.get(p.getTypeElement().getFirstChild().getFirstChild().getText()))
.orElse("new " + p.getType()
.toString()
.split(":")[1] + "()"))
.collect(
Collectors.joining("," + NEW_LINE));
clipBoardText += ")";
return clipBoardText;
}
}
plugin.xml
Since the action you want to perform this time is ʻIntention Action, describe the execution class in
plugin.xml by enclosing it in the ʻIntention Action
tag.
<extensions defaultExtensionNs="com.intellij">
<intentionAction>
<className>CopyAction</className>
</intentionAction>
</extensions>
Search for extension points from Plugin Extension Points (IntentionActions is LangExtentionPoints.xml (Around line 43 of .jetbrains.com/idea-ce/file/idea-ce-4f9b5f89b2a19ce700b1373a465c16b28ed8ad52/platform/platform-resources/src/META-INF/LangExtensionPoints.xml).
For the time being, I made a plug-in that copies the description of creating an instance to the clipboard while entering the initial value.
Recommended Posts