From 30c86d3291eff44483132c8e405ac481877ce2fc Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 10 Jun 2018 19:59:24 +0200 Subject: [PATCH] reformat ScmAnnotationProcessor --- .../annotation/ScmAnnotationProcessor.java | 358 ++++-------------- 1 file changed, 71 insertions(+), 287 deletions(-) diff --git a/scm-annotation-processor/src/main/java/sonia/scm/annotation/ScmAnnotationProcessor.java b/scm-annotation-processor/src/main/java/sonia/scm/annotation/ScmAnnotationProcessor.java index c1cd26dfe7..6d53276498 100644 --- a/scm-annotation-processor/src/main/java/sonia/scm/annotation/ScmAnnotationProcessor.java +++ b/scm-annotation-processor/src/main/java/sonia/scm/annotation/ScmAnnotationProcessor.java @@ -111,82 +111,53 @@ import static javax.lang.model.util.ElementFilter.methodsIn; @MetaInfServices(Processor.class) @SuppressWarnings({ "Since16", "Since15" }) @SupportedSourceVersion(SourceVersion.RELEASE_8) -public final class ScmAnnotationProcessor extends AbstractProcessor -{ +public final class ScmAnnotationProcessor extends AbstractProcessor { - /** Field description */ private static final String DESCRIPTOR_MODULE = "META-INF/scm/module.xml"; - - /** Field description */ private static final String DESCRIPTOR_PLUGIN = "META-INF/scm/plugin.xml"; - /** Field description */ private static final String EL_MODULE = "module"; - /** Field description */ private static final String EMPTY = ""; - /** Field description */ private static final String PROPERTY_VALUE = "yes"; - /** Field description */ - private static final Set SUBSCRIBE_ANNOTATIONS = - ImmutableSet.of(Subscribe.class.getName()); + private static final Set SUBSCRIBE_ANNOTATIONS = ImmutableSet.of(Subscribe.class.getName()); - /** Field description */ - private static final Set CLASS_ANNOTATIONS = - ImmutableSet.of(new ClassAnnotation("rest-resource", Path.class), - new ClassAnnotation("rest-provider", Provider.class)); + private static final Set CLASS_ANNOTATIONS = ImmutableSet.of( + new ClassAnnotation("rest-resource", Path.class), + new ClassAnnotation("rest-provider", Provider.class) + ); //~--- methods -------------------------------------------------------------- - /** - * Method description - * - * - * @param annotations - * @param roundEnv - * - * @return - */ @Override - public boolean process(Set annotations, - RoundEnvironment roundEnv) - { - if (!roundEnv.processingOver()) - { + public boolean process(Set annotations, RoundEnvironment roundEnv) { + if (!roundEnv.processingOver()) { Set descriptorElements = Sets.newHashSet(); Set subscriberAnnotations = Sets.newHashSet(); - for (TypeElement e : annotations) - { + for (TypeElement e : annotations) { PluginAnnotation pa = e.getAnnotation(PluginAnnotation.class); - if (pa != null) - { + if (pa != null) { scanForClassAnnotations(descriptorElements, roundEnv, e, pa.value()); } - if (SUBSCRIBE_ANNOTATIONS.contains(e.getQualifiedName().toString())) - { + if (SUBSCRIBE_ANNOTATIONS.contains(e.getQualifiedName().toString())) { subscriberAnnotations.add(e); } } - for (ClassAnnotation ca : CLASS_ANNOTATIONS) - { - TypeElement annotation = findAnnotation(annotations, - ca.annotationClass); + for (ClassAnnotation ca : CLASS_ANNOTATIONS) { + TypeElement annotation = findAnnotation(annotations, ca.annotationClass); - if (annotation != null) - { - scanForClassAnnotations(descriptorElements, roundEnv, annotation, - ca.elementName); + if (annotation != null) { + scanForClassAnnotations(descriptorElements, roundEnv, annotation, ca.elementName); } } - for (TypeElement annotation : subscriberAnnotations) - { + for (TypeElement annotation : subscriberAnnotations) { scanForSubscriberAnnotations(descriptorElements, roundEnv, annotation); } @@ -196,45 +167,22 @@ public final class ScmAnnotationProcessor extends AbstractProcessor return false; } - /** - * Method description - * - * - * @param closeable - */ - private void close(Closeable closeable) - { - if (closeable != null) - { - try - { + private void close(Closeable closeable) { + if (closeable != null) { + try { closeable.close(); - } - catch (IOException ex) - { + } catch (IOException ex) { printException("could not close closeable", ex); } } } - /** - * Method description - * - * - * @param annotations - * @param annotationClass - * - * @return - */ private TypeElement findAnnotation(Set annotations, - Class annotationClass) - { + Class annotationClass) { TypeElement annotation = null; - for (TypeElement te : annotations) - { - if (te.getQualifiedName().toString().equals(annotationClass.getName())) - { + for (TypeElement te : annotations) { + if (te.getQualifiedName().toString().equals(annotationClass.getName())) { annotation = te; break; @@ -244,94 +192,50 @@ public final class ScmAnnotationProcessor extends AbstractProcessor return annotation; } - /** - * Method description - * - * - * @param filer - * - * @return - * - * @throws IOException - */ - private File findDescriptor(Filer filer) throws IOException - { - FileObject f = filer.getResource(StandardLocation.CLASS_OUTPUT, EMPTY, - DESCRIPTOR_PLUGIN); + private File findDescriptor(Filer filer) throws IOException { + FileObject f = filer.getResource(StandardLocation.CLASS_OUTPUT, EMPTY, DESCRIPTOR_PLUGIN); File file = new File(f.toUri()); - if (!file.exists()) - { - f = filer.getResource(StandardLocation.CLASS_OUTPUT, EMPTY, - DESCRIPTOR_MODULE); + if (!file.exists()) { + f = filer.getResource(StandardLocation.CLASS_OUTPUT, EMPTY, DESCRIPTOR_MODULE); file = new File(f.toUri()); } return file; } - /** - * Method description - * - * - * @param f - * - * @param file - * - * @return - */ - private Document parseDocument(File file) - { + private Document parseDocument(File file) { Document doc = null; InputStream input = null; - try - { + try { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - if (file.exists()) - { + if (file.exists()) { input = new FileInputStream(file); doc = builder.parse(input); - } - else - { + } else { doc = builder.newDocument(); doc.appendChild(doc.createElement(EL_MODULE)); } } - catch (ParserConfigurationException | SAXException | IOException - | DOMException ex) - { + catch (ParserConfigurationException | SAXException | IOException | DOMException ex) { printException("could not parse document", ex); - } - finally - { + } finally { close(input); } return doc; } - /** - * Method description - * - * - * @param obj - * - * @return - */ - private String prepareArrayElement(Object obj) - { + private String prepareArrayElement(Object obj) { String v = obj.toString(); - if (v.startsWith("\"")) - { + if (v.startsWith("\"")) { v = v.substring(1); - if (v.endsWith("")) - { + if (v.endsWith("")) { v = v.substring(0, v.length() - 1); } } @@ -339,15 +243,7 @@ public final class ScmAnnotationProcessor extends AbstractProcessor return v; } - /** - * Method description - * - * - * @param msg - * @param throwable - */ - private void printException(String msg, Throwable throwable) - { + private void printException(String msg, Throwable throwable) { processingEnv.getMessager().printMessage(Kind.ERROR, msg); String stack = Throwables.getStackTraceAsString(throwable); @@ -355,37 +251,18 @@ public final class ScmAnnotationProcessor extends AbstractProcessor processingEnv.getMessager().printMessage(Kind.ERROR, stack); } - /** - * Method description - * - * - * @param descriptorElements - * @param roundEnv - * @param annotation - * @param elementName - * @param elements - * - * @return - */ - private void scanForClassAnnotations( - Set descriptorElements, RoundEnvironment roundEnv, - TypeElement annotation, String elementName) - { + private void scanForClassAnnotations(Set descriptorElements, + RoundEnvironment roundEnv, TypeElement annotation, String elementName) { Set classes = Sets.newHashSet(); - - for (Element e : roundEnv.getElementsAnnotatedWith(annotation)) - { - if (e.getKind().isClass() || e.getKind().isInterface()) - { + for (Element e : roundEnv.getElementsAnnotatedWith(annotation)) { + if (e.getKind().isClass() || e.getKind().isInterface()) { TypeElement type = (TypeElement) e; String desc = processingEnv.getElementUtils().getDocComment(type); - if (desc != null) - { + if (desc != null) { desc = desc.trim(); } - //J- classes.add( new ClassWithAttributes( type.getQualifiedName().toString(), @@ -393,45 +270,29 @@ public final class ScmAnnotationProcessor extends AbstractProcessor getAttributesFromAnnotation(e, annotation) ) ); - //J+ } } descriptorElements.add(new ClassSetElement(elementName, classes)); } - /** - * Method description - * - * - * @param descriptorElements - * @param roundEnv - * @param annotation - */ - private void scanForSubscriberAnnotations( - Set descriptorElements, RoundEnvironment roundEnv, - TypeElement annotation) - { - for (Element el : roundEnv.getElementsAnnotatedWith(annotation)) - { - if (el.getKind() == ElementKind.METHOD) - { + private void scanForSubscriberAnnotations(Set descriptorElements, RoundEnvironment roundEnv, + TypeElement annotation) { + for (Element el : roundEnv.getElementsAnnotatedWith(annotation)) { + if (el.getKind() == ElementKind.METHOD) { ExecutableElement ee = (ExecutableElement) el; List params = ee.getParameters(); - if ((params != null) && (params.size() == 1)) - { + if ((params != null) && (params.size() == 1)) { VariableElement param = params.get(0); Element clazz = el.getEnclosingElement(); String desc = processingEnv.getElementUtils().getDocComment(clazz); - if (desc != null) - { + if (desc != null) { desc = desc.trim(); } - //J- descriptorElements.add( new SubscriberElement( clazz.toString(), @@ -439,60 +300,37 @@ public final class ScmAnnotationProcessor extends AbstractProcessor desc ) ); - //J+ } } } } - /** - * Method description - * - * - * @param descriptorElements - */ - private void write(Set descriptorElements) - { + private void write(Set descriptorElements) { Filer filer = processingEnv.getFiler(); - try - { + try { File file = findDescriptor(filer); Document doc = parseDocument(file); - if (doc != null) - { + if (doc != null) { org.w3c.dom.Element root = doc.getDocumentElement(); - for (DescriptorElement el : descriptorElements) - { + for (DescriptorElement el : descriptorElements) { el.append(doc, root); } writeDocument(doc, file); } - } - catch (IOException ex) - { + } catch (IOException ex) { printException("could not open plugin descriptor", ex); } } - /** - * Method description - * - * - * @param doc - * @param f - * @param file - */ - private void writeDocument(Document doc, File file) - { + private void writeDocument(Document doc, File file) { Writer writer = null; - try - { + try { file.getParentFile().mkdirs(); writer = new FileWriter(file); @@ -501,42 +339,23 @@ public final class ScmAnnotationProcessor extends AbstractProcessor transformer.setOutputProperty(OutputKeys.INDENT, PROPERTY_VALUE); transformer.transform(new DOMSource(doc), new StreamResult(writer)); - } - catch (IOException | IllegalArgumentException | TransformerException ex) - { + } catch (IOException | IllegalArgumentException | TransformerException ex) { printException("could not write document", ex); - } - finally - { + } finally { close(writer); } } //~--- get methods ---------------------------------------------------------- - /** - * Method description - * - * - * @param el - * @param annotation - * - * @return - */ - private Map getAttributesFromAnnotation(Element el, - TypeElement annotation) - { + private Map getAttributesFromAnnotation(Element el, TypeElement annotation) { Map attributes = Maps.newHashMap(); - for (AnnotationMirror am : el.getAnnotationMirrors()) - { + for (AnnotationMirror am : el.getAnnotationMirrors()) { String qn = am.getAnnotationType().asElement().toString(); - if (qn.equals(annotation.toString())) - { - for (Entry entry : am.getElementValues().entrySet()) - { + if (qn.equals(annotation.toString())) { + for (Entry entry : am.getElementValues().entrySet()) { attributes.put(entry.getKey().getSimpleName().toString(), getValue(entry.getValue())); } @@ -555,39 +374,25 @@ public final class ScmAnnotationProcessor extends AbstractProcessor return attributes; } - /** - * Method description - * - * - * @param v - * - * @return - */ - private String getValue(AnnotationValue v) - { + private String getValue(AnnotationValue v) { String value; Object object = v.getValue(); - if (object instanceof Iterable) - { + if (object instanceof Iterable) { Iterator it = ((Iterable) object).iterator(); StringBuilder buffer = new StringBuilder(); - while (it.hasNext()) - { + while (it.hasNext()) { buffer.append(prepareArrayElement(it.next())); - if (it.hasNext()) - { + if (it.hasNext()) { buffer.append(","); } } value = buffer.toString(); - } - else - { + } else { value = object.toString(); } @@ -596,36 +401,15 @@ public final class ScmAnnotationProcessor extends AbstractProcessor //~--- inner classes -------------------------------------------------------- - /** - * Class description - * - * - * @version Enter version here..., 14/03/18 - * @author Enter your name here... - */ - private static final class ClassAnnotation - { + private static final class ClassAnnotation { - /** - * Constructs ... - * - * - * @param elementName - * @param annotationClass - */ - public ClassAnnotation(String elementName, - Class annotationClass) - { + private final String elementName; + private final Class annotationClass; + + ClassAnnotation(String elementName, Class annotationClass) { this.elementName = elementName; this.annotationClass = annotationClass; } - //~--- fields ------------------------------------------------------------- - - /** Field description */ - private final Class annotationClass; - - /** Field description */ - private final String elementName; } }