-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
141 additions
and
47 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
25 changes: 25 additions & 0 deletions
25
allure-descriptions-javadoc/src/main/java/io/qameta/allure/description/ClassNames.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,25 @@ | ||
/* | ||
* Copyright 2023 Qameta Software OÜ | ||
* | ||
* Licensed 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 io.qameta.allure.description; | ||
|
||
/** | ||
* @author charlie (Dmitry Baev). | ||
*/ | ||
final class ClassNames { | ||
|
||
static final String DESCRIPTION_ANNOTATION = "io.qameta.allure.Description"; | ||
|
||
} |
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 |
---|---|---|
|
@@ -15,8 +15,6 @@ | |
*/ | ||
package io.qameta.allure.description; | ||
|
||
import io.qameta.allure.Description; | ||
|
||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.annotation.processing.Filer; | ||
import javax.annotation.processing.Messager; | ||
|
@@ -28,22 +26,27 @@ | |
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.element.VariableElement; | ||
import javax.lang.model.util.ElementFilter; | ||
import javax.lang.model.util.Elements; | ||
import javax.tools.Diagnostic; | ||
import javax.tools.FileObject; | ||
import javax.tools.StandardLocation; | ||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.math.BigInteger; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static io.qameta.allure.util.ResultsUtils.generateMethodSignatureHash; | ||
import static io.qameta.allure.description.ClassNames.DESCRIPTION_ANNOTATION; | ||
|
||
/** | ||
* @author Egor Borisov [email protected] | ||
*/ | ||
@SupportedAnnotationTypes("io.qameta.allure.Description") | ||
@SupportedAnnotationTypes(DESCRIPTION_ANNOTATION) | ||
public class JavaDocDescriptionsProcessor extends AbstractProcessor { | ||
|
||
private static final String ALLURE_DESCRIPTIONS_FOLDER = "META-INF/allureDescriptions/"; | ||
|
@@ -68,24 +71,25 @@ public SourceVersion getSupportedSourceVersion() { | |
|
||
@Override | ||
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment env) { | ||
final Set<? extends Element> elements = env.getElementsAnnotatedWith(Description.class); | ||
elements.forEach(el -> { | ||
if (!el.getAnnotation(Description.class).useJavaDoc()) { | ||
return; | ||
} | ||
final String docs = elementUtils.getDocComment(el); | ||
final List<String> typeParams = ((ExecutableElement) el).getParameters().stream() | ||
final TypeElement typeElement = elementUtils.getTypeElement(DESCRIPTION_ANNOTATION); | ||
final Set<? extends Element> elements = env.getElementsAnnotatedWith(typeElement); | ||
final Set<ExecutableElement> methods = ElementFilter.methodsIn(elements); | ||
methods.forEach(method -> { | ||
final String rawDocs = elementUtils.getDocComment(method); | ||
final List<String> typeParams = method.getParameters().stream() | ||
.map(this::methodParameterTypeMapper) | ||
.collect(Collectors.toList()); | ||
final String name = el.getSimpleName().toString(); | ||
if (docs == null) { | ||
messager.printMessage(Diagnostic.Kind.WARNING, | ||
"Unable to create resource for method " + name + typeParams | ||
+ " as it does not have a docs comment"); | ||
final String name = method.getSimpleName().toString(); | ||
if (rawDocs == null) { | ||
return; | ||
} | ||
|
||
final String docs = rawDocs.trim(); | ||
if ("".equals(docs)) { | ||
return; | ||
} | ||
|
||
final String hash = generateMethodSignatureHash(el.getEnclosingElement().toString(), name, typeParams); | ||
final String hash = generateMethodSignatureHash(method.getEnclosingElement().toString(), name, typeParams); | ||
try { | ||
final FileObject file = filer.createResource(StandardLocation.CLASS_OUTPUT, "", | ||
ALLURE_DESCRIPTIONS_FOLDER + hash); | ||
|
@@ -105,4 +109,29 @@ private String methodParameterTypeMapper(final VariableElement parameter) { | |
final Element typeElement = processingEnv.getTypeUtils().asElement(parameter.asType()); | ||
return typeElement != null ? typeElement.toString() : parameter.asType().toString(); | ||
} | ||
|
||
private static String generateMethodSignatureHash(final String className, | ||
final String methodName, | ||
final List<String> parameterTypes) { | ||
final MessageDigest md = getMd5Digest(); | ||
md.update(className.getBytes(StandardCharsets.UTF_8)); | ||
md.update(methodName.getBytes(StandardCharsets.UTF_8)); | ||
parameterTypes.stream() | ||
.map(string -> string.getBytes(StandardCharsets.UTF_8)) | ||
.forEach(md::update); | ||
final byte[] bytes = md.digest(); | ||
return bytesToHex(bytes); | ||
} | ||
|
||
private static MessageDigest getMd5Digest() { | ||
try { | ||
return MessageDigest.getInstance("MD5"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException("Can not find hashing algorithm", e); | ||
} | ||
} | ||
|
||
private static String bytesToHex(final byte[] bytes) { | ||
return new BigInteger(1, bytes).toString(16); | ||
} | ||
} |
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
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