This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement comment inheritance for methods (#40)
- Loading branch information
1 parent
d09486e
commit 78fc76a
Showing
22 changed files
with
1,568 additions
and
4 deletions.
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
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,89 @@ | ||
package com.microsoft.util; | ||
|
||
import com.sun.source.doctree.DocTree; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.lang.model.element.Element; | ||
|
||
public class CommentHelper { | ||
public Element element; | ||
public List<? extends DocTree> inlineTags = Collections.emptyList(); | ||
private Utils utils; | ||
private boolean hasInheritDocTag = false; | ||
|
||
public CommentHelper(Element element, Utils utils) { | ||
this.element = element; | ||
this.utils = utils; | ||
this.inlineTags = utils.getFullBody(element); | ||
this.hasInheritDocTag = utils.hasInlineTag(inlineTags, DocTree.Kind.INHERIT_DOC); | ||
} | ||
|
||
public CommentHelper(Element element, Utils utils, List<? extends DocTree> inlineTags) { | ||
this.element = element; | ||
this.utils = utils; | ||
this.inlineTags = inlineTags; | ||
this.hasInheritDocTag = utils.hasInlineTag(inlineTags, DocTree.Kind.INHERIT_DOC); | ||
} | ||
|
||
/** | ||
* Returns true if the method has no comments, or a lone @inheritDoc. | ||
* | ||
* @return true if there are no comments, false otherwise | ||
*/ | ||
public boolean isSimpleOverride() { | ||
return inlineTags.isEmpty() || | ||
(inlineTags.size() == 1 && hasInheritDocTag); | ||
} | ||
|
||
public boolean hasInheritDocTag(){ | ||
return this.hasInheritDocTag; | ||
} | ||
|
||
public CommentHelper copy() { | ||
if (this.element == null) { | ||
throw new NullPointerException(); | ||
} | ||
CommentHelper clone = new CommentHelper(this.element, this.utils); | ||
return clone; | ||
} | ||
|
||
public CommentHelper inherit(CommentHelper chInheritFrom) { | ||
List<? extends DocTree> mergedTags = new ArrayList<>(); | ||
|
||
if (this.isSimpleOverride()) | ||
mergedTags = chInheritFrom.inlineTags; | ||
else { | ||
mergedTags = inheritInlineTags(this, chInheritFrom); | ||
} | ||
|
||
return new CommentHelper(this.element, this.utils, mergedTags); | ||
} | ||
|
||
List<? extends DocTree> inheritInlineTags(CommentHelper origin, CommentHelper chInheritFrom) { | ||
List<DocTree> mergedTags = new ArrayList<>(); | ||
if (!origin.isSimpleOverride() && !origin.hasInheritDocTag) { | ||
return origin.inlineTags; | ||
} | ||
|
||
// Get the index of "{@inheritedDoc}". | ||
int index = origin.inlineTags.stream().map(e -> e.getKind()) | ||
.collect(Collectors.toList()) | ||
.indexOf(DocTree.Kind.INHERIT_DOC); | ||
|
||
// Replace the "{@inheritedDoc}" with inherited inlineTags. | ||
mergedTags = origin.inlineTags.stream().collect(Collectors.toList()); | ||
mergedTags.remove(index); | ||
|
||
for (DocTree d : chInheritFrom.inlineTags | ||
) { | ||
mergedTags.add(index, d); | ||
index++; | ||
} | ||
|
||
return mergedTags; | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
src/test/java/com/microsoft/samples/commentinheritance/Animal.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,28 @@ | ||
package com.microsoft.samples.commentinheritance; | ||
|
||
/** | ||
* Animal. | ||
*/ | ||
public abstract class Animal implements Organism{ | ||
/** | ||
* Breathe. | ||
*/ | ||
public void breathe() { | ||
} | ||
|
||
/** | ||
* Communicate verbally. | ||
*/ | ||
public abstract void verballyCommunicate(); | ||
|
||
/** | ||
* Feed offspring. | ||
*/ | ||
public abstract void feed(); | ||
|
||
/** | ||
* {@inheritDoc} | ||
* Get kind from Animal. | ||
*/ | ||
public abstract String getKind(); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/com/microsoft/samples/commentinheritance/Carnivorous.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,18 @@ | ||
package com.microsoft.samples.commentinheritance; | ||
|
||
/** | ||
* Marks an Animal that eats other animals. | ||
*/ | ||
public interface Carnivorous { | ||
/** | ||
* Eat the provided animal. | ||
* | ||
* @param animalBeingEaten Animal that will be eaten. | ||
*/ | ||
void eat(Animal animalBeingEaten); | ||
|
||
/** | ||
* Get kind from Carnivorous. | ||
*/ | ||
String getKind(); | ||
} |
71 changes: 71 additions & 0 deletions
71
src/test/java/com/microsoft/samples/commentinheritance/Dog.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,71 @@ | ||
package com.microsoft.samples.commentinheritance; | ||
|
||
import java.awt.Color; | ||
|
||
import static java.lang.System.out; | ||
|
||
/** | ||
* Canine and man's best friend. | ||
*/ | ||
public class Dog extends Mammal implements Omnivorous, Viviparous { | ||
private final Color hairColor = null; | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param otherAnimal Tasty treat. | ||
*/ | ||
@Override | ||
public void eat(final Animal otherAnimal) { | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param plantToBeEaten Plant that this dog will eat. | ||
*/ | ||
@Override | ||
public void eat(final Plant plantToBeEaten) { | ||
} | ||
|
||
/** | ||
* {@inheritDoc} Bark. | ||
*/ | ||
public void verballyCommunicate() { | ||
out.println("Woof!"); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param numberPuppies Number of puppies being born. | ||
*/ | ||
@Override | ||
public void giveBirth(final int numberPuppies) { | ||
} | ||
|
||
/** | ||
* Provide the color of the dog's hair. | ||
* | ||
* @return Color of the dog's fur. | ||
*/ | ||
public Color getHairColor() { | ||
return hairColor; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void feed() { | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
* Get kind from Dog. | ||
*/ | ||
@Override | ||
public String getKind() { | ||
return "Dog"; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/com/microsoft/samples/commentinheritance/Herbivorous.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,22 @@ | ||
package com.microsoft.samples.commentinheritance; | ||
|
||
/** | ||
* Marks animals that eat plants. | ||
*/ | ||
public interface Herbivorous { | ||
/** | ||
* Eat the provided plant. | ||
* | ||
* @param plantToBeEaten Plant that will be eaten. | ||
*/ | ||
void eat(Plant plantToBeEaten); | ||
|
||
/** | ||
* Get kind from Herbivorous. | ||
*/ | ||
String getKind(); | ||
|
||
public class Plant { | ||
} | ||
|
||
} |
Oops, something went wrong.