Writing a comment in Eclipse linking a specific line - java

i'm working with Eclipse in Java and with long long classes i need a feature like this: in the top comment of a method (for example) there is a list of operations executed by the method.
For each operation listed, i'd like to "hyperlink" a portion of the comment to a specific line of the related code.
Then using Ctrl+Click to that line i can jump directly to the specified line code.
Is it possible an operation like this?
Thanks

In the comment below your question you say:
how can i link methods?
Take a look at the following example: you can press ctrl + click on bar() within the JavaDoc of foo() and eclipse jumps to the method bar().
public class Example {
/**
* JavaDoc of foo(). This method executes {#link Example#bar()}
*/
public void foo() {
bar();
}
/**
* Javadoc of bar().
*/
public void bar() { }
}
Eclipse even offers autocomplete for #link, the classname and the method (after you manually entered the #).
Is that what you are looking for?

You can use the JavaDoc #see tag:
/**
* #see MyClass#myMethod()
*/
This generates a hyperlink in your JavaDoc.
SRC: method-linking-anchoring-in-java

The Eclipse IDE allows you to go from a method call to the method's definition ('F3' I think).
Apart from that, I don't think there's a way to set up "special" navigation. Mind you, if you need something like that, it is a strong indication that your methods are WAY too large. Refactor them.
Thinking outside of the box, if you were to feed your code through a code-to-html pretty printer, you could embed HTML hyperlinks and anchors in comments (javadoc or normal). With a bit of luck, they would be clickable when you viewed the HTML-ized source code in a web browser.
Of course, Eclipse can follow javadoc "links". Obviously the standard tags can't link to deep inside a method, but I guess you could write an Eclipse plugin that supported non-standard javadoc tags for linking to embedded anchors, and the navigation thereof.

Related

how to change a method signature dynamically in java any utiltiy

I wanted to refactor a method signature of a java class using utility classes E.g.:
Test.java
public class{
public void test(int a, int b) {
.....
....
}
}
In my entire project, I wanted to change the methods with the above signature to a different one like below:
public void test(String str) {
..
}
without opening all the files and rewriting them.
antlr is used to convert code written in one language to another language.
I wanted to do these kind of refactoring using java utility kind of class where i will provide my input class as argument and refactoring patters as another argument.i have explored using codemodel and eclipse AST but no luck.
In Eclipse put your blinking cursor onto the method and hit ctrl+shift+r. This allows you to rename methods, variables etc -- whatever you have selected in the entire project.
EDIT: I misread your question.
You need to "change method signature". Most IDEs have similar features e.g., IntelliJ -- Right Click -- Refactor - Change Signature. If you do not have an IDE then you will have to write a script or manually change all the calls.
if IDE provided refactoring is not option then I use Notepad++, it allows you to find and replace things without opening files, by selecting parent directory. I hope that will help you as well.

Is there any difference between these multi-line comments in Java?

I just wanted to know if there are any differences between these types of comments.
/*
...
Content of the comment
...
*/
and
/*
* ...
* Content of the comment
* ...
*/
No, there is no difference. It's purely a style decision.
No difference, but this syntax
/*
* ...
* Content of the comment
* ...
*/
is more appropriate for generating JavaDoc
From http://javadude.com/articles/comments.html
Documentation comments should (at very least) be used in front of
every public class, interface, method and class/instance variable in
your source code. This allows someone to run javadoc against the code
and generate a simple document that lists the public entities and a
brief description of each. You may also use documentation comments in
front on non-public methods, and use a javadoc option to generate
documentation for them. Using documentation comments on non-public
entities is not as important as publics (the interface isn't
exposed...) but if you're commenting the code anyway you might as well
write those comments as documentation comments.
No it makes no difference.
Side Note (for the sake of completeness)
There is a difference between the following to examples.
/*
* ...
* Content of the comment
* ...
*/
and
/**
* ...
* Content of the comment
* ...
*/
the second one indicates a javadoc comment (the ** at the first line of the comment). There you can use different tags to enhance your javadoc.
There isn't any difference. Just a different way.
I'd prefer the first form:
/*
something
*/
Specially if the commented out part is code and if I am trying something out. For obvious reasons, extra asterixes at the beginning of each line causes head aches for this case.
Modern IDE's also support short-cuts for commenting multiple lines at the same time. For example in Eclipse, you can select a few lines and press CTRL + / to comment them out. The form of comments is though would be as follows:
// line1
// line2
// ...
There is no difference
As a developer What ever the code we are writing we need to make a small document on top of that particular code.for documenting purpose we will use these comments based on our choice we can use what ever we want.

Distinguishing internal/external methods for Javadoc

I am writing a library in Java. I've divided its implementation into Java packages to help manage the complexity. Only one package contains classes that are visible to clients of the library. However, because only public methods are visible outside of the package for use by other packages of the library, I find myself forced to do one of the following:
(1) Only put interfaces and factory methods in the externally-visible package, putting implementations of those interfaces in a separate package, as described in this SO answer. For example external.MyInterface and internal.MyInterfaceImpl. I find this messy.
(2) Make both internal and external methods public in the external package, and attach Javadoc tags to the internal methods so I can remove their docs prior to publication, either manually (error-prone) or by writing some sort of Javadoc preprocessor or postprocessor.
(3) Use a mechanism that Javadoc provides for this purpose -- ideally, a Javadoc tag.
Whatever the approach, all I really care about is having a consistent way to automatically generate Javadocs for just the external APIs. Is there a standard way to do this? A tool for the purpose?
An alternative solution I've been using for years is to add an #exclude tag, using the public domain code provided in this blog: Implementing #exclude using Dynamic Proxies.
To exclude a Java element (attribute, method, constructor, class, inner class or package) from the Javadoc output, just add the #exclude tag in its Javadoc:
public class MyClass {
/**
* This is my internal attribute, javadoc not exposed.
* #exclude
*/
protected String myInternalAttribute;
/**
* This is my external attribute, javadoc is exposed.
*/
protected String myExternalAttribute;
/**
* This is my internal method, javadoc not exposed.
* #exclude
*/
public void myInternalMethod() { }
/**
* This is my external method, javadoc is exposed.
*/
public void myExternalMethod() { }
}
I found these two answers elsewhere on SO. One approach is to create a custom Javadoc annotation and have an Ant task replace the annotation with deprecated prior to generating the Javadoc. The other, far simpler approach is to use Doxygen's conditional inclusion.
I'm not stuck with Javadoc, so I could go with Doxygen. However, looking at Doxygen right now, it's so different from Javadoc that I'm not sure it's worth the learning curve or establishing a precedent just to be able to generate external APIs.
Here's another solution I will try next time I'm in a position to build: I'll demarcate the portions of the source files that are internal-only, write a tool that duplicates the source files of the external package while removing the portions of the files that are demarcated internal-only, and then run Javadoc off of the generated source. This should work unless Javadoc needs the linker to be happy.
I don't know if it's worth keeping my question around. Might help others find the answer, should they be thinking about it the way I was. Even so, no one has presented a great solution yet.

Inline comments in Java: /** opposed to /*?

is there a reason i should prefer to write inline-comments in java like this:
/** Init operation */
mindControlLaser.engage();
as opposed to use just one *:
/* i'm a happy comment */
Eclipse colours the syntax differently, but is there really anything in the "toolchain" (javadoc, eclipse, etc.) giving me an advantage when using /** */ ?
No reason for inline comments.
/** signals to javadoc utility to extract documentation about your API automatically. It does not have any effect when is used inside methods.
Regular comments
/* Regular comment */
With regular comments you explain maybe a part of an algorithm that is tricky.
Or anything that you don't want to be a part of the JavaDOC. Inline comments are regular comments too, and can be used for example when the description is shorter.
Java Documentation
/** JAVA DOC COMMENT */
With javadoc you explain classes, methods, or fields(variables).
Then, most IDEs like Eclipse can use this information to help you while you code.
For example, if you have a classA and a classB, and in classB you use stuff from classA, then if you hover on methods or variables you can see the JavaDOC information. It's very handy.
Also, with build tools like ant you can automatically build HTML files out of the JavaDOC, and if you publish them you can allow others to reuse your work.
Look for example the documentation of Java itself here.
The syntax for a comment is /* */.
Javadoc has as a default that you use /** */. This is a comment because the second * is inside the comment, so would not be seen differently by your compiler.
So without a second * you are just adding a comment, and with the second one you write javadoc: eclipse will recognize it and give you hints etc when hovering on the functioncall somewhere else.
/** .... */ will generate Javadoc, /* ... */ won't.
Of course, it will generate Javadoc only when in the correct places. Javadoc also has a pretty well defined format, see here.
The /** denotes "documentation" comments; Javadocs etc. look for these when creating documentation for your code.
So they should really only be used above methods and classes, e.g.:
/**
* Class to represent tigers.
*/
class Tiger {
/**
* Go extinct.
*/
void goExtinct() {
}
}
The /* variant just denotes a standard comment block.
Yep, it's the javadoc notation to use /** Primary sentence. Other descriptions... */. First sentece up to the . will be used in summaries of javadoc and the rest in the detailed view.
Javadoc treats /** differently; classes and methods which have /** comments above them will get put into javadoc output.
If you use the reference formatting (e.g. {#link ClassA}) and rename the class ClassA with Eclipse, it will automatically update the comment if it is a javadoc comment.

The -nodeprecated option in javadoc doesn't seem to work. What can I do?

I have a deprecated method in my class:
#Deprecated
public void deprecatedMethod() {
//do bad things
}
I don't want that method to appear in the javadoc.
I know there's an option called -nodeprecated which:
"Prevents the generation of any
deprecated API at all in the
documentation."
So I'm using this option and it doesn't exclude the method from javadoc. Is it a bug in javadoc or am I using it wrong? What else can I do?
(I'm using eclipse 3.4.2 to produce javadoc)
You have to include "-nodeprecated" option in the Export to javadoc wizard.
Warning: it is a javadoc option, not a VM option.
I've tested it in Eclipse 3.4 and it worked.
Edit: If you only include Deprecated annotation it doesn't work. You have to include #deprecated tag inside method javadoc as well.
I don't know if there's a way to tell javadoc to use #Deprecated anotation (which curiously doesn't have a message parameter to document why is deprecated and what else to use).
Edit: before-1.5 way of deprecate methods
You have to include a #deprecated tag (or indicator or whatever) with the message you want to display to the user in the javadoc after the "deprecated".
/**
This method sets the property A.
#see getA
#author helios
#deprecated This method is not sync safe, use setAOk instead
*/
public void setA(String value) ...
#helios
john is saying that you must include the #deprecated javadoc tag within the javadoc comment block (/** ... */) as he has done above with:
#deprecated This method is not sync safe, use setAOk instead
Add this, then use the -nodeprecated option when running javadoc and the methods will not appear in the generated doc.

Categories