Refine groovydoc support further (cherry picked from commit 4306747)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f7e2c46a Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f7e2c46a Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f7e2c46a Branch: refs/heads/GROOVY_2_6_X Commit: f7e2c46a0b3e3b91247166114067dd7f990efcfb Parents: ba37b01 Author: sunlan <sun...@apache.org> Authored: Sun Sep 10 21:16:40 2017 +0800 Committer: sunlan <sun...@apache.org> Committed: Sun Sep 10 22:46:59 2017 +0800 ---------------------------------------------------------------------- src/main/groovy/lang/groovydoc/Groovydoc.java | 96 ++++++++++++++++++++ .../groovy/lang/groovydoc/GroovydocHolder.java | 33 +++++++ .../groovy/lang/groovydoc/GroovydocTag.java | 68 ++++++++++++++ src/main/org/codehaus/groovy/ast/ClassNode.java | 9 +- src/main/org/codehaus/groovy/ast/FieldNode.java | 9 +- .../org/codehaus/groovy/ast/MethodNode.java | 9 +- .../groovy/ast/groovydoc/Groovydoc.java | 68 -------------- .../groovy/ast/groovydoc/GroovydocHolder.java | 31 ------- .../groovy/ast/groovydoc/GroovydocTag.java | 74 --------------- .../groovy/runtime/DefaultGroovyMethods.java | 4 +- .../groovy/parser/antlr4/GroovydocManager.java | 4 +- .../test/resources/core/Groovydoc_01x.groovy | 12 +-- 12 files changed, 228 insertions(+), 189 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/groovy/lang/groovydoc/Groovydoc.java ---------------------------------------------------------------------- diff --git a/src/main/groovy/lang/groovydoc/Groovydoc.java b/src/main/groovy/lang/groovydoc/Groovydoc.java new file mode 100644 index 0000000..7b281ff --- /dev/null +++ b/src/main/groovy/lang/groovydoc/Groovydoc.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package groovy.lang.groovydoc; + +import java.lang.reflect.AnnotatedElement; +import java.util.List; +import java.util.Objects; + +/** + * Represents groovydoc + */ +public class Groovydoc { + private final String content; + private List<GroovydocTag> tagList; + private final GroovydocHolder groovydocHolder; + + public Groovydoc(String content, GroovydocHolder groovydocHolder) { + this.content = content; + this.groovydocHolder = groovydocHolder; + } + + public Groovydoc(final String content, final AnnotatedElement annotatedElement) { + this.content = content; + this.groovydocHolder = new GroovydocHolder() { + @Override + public Groovydoc getGroovydoc() { + return Groovydoc.this; + } + + @Override + public Object getInstance() { + return annotatedElement; + } + }; + } + + /** + * Get the content of groovydoc + * @return the text content + */ + public String getContent() { + return content; + } + + /** + * TODO Get list of groovydoc tags + * @return a list of tags + */ + public List<GroovydocTag> getTagList() { + throw new UnsupportedOperationException("[TODO]parsing tags will be a new features of the next releases"); +// return tagList; + } + + /** + * Get the holder of the groovydoc + * @return the groovydoc holder + */ + public GroovydocHolder getGroovydocHolder() { + return groovydocHolder; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Groovydoc groovydoc = (Groovydoc) o; + return Objects.equals(content, groovydoc.content) && + Objects.equals(groovydocHolder, groovydoc.groovydocHolder); + } + + @Override + public int hashCode() { + return Objects.hash(content, groovydocHolder); + } + + @Override + public String toString() { + return this.content; + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/groovy/lang/groovydoc/GroovydocHolder.java ---------------------------------------------------------------------- diff --git a/src/main/groovy/lang/groovydoc/GroovydocHolder.java b/src/main/groovy/lang/groovydoc/GroovydocHolder.java new file mode 100644 index 0000000..8db3135 --- /dev/null +++ b/src/main/groovy/lang/groovydoc/GroovydocHolder.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package groovy.lang.groovydoc; + +/** + * Represents Groovydoc Holder + */ +public interface GroovydocHolder { + String DOC_COMMENT = "_DOC_COMMENT"; // keys for meta data + /** + * Get the groovydoc + * @return the groovydoc + */ + Groovydoc getGroovydoc(); + + Object getInstance(); +} http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/groovy/lang/groovydoc/GroovydocTag.java ---------------------------------------------------------------------- diff --git a/src/main/groovy/lang/groovydoc/GroovydocTag.java b/src/main/groovy/lang/groovydoc/GroovydocTag.java new file mode 100644 index 0000000..14e5aaa --- /dev/null +++ b/src/main/groovy/lang/groovydoc/GroovydocTag.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package groovy.lang.groovydoc; + +import java.util.Objects; + +/** + * TODO parse groovydoc to get tag content + */ +public class GroovydocTag { + private String name; + private String content; + private Groovydoc groovydoc; + + public GroovydocTag(String name, String content, Groovydoc groovydoc) { + this.name = name; + this.content = content; + this.groovydoc = groovydoc; + } + + public String getName() { + return name; + } + + public String getContent() { + return content; + } + + public Groovydoc getGroovydoc() { + return groovydoc; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + GroovydocTag that = (GroovydocTag) o; + return Objects.equals(name, that.name) && + Objects.equals(content, that.content) && + Objects.equals(groovydoc, that.groovydoc); + } + + @Override + public int hashCode() { + return Objects.hash(name, content, groovydoc); + } + + @Override + public String toString() { + return content; + } +} http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/org/codehaus/groovy/ast/ClassNode.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/ast/ClassNode.java b/src/main/org/codehaus/groovy/ast/ClassNode.java index 7e24de2..fcf7cd6 100644 --- a/src/main/org/codehaus/groovy/ast/ClassNode.java +++ b/src/main/org/codehaus/groovy/ast/ClassNode.java @@ -18,13 +18,13 @@ */ package org.codehaus.groovy.ast; +import groovy.lang.groovydoc.GroovydocHolder; import org.codehaus.groovy.GroovyBugError; import org.codehaus.groovy.ast.expr.BinaryExpression; import org.codehaus.groovy.ast.expr.Expression; import org.codehaus.groovy.ast.expr.FieldExpression; import org.codehaus.groovy.ast.expr.TupleExpression; -import org.codehaus.groovy.ast.groovydoc.Groovydoc; -import org.codehaus.groovy.ast.groovydoc.GroovydocHolder; +import groovy.lang.groovydoc.Groovydoc; import org.codehaus.groovy.ast.stmt.BlockStatement; import org.codehaus.groovy.ast.stmt.ExpressionStatement; import org.codehaus.groovy.ast.stmt.Statement; @@ -1501,4 +1501,9 @@ public class ClassNode extends AnnotatedNode implements Opcodes, GroovydocHolder public Groovydoc getGroovydoc() { return this.<Groovydoc>getNodeMetaData(DOC_COMMENT); } + + @Override + public Object getInstance() { + return this; + } } http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/org/codehaus/groovy/ast/FieldNode.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/ast/FieldNode.java b/src/main/org/codehaus/groovy/ast/FieldNode.java index 987349b..78e3ace 100644 --- a/src/main/org/codehaus/groovy/ast/FieldNode.java +++ b/src/main/org/codehaus/groovy/ast/FieldNode.java @@ -18,9 +18,9 @@ */ package org.codehaus.groovy.ast; +import groovy.lang.groovydoc.GroovydocHolder; import org.codehaus.groovy.ast.expr.Expression; -import org.codehaus.groovy.ast.groovydoc.Groovydoc; -import org.codehaus.groovy.ast.groovydoc.GroovydocHolder; +import groovy.lang.groovydoc.Groovydoc; import org.objectweb.asm.Opcodes; import java.lang.reflect.Field; @@ -197,4 +197,9 @@ public class FieldNode extends AnnotatedNode implements Opcodes, Variable, Groov public Groovydoc getGroovydoc() { return this.<Groovydoc>getNodeMetaData(DOC_COMMENT); } + + @Override + public Object getInstance() { + return this; + } } http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/org/codehaus/groovy/ast/MethodNode.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/ast/MethodNode.java b/src/main/org/codehaus/groovy/ast/MethodNode.java index ea0db81..db7f723 100644 --- a/src/main/org/codehaus/groovy/ast/MethodNode.java +++ b/src/main/org/codehaus/groovy/ast/MethodNode.java @@ -18,9 +18,9 @@ */ package org.codehaus.groovy.ast; +import groovy.lang.groovydoc.Groovydoc; +import groovy.lang.groovydoc.GroovydocHolder; import org.apache.groovy.ast.tools.MethodNodeUtils; -import org.codehaus.groovy.ast.groovydoc.Groovydoc; -import org.codehaus.groovy.ast.groovydoc.GroovydocHolder; import org.codehaus.groovy.ast.stmt.BlockStatement; import org.codehaus.groovy.ast.stmt.Statement; import org.objectweb.asm.Opcodes; @@ -276,4 +276,9 @@ public class MethodNode extends AnnotatedNode implements Opcodes, GroovydocHolde public Groovydoc getGroovydoc() { return this.<Groovydoc>getNodeMetaData(DOC_COMMENT); } + + @Override + public Object getInstance() { + return this; + } } http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/org/codehaus/groovy/ast/groovydoc/Groovydoc.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/ast/groovydoc/Groovydoc.java b/src/main/org/codehaus/groovy/ast/groovydoc/Groovydoc.java deleted file mode 100644 index 04aef38..0000000 --- a/src/main/org/codehaus/groovy/ast/groovydoc/Groovydoc.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.codehaus.groovy.ast.groovydoc; - -import java.util.List; -import java.util.Objects; - -/** - * Represents groovydoc - */ -public class Groovydoc { - private String content; - private List<GroovydocTag> tagList; - private GroovydocHolder groovydocHolder; - - public Groovydoc(String content, GroovydocHolder groovydocHolder) { - this.content = content; - this.groovydocHolder = groovydocHolder; - } - - public String getContent() { - return content; - } - - public List<GroovydocTag> getTagList() { - throw new UnsupportedOperationException("[TODO]parsing tags will be a new features of the next releases"); -// return tagList; - } - - public GroovydocHolder getGroovydocHolder() { - return groovydocHolder; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Groovydoc groovydoc = (Groovydoc) o; - return Objects.equals(content, groovydoc.content) && - Objects.equals(groovydocHolder, groovydoc.groovydocHolder); - } - - @Override - public int hashCode() { - return Objects.hash(content, groovydocHolder); - } - - @Override - public String toString() { - return this.content; - } -} http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/org/codehaus/groovy/ast/groovydoc/GroovydocHolder.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/ast/groovydoc/GroovydocHolder.java b/src/main/org/codehaus/groovy/ast/groovydoc/GroovydocHolder.java deleted file mode 100644 index e5585fe..0000000 --- a/src/main/org/codehaus/groovy/ast/groovydoc/GroovydocHolder.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.codehaus.groovy.ast.groovydoc; - -/** - * Represents Groovydoc Holder - */ -public interface GroovydocHolder { - String DOC_COMMENT = "_DOC_COMMENT"; // keys for meta data - /** - * Get the groovydoc - * @return the groovydoc - */ - Groovydoc getGroovydoc(); -} http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/org/codehaus/groovy/ast/groovydoc/GroovydocTag.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/ast/groovydoc/GroovydocTag.java b/src/main/org/codehaus/groovy/ast/groovydoc/GroovydocTag.java deleted file mode 100644 index 54b2e2e..0000000 --- a/src/main/org/codehaus/groovy/ast/groovydoc/GroovydocTag.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.codehaus.groovy.ast.groovydoc; - -import java.util.Objects; - -/** - * TODO parse groovydoc to get tag content - */ -public class GroovydocTag { - private String name; - private String content; - private Groovydoc groovydoc; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getContent() { - return content; - } - - public void setContent(String content) { - this.content = content; - } - - public Groovydoc getGroovydoc() { - return groovydoc; - } - - public void setGroovydoc(Groovydoc groovydoc) { - this.groovydoc = groovydoc; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - GroovydocTag that = (GroovydocTag) o; - return Objects.equals(name, that.name) && - Objects.equals(content, that.content) && - Objects.equals(groovydoc, that.groovydoc); - } - - @Override - public int hashCode() { - return Objects.hash(name, content, groovydoc); - } - - @Override - public String toString() { - return content; - } -} http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java ---------------------------------------------------------------------- diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java index b4dae13..1b8e803 100644 --- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java +++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java @@ -18890,7 +18890,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport { * @return runtime groovydoc * @since 2.6.0 */ - public static String getGroovydoc(AnnotatedElement holder) { - return holder.<Groovydoc>getAnnotation(Groovydoc.class).value(); + public static groovy.lang.groovydoc.Groovydoc getGroovydoc(AnnotatedElement holder) { + return new groovy.lang.groovydoc.Groovydoc(holder.<Groovydoc>getAnnotation(Groovydoc.class).value(), holder); } } http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java ---------------------------------------------------------------------- diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java index b3b58f2..f93549b 100644 --- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java +++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/GroovydocManager.java @@ -28,7 +28,7 @@ import org.codehaus.groovy.ast.AnnotatedNode; import org.codehaus.groovy.ast.AnnotationNode; import org.codehaus.groovy.ast.ClassHelper; import org.codehaus.groovy.ast.expr.ConstantExpression; -import org.codehaus.groovy.ast.groovydoc.GroovydocHolder; +import groovy.lang.groovydoc.GroovydocHolder; import java.util.List; @@ -109,7 +109,7 @@ public class GroovydocManager { return; } - node.putNodeMetaData(DOC_COMMENT, new org.codehaus.groovy.ast.groovydoc.Groovydoc(docCommentNodeText, (GroovydocHolder) node)); + node.putNodeMetaData(DOC_COMMENT, new groovy.lang.groovydoc.Groovydoc(docCommentNodeText, (GroovydocHolder) node)); } /* http://git-wip-us.apache.org/repos/asf/groovy/blob/f7e2c46a/subprojects/parser-antlr4/src/test/resources/core/Groovydoc_01x.groovy ---------------------------------------------------------------------- diff --git a/subprojects/parser-antlr4/src/test/resources/core/Groovydoc_01x.groovy b/subprojects/parser-antlr4/src/test/resources/core/Groovydoc_01x.groovy index 5ab86a8..9ed1eec 100644 --- a/subprojects/parser-antlr4/src/test/resources/core/Groovydoc_01x.groovy +++ b/subprojects/parser-antlr4/src/test/resources/core/Groovydoc_01x.groovy @@ -62,9 +62,9 @@ class AA { } -assert AA.class.groovydoc.contains('class AA') -assert AA.class.getMethod('m', new Class[0]).groovydoc.contains('method m') -assert AA.class.getConstructor().groovydoc.contains('constructor AA') -assert AA.class.getField('SOME_FIELD').groovydoc.contains('field SOME_FIELD') -assert AA.class.getDeclaredClasses().find {it.simpleName.contains('InnerClass')}.groovydoc.contains('class InnerClass') -assert BB.class.groovydoc.contains('annotation BB') +assert AA.class.groovydoc.content.contains('class AA') +assert AA.class.getMethod('m', new Class[0]).groovydoc.content.contains('method m') +assert AA.class.getConstructor().groovydoc.content.contains('constructor AA') +assert AA.class.getField('SOME_FIELD').groovydoc.content.contains('field SOME_FIELD') +assert AA.class.getDeclaredClasses().find {it.simpleName.contains('InnerClass')}.groovydoc.content.contains('class InnerClass') +assert BB.class.groovydoc.content.contains('annotation BB')