Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.apache.ozone.annotations;

import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.SimpleAnnotationValueVisitor8;
import javax.tools.Diagnostic;

/**
* Validates that picocli options use the preferred Ozone CLI option style.
*/
@SupportedAnnotationTypes(CliOptionStyleProcessor.OPTION_ANNOTATION)
public class CliOptionStyleProcessor extends AbstractProcessor {

static final String OPTION_ANNOTATION = "picocli.CommandLine.Option";
private static final String NAMES_ATTRIBUTE = "names";
private static final Pattern CAMEL_CASE = Pattern.compile("--.*[A-Z].*");
private static final Pattern UNDER_SCORE = Pattern.compile("--.*_.*");

@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}

@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
if (OPTION_ANNOTATION.contentEquals(annotation.getQualifiedName())) {
roundEnv.getElementsAnnotatedWith(annotation)
.forEach(this::checkOptionNames);
}
}
return false;
}

private void checkOptionNames(Element element) {
for (AnnotationMirror annotation : element.getAnnotationMirrors()) {
if (isOptionAnnotation(annotation)) {
checkOptionNames(element, annotation);
}
}
}

private boolean isOptionAnnotation(AnnotationMirror annotation) {
return OPTION_ANNOTATION.contentEquals(
annotation.getAnnotationType().asElement().toString());
}

private void checkOptionNames(Element element, AnnotationMirror annotation) {
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
annotation.getElementValues().entrySet()) {
if (entry.getKey().getSimpleName().contentEquals(NAMES_ATTRIBUTE)) {
checkOptionNameValues(element, annotation, entry.getValue());
}
}
}

private void checkOptionNameValues(Element element, AnnotationMirror annotation,
AnnotationValue value) {
value.accept(new SimpleAnnotationValueVisitor8<Void, Void>() {
@Override
public Void visitArray(List<? extends AnnotationValue> values,
Void unused) {
values.forEach(v -> checkOptionNameValues(element, annotation, v));
return null;
}

@Override
public Void visitString(String option, Void unused) {
checkOptionName(option, element, annotation, value);
return null;
}
}, null);
}

private void checkOptionName(String option, Element element,
AnnotationMirror annotation, AnnotationValue value) {
if (hasDeprecatedStyle(option)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR,
String.format("CLI option '%s' uses a deprecated style. New options "
+ "should use --dash-separated-style long names or "
+ "single-character short names.", option),
element, annotation, value);
}
}

private static boolean hasDeprecatedStyle(String option) {
if (option.startsWith("--")) {
return CAMEL_CASE.matcher(option).matches()
|| UNDER_SCORE.matcher(option).matches();
}
return option.startsWith("-") && option.length() > 2;
}

}
11 changes: 11 additions & 0 deletions hadoop-hdds/cli-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -67,6 +72,11 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<version>${hdds.version}</version>
</path>
<path>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
Expand All @@ -80,6 +90,7 @@
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.kohsuke.metainf_services.AnnotationProcessorImpl</annotationProcessor>
<annotationProcessor>org.apache.ozone.annotations.CliOptionStyleProcessor</annotationProcessor>
<annotationProcessor>picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
Expand Down
6 changes: 6 additions & 0 deletions hadoop-hdds/server-scm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-docs</artifactId>
Expand Down Expand Up @@ -255,6 +260,7 @@
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.apache.hadoop.hdds.conf.ConfigFileGenerator</annotationProcessor>
<annotationProcessor>org.apache.ozone.annotations.CliOptionStyleProcessor</annotationProcessor>
<annotationProcessor>org.apache.ozone.annotations.ReplicateAnnotationProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
Expand Down
11 changes: 11 additions & 0 deletions hadoop-ozone/cli-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<!-- required for @MetaInfServices at compile-time, but not at runtime -->
<groupId>org.kohsuke.metainf-services</groupId>
Expand Down Expand Up @@ -166,6 +171,11 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<version>${hdds.version}</version>
</path>
<path>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
Expand All @@ -179,6 +189,7 @@
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.kohsuke.metainf_services.AnnotationProcessorImpl</annotationProcessor>
<annotationProcessor>org.apache.ozone.annotations.CliOptionStyleProcessor</annotationProcessor>
<annotationProcessor>picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public class LifecycleResumeSubCommand implements Callable<Void> {
private LifecycleSubCommand parent;

@CommandLine.Option(
names = {"-id", "--service-id"},
names = {"--service-id"},
description = "Ozone Manager Service ID"
)
private String omServiceId;

@CommandLine.Option(
names = {"-host", "--service-host"},
names = {"--service-host"},
description = "Ozone Manager Host"
)
private String omHost;
Expand All @@ -70,4 +70,3 @@ protected PrintStream out() {
return System.out;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public class LifecycleStatusSubCommand implements Callable<Void> {
private LifecycleSubCommand parent;

@CommandLine.Option(
names = {"-id", "--service-id"},
names = {"--service-id"},
description = "Ozone Manager Service ID"
)
private String omServiceId;

@CommandLine.Option(
names = {"-host", "--service-host"},
names = {"--service-host"},
description = "Ozone Manager Host"
)
private String omHost;
Expand Down Expand Up @@ -86,4 +86,3 @@ protected PrintStream out() {
return System.out;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public class LifecycleSuspendSubCommand implements Callable<Void> {
private LifecycleSubCommand parent;

@CommandLine.Option(
names = {"-id", "--service-id"},
names = {"--service-id"},
description = "Ozone Manager Service ID"
)
private String omServiceId;

@CommandLine.Option(
names = {"-host", "--service-host"},
names = {"--service-host"},
description = "Ozone Manager Host"
)
private String omHost;
Expand Down Expand Up @@ -73,4 +73,3 @@ protected PrintStream out() {
return System.out;
}
}

11 changes: 11 additions & 0 deletions hadoop-ozone/cli-debug/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<!-- required for @MetaInfServices at compile-time, but not at runtime -->
<groupId>org.kohsuke.metainf-services</groupId>
Expand Down Expand Up @@ -262,6 +267,11 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<version>${hdds.version}</version>
</path>
<path>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
Expand All @@ -275,6 +285,7 @@
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.kohsuke.metainf_services.AnnotationProcessorImpl</annotationProcessor>
<annotationProcessor>org.apache.ozone.annotations.CliOptionStyleProcessor</annotationProcessor>
<annotationProcessor>picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
Expand Down
11 changes: 11 additions & 0 deletions hadoop-ozone/cli-repair/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<!-- required for @MetaInfServices at compile-time, but not at runtime -->
<groupId>org.kohsuke.metainf-services</groupId>
Expand Down Expand Up @@ -201,6 +206,11 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<version>${hdds.version}</version>
</path>
<path>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
Expand All @@ -214,6 +224,7 @@
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.kohsuke.metainf_services.AnnotationProcessorImpl</annotationProcessor>
<annotationProcessor>org.apache.ozone.annotations.CliOptionStyleProcessor</annotationProcessor>
<annotationProcessor>picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
Expand Down
11 changes: 11 additions & 0 deletions hadoop-ozone/cli-shell/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<!-- required for @MetaInfServices at compile-time, but not at runtime -->
<groupId>org.kohsuke.metainf-services</groupId>
Expand Down Expand Up @@ -154,6 +159,11 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<version>${hdds.version}</version>
</path>
<path>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
Expand All @@ -167,6 +177,7 @@
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.kohsuke.metainf_services.AnnotationProcessorImpl</annotationProcessor>
<annotationProcessor>org.apache.ozone.annotations.CliOptionStyleProcessor</annotationProcessor>
<annotationProcessor>picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
Expand Down
11 changes: 11 additions & 0 deletions hadoop-ozone/freon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<!-- required for @MetaInfServices at compile-time, but not at runtime -->
<groupId>org.kohsuke.metainf-services</groupId>
Expand Down Expand Up @@ -166,6 +171,11 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.apache.ozone</groupId>
<artifactId>hdds-annotation-processing</artifactId>
<version>${hdds.version}</version>
</path>
<path>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
Expand All @@ -179,6 +189,7 @@
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.kohsuke.metainf_services.AnnotationProcessorImpl</annotationProcessor>
<annotationProcessor>org.apache.ozone.annotations.CliOptionStyleProcessor</annotationProcessor>
<annotationProcessor>picocli.codegen.aot.graalvm.processor.NativeImageConfigGeneratorProcessor</annotationProcessor>
</annotationProcessors>
</configuration>
Expand Down
Loading