Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MPLUGIN-426] add @Description annotation #152

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.apache.maven.plugins.annotations;

/*
* 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.
*/

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Describes a {@code Mojo} or a Mojo’s {@code Parameter} when JavaDoc extraction is not feasible (because of deviating
* documentation goals) or not possible (e.g. for other JVM languages like Scala, Groovy or Kotlin).
*/
@Documented
@Retention( RetentionPolicy.CLASS )
@Target( { ElementType.TYPE, ElementType.FIELD } )
@Inherited
public @interface Description
{
/**
* Description content for the {@code Mojo} or Mojo {@code Parameter}.
*
* <p>A &quot;Safe HTML&quot; subset can be used. This is achieved by running
* the content through the <a href="https://github.com/owasp/java-html-sanitizer"<OWASP Java HTML Sanitizer</a>
* before rendering.</p>
*
* @return a description of the Mojo or the parameter.
*/
String content();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this named as value, could annotation be used as @Description("xxx yyy")?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was my first idea. But since it contains HTML, I thought it might be a good idea to not use "value". But then, what would value be if added later...
Can change that. Will also add tests.


/**
* The version of the plugin since when this goal or parameter was introduced (inclusive, optional).
*
* @return The version of the plugin since when this goal or parameter was introduced (inclusive) or an empty string
* of no since version has been given.
*/
String since() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,19 @@ protected void populateDataFromJavadoc( Map<String, MojoAnnotatedClass> mojoAnno
MojoAnnotationContent mojoAnnotationContent = entry.getValue().getMojo();
if ( mojoAnnotationContent != null )
{
mojoAnnotationContent.setDescription( javaClass.getComment() );
if ( StringUtils.isEmpty( mojoAnnotationContent.getDescription() ) )
{
mojoAnnotationContent.setDescription( javaClass.getComment() );
}

DocletTag since = findInClassHierarchy( javaClass, "since" );
if ( since != null )
if ( since != null && StringUtils.isEmpty( mojoAnnotationContent.getSince() ) )
{
mojoAnnotationContent.setSince( since.getValue() );
}

DocletTag deprecated = findInClassHierarchy( javaClass, "deprecated" );
if ( deprecated != null )
if ( deprecated != null && StringUtils.isEmpty( mojoAnnotationContent.getDeprecated() ) )
{
Comment on lines -291 to 304
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New behaviour (compatible): Only set from javadoc annotation if not already set by Java annotation..

mojoAnnotationContent.setDeprecated( deprecated.getValue() );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.apache.maven.tools.plugin.extractor.annotations.datamodel;

/*
* 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.
*/

import java.util.StringJoiner;

/**
* @author Benjamin Marwell
* @since 3.7.0
*/
public class DescriptionAnnotationContent
{

private String content;

private String since;

public String getContent()
{
return content;
}

public void setContent( String content )
{
this.content = content;
}

public String getSince()
{
return since;
}

public void setSince( String since )
{
this.since = since;
}

@Override
public String toString()
{
return new StringJoiner( ", ", DescriptionAnnotationContent.class.getSimpleName() + "[", "]" )
.add( "content='" + content + "'" )
.add( "since='" + since + "'" )
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@

import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Description;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.tools.plugin.extractor.ExtractionException;
import org.apache.maven.tools.plugin.extractor.annotations.datamodel.ComponentAnnotationContent;
import org.apache.maven.tools.plugin.extractor.annotations.datamodel.DescriptionAnnotationContent;
import org.apache.maven.tools.plugin.extractor.annotations.datamodel.ExecuteAnnotationContent;
import org.apache.maven.tools.plugin.extractor.annotations.datamodel.MojoAnnotationContent;
import org.apache.maven.tools.plugin.extractor.annotations.datamodel.ParameterAnnotationContent;
Expand Down Expand Up @@ -285,6 +287,16 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )
mojoAnnotatedClass.setMojo( mojoAnnotationContent );
}

// @Description annotation
mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitor( Description.class );
if ( mojoAnnotationVisitor != null )
{
DescriptionAnnotationContent descriptionAnnotationContent = new DescriptionAnnotationContent();
populateAnnotationContent( descriptionAnnotationContent, mojoAnnotationVisitor );

mojoAnnotatedClass.getMojo().setDescription( descriptionAnnotationContent.getContent() );
}

// @Execute annotation
mojoAnnotationVisitor = mojoClassVisitor.getAnnotationVisitor( Execute.class );
if ( mojoAnnotationVisitor != null )
Expand All @@ -306,6 +318,17 @@ protected void analyzeVisitors( MojoClassVisitor mojoClassVisitor )

populateAnnotationContent( parameterAnnotationContent, fieldAnnotationVisitor );

DescriptionAnnotationContent descriptionAnnotationContent = new DescriptionAnnotationContent();
final MojoAnnotationVisitor descriptionAnnotationVisitor =
annotationVisitorMap.get( Description.class.getName() );
if ( descriptionAnnotationVisitor != null )
{
populateAnnotationContent( descriptionAnnotationContent, descriptionAnnotationVisitor );
}
Comment on lines +322 to +328
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Description is optional and thus can be null.


parameterAnnotationContent.setDescription( descriptionAnnotationContent.getContent() );
parameterAnnotationContent.setSince( descriptionAnnotationContent.getSince() );

if ( annotationVisitorMap.containsKey( Deprecated.class.getName() ) )
{
parameterAnnotationContent.setDeprecated( EMPTY );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Description;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -38,10 +39,12 @@ public interface MojoAnnotationsScanner
String ROLE = MojoAnnotationsScanner.class.getName();

List<String> CLASS_LEVEL_ANNOTATIONS = Arrays.asList( Mojo.class.getName(),
Description.class.getName(),
Execute.class.getName(),
Deprecated.class.getName() );

List<String> FIELD_LEVEL_ANNOTATIONS = Arrays.asList( Parameter.class.getName(),
Description.class.getName(),
Component.class.getName(),
Deprecated.class.getName() );

Expand Down