1

I want to generate Javadoc every creation of new file with class name as words, meaning separated by space before each uppercase, for example

When creating class NewJavaClass.java it will create java docs as:

/**
 * New Java Class 
 *
/*
public class NewJavaClass 
codeLover
  • 2,571
  • 1
  • 11
  • 27
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • Please check this link: https://stackoverflow.com/questions/4468669/how-to-generate-javadoc-html-files-in-eclipse – codeLover Jul 12 '18 at 06:34
  • @GarimaGupta can I use eclipse's code templates? – Ori Marko Jul 12 '18 at 06:40
  • According to my understanding yes.. – codeLover Jul 12 '18 at 06:41
  • 1
    Ahem, why do you want to do that? So that you have to remember to update these words each time you refactor your classes and rename some of them? Seriously: comments should *document* the things that aren't obvious. The name of the class is right there, what advantage do you have from "wording" it into the javadoc?! – GhostCat Jul 12 '18 at 07:00
  • 1
    @GhostCat you __may__ be right, but I want to manipulate class name in other ways, as if class name end with DAO enter similar text with TODO comment – Ori Marko Jul 12 '18 at 08:32
  • 1
    I see. For the record: i think with intellij, you should be able to do such think, but for eclipse, I only know about their "templates", and I am not aware of any "dynamic" features around templates.IntelliJ allows you to run specific code when generating something, no idea about eclipse. – GhostCat Jul 12 '18 at 09:03
  • @GhostCat For the record, Java code convention is to add to file `/* * Classname` comment http://www.oracle.com/technetwork/java/codeconventions-150003.pdf – Ori Marko Jul 16 '18 at 05:49
  • 1
    @user7294900 Yes. But it is also good convention to use comments for meaningful things. Repeating information that can be found a bit further down in source code is not meaningful. Generating comments once from names is, as said, breaking as soon as you refactor names. Therefore it will sooner or later lead to misinformation, aka: comments lying to the reader. – GhostCat Jul 16 '18 at 06:49

2 Answers2

1

Try this:

  1. Go to Window > Preferences.
  2. Type code templates on the left top search box.
  3. Go to Java Code Templates.

enter image description here

  1. On the right side select Code > New Java files then click Edit.
  2. Now You can paste the code that you want while creating new java file. Don't forget to save. Now you can check by creating new java file.
Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26
  • @user7294900 try this /** * New Java Class * ${file_name} * */ Here file_name will automatically set the file name here in comment section – Nitin Bisht Jul 12 '18 at 08:36
  • @user7294900 check my updated answer with updated image and let me know whether It worked for you. – Nitin Bisht Jul 12 '18 at 08:40
  • I meant to manipulate ${file_name} variable to change _dynamically_ the value with space between upper case characters – Ori Marko Jul 12 '18 at 08:59
  • @user7294900 You update your question with sample example what you want exactly then I will be able to help you. Because I did not get what do you mean by uppercase character here. – Nitin Bisht Jul 12 '18 at 09:00
  • He wants to **generate** that first line based on the class name. So when the class is named "FooBar", the javadoc should say `/** Foo Bar` ... at least that is what I understand. – GhostCat Jul 12 '18 at 09:02
  • @GhostCat but This is totally illogical. Isn't it??? Class Name should be exactly same in javadoc also – Nitin Bisht Jul 12 '18 at 09:03
  • I already expressed my concern regarding the usefulness of this ideas as comment to the question ;-) – GhostCat Jul 12 '18 at 09:03
  • @user7294900 In eclipse It is not possible because you can't create a class including space in eclipse. Hence No such feature is available in eclipse. – Nitin Bisht Jul 12 '18 at 09:28
  • The space (or string manipulation of class name) is saved inside __comments__ / __Javadocs__ – Ori Marko Jul 12 '18 at 09:31
  • @user7294900 This feature must be available in any other editor but It is not available in eclipse. – Nitin Bisht Jul 12 '18 at 09:46
1

Code templates are limited to simple variable substitution. Some of variables do accept arguments to manipulate the output in certain ways (e.g. currentDate can be configured with a date format pattern) but none of the built-in variables allow you to do complex string manipulation. You most certainly need a plugin to achieve what you want.

You should look for plugins that let you define more complex class templates. After a quick search I found the FastCode Eclipse plugin which looks like it allows you to create custom templates in some template language. The screenshots look quite dated though and the website doesn't say which Eclipse versions are supported.

You can also write a plugin yourself. It should be possible to provide some simple custom variables for templates. Or write a plugin that provides a file creation wizard which gives you complete control over the generated class. Another option would be to write a plugin that extends the editor with custom content assist (code completion) to generate code or documentation in existing files.

kapex
  • 28,903
  • 6
  • 107
  • 121