Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
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
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand Down Expand Up @@ -61,6 +62,8 @@ public interface AndroidAnnotationsEnvironment {

AndroidManifest getAndroidManifest();

AnnotationElements getExtractedElements();

AnnotationElements getValidatedElements();

JCodeModel getCodeModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ public void typeHasAnnotation(Class<? extends Annotation> annotation, Element el
typeHasAnnotation(annotation, elementType, valid);
}

public void typeHasValidAnnotation(Class<? extends Annotation> annotation, Element element, ElementValidation valid) {
typeHasAnnotation(annotation, element, valid);

if (valid.isValid()) {
typeIsValid(annotation, element.asType(), valid);
}
}

public void typeHasAnnotation(Class<? extends Annotation> annotation, TypeMirror elementType, ElementValidation valid) {
Element typeElement = annotationHelper.getTypeUtils().asElement(elementType);
if (!elementHasAnnotationSafe(annotation, typeElement)) {
Expand All @@ -324,14 +332,14 @@ public void typeOrTargetValueHasAnnotation(Class<? extends Annotation> annotatio
DeclaredType targetAnnotationClassValue = annotationHelper.extractAnnotationClassParameter(element);

if (targetAnnotationClassValue != null) {
typeHasAnnotation(annotation, targetAnnotationClassValue, valid);
targetElement = targetAnnotationClassValue.asElement();

if (!annotationHelper.getTypeUtils().isAssignable(targetAnnotationClassValue, targetElement.asType())) {
valid.addError("The value of %s must be assignable into the annotated field");
}
} else {
typeHasAnnotation(annotation, targetElement, valid);
}

typeHasValidAnnotation(annotation, targetElement, valid);
}

Element findTargetElement(Element element, ElementValidation valid) {
Expand All @@ -352,6 +360,20 @@ Element findTargetElement(Element element, ElementValidation valid) {
return element;
}

public void typeIsValid(Class<? extends Annotation> annotation, TypeMirror elementType, ElementValidation elementValidation) {
Set<? extends Element> validElements = validatedModel().getRootAnnotatedElements(annotation.getName());

Set<? extends Element> extractedElements = environment().getExtractedElements().getRootAnnotatedElements(annotation.getName());

Element typeElement = annotationHelper.getTypeUtils().asElement(elementType);

if (!extractedElements.contains(typeElement) || validElements.contains(typeElement)) {
return;
}

elementValidation.addError("The type " + typeElement.getSimpleName() + " is invalid, " + "please check the messages on that type.");
}

private boolean elementHasAnnotationSafe(Class<? extends Annotation> annotation, Element element) {
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for (AnnotationMirror annotationMirror : annotationMirrors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand Down Expand Up @@ -37,7 +38,6 @@
import org.androidannotations.internal.exception.AndroidManifestNotFoundException;
import org.androidannotations.internal.exception.ProcessingException;
import org.androidannotations.internal.exception.RClassNotFoundException;
import org.androidannotations.internal.exception.ValidationException;
import org.androidannotations.internal.exception.VersionMismatchException;
import org.androidannotations.internal.exception.VersionNotFoundException;
import org.androidannotations.internal.generation.CodeModelGenerator;
Expand Down Expand Up @@ -124,8 +124,6 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
try {
checkApiAndProcessorVersions();
processThrowing(annotations, roundEnv);
} catch (ValidationException e) {
// We do nothing, errors have been printed by ModelValidator
} catch (ProcessingException e) {
handleException(annotations, roundEnv, e);
} catch (Exception e) {
Expand All @@ -136,7 +134,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

LOGGER.info("Finish processing");

LoggerContext.getInstance().close();
LoggerContext.getInstance().close(roundEnv.processingOver());
return true;
}

Expand All @@ -156,6 +154,8 @@ private void processThrowing(Set<? extends TypeElement> annotations, RoundEnviro
}

AnnotationElementsHolder extractedModel = extractAnnotations(annotations, roundEnv);
androidAnnotationsEnv.setExtractedElements(extractedModel);

AnnotationElementsHolder validatingHolder = extractedModel.validatingHolder();
androidAnnotationsEnv.setValidatedElements(validatingHolder);

Expand Down Expand Up @@ -210,7 +210,7 @@ private IRClass findRClasses(AndroidManifest androidManifest) throws RClassNotFo
}
}

private AnnotationElements validateAnnotations(AnnotationElements extractedModel, AnnotationElementsHolder validatingHolder) throws ValidationException {
private AnnotationElements validateAnnotations(AnnotationElements extractedModel, AnnotationElementsHolder validatingHolder) {
timeStats.start("Validate Annotations");
ModelValidator modelValidator = new ModelValidator(androidAnnotationsEnv);
AnnotationElements validatedAnnotations = modelValidator.validate(extractedModel, validatingHolder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand Down Expand Up @@ -48,6 +49,8 @@ public class InternalAndroidAnnotationsEnvironment implements AndroidAnnotations
private IRClass rClass;
private AndroidManifest androidManifest;

private AnnotationElements extractedElements;

private AnnotationElements validatedElements;

private ProcessHolder processHolder;
Expand All @@ -73,6 +76,10 @@ public void setAndroidEnvironment(IRClass rClass, AndroidManifest androidManifes
this.androidManifest = androidManifest;
}

public void setExtractedElements(AnnotationElements extractedElements) {
this.extractedElements = extractedElements;
}

public void setValidatedElements(AnnotationElements validatedElements) {
this.validatedElements = validatedElements;
}
Expand Down Expand Up @@ -141,6 +148,11 @@ public AndroidManifest getAndroidManifest() {
return androidManifest;
}

@Override
public AnnotationElements getExtractedElements() {
return extractedElements;
}

@Override
public AnnotationElements getValidatedElements() {
return validatedElements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void validate(Element element, ElementValidation validation) {
validatorHelper.isNotPrivate(element, validation);

Element param = injectHelper.getParam(element);
validatorHelper.typeHasAnnotation(EApplication.class, param, validation);
validatorHelper.typeHasValidAnnotation(EApplication.class, param, validation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand Down Expand Up @@ -51,6 +52,10 @@ public void validate(Element element, ElementValidation validation) {
validatorHelper.isNotPrivate(element, validation);

validatorHelper.isNotFinal(element, validation);

if (element.getAnnotation(Bean.class) != null) {
validatorHelper.typeIsValid(EBean.class, element.asType(), validation);
}
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand All @@ -15,9 +16,7 @@
*/
package org.androidannotations.internal.process;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.lang.model.element.AnnotationMirror;
Expand All @@ -26,7 +25,6 @@
import org.androidannotations.AndroidAnnotationsEnvironment;
import org.androidannotations.ElementValidation;
import org.androidannotations.handler.AnnotationHandler;
import org.androidannotations.internal.exception.ValidationException;
import org.androidannotations.internal.model.AnnotationElements;
import org.androidannotations.internal.model.AnnotationElementsHolder;
import org.androidannotations.logger.Logger;
Expand All @@ -41,10 +39,9 @@ public ModelValidator(AndroidAnnotationsEnvironment environment) {
this.environment = environment;
}

public AnnotationElements validate(AnnotationElements extractedModel, AnnotationElementsHolder validatingHolder) throws ValidationException {
public AnnotationElements validate(AnnotationElements extractedModel, AnnotationElementsHolder validatingHolder) {

LOGGER.info("Validating elements");
List<ElementValidation> failedValidations = new ArrayList<>();

/*
* We currently do not validate the elements on the ancestors, assuming
Expand Down Expand Up @@ -78,22 +75,17 @@ public AnnotationElements validate(AnnotationElements extractedModel, Annotation
}

for (String warning : elementValidation.getWarnings()) {
LOGGER.warn(warning, elementValidation.getElement(), elementValidation.getAnnotationMirror());
LOGGER.warn(warning, elementValidation.getElement(), annotationMirror);
}

if (elementValidation.isValid()) {
validatedAnnotatedElements.add(annotatedElement);
} else {
failedValidations.add(elementValidation);
LOGGER.warn("Element {} invalidated by {}", annotatedElement, annotatedElement, validatorSimpleName);
}
}
}

if (!failedValidations.isEmpty()) {
throw new ValidationException(failedValidations);
}

return validatingHolder;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand Down Expand Up @@ -85,9 +86,9 @@ public void setEnvironment(AndroidAnnotationsEnvironment environment) {
}
}

public void close() {
public void close(boolean lastRound) {
for (Appender appender : appenders) {
appender.close();
appender.close(lastRound);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand Down Expand Up @@ -44,6 +45,6 @@ public void setEnvironment(AndroidAnnotationsEnvironment environment) {

public abstract void append(Level level, Element element, AnnotationMirror annotationMirror, String message);

public abstract void close();
public abstract void close(boolean lastRound);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand All @@ -15,6 +16,9 @@
*/
package org.androidannotations.logger.appender;

import java.util.LinkedList;
import java.util.List;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;

Expand All @@ -23,6 +27,8 @@

public class ConsoleAppender extends Appender {

private final List<String> errors = new LinkedList<>();

public ConsoleAppender() {
super(new FormatterFull());
}
Expand All @@ -36,12 +42,17 @@ public void append(Level level, Element element, AnnotationMirror annotationMirr
if (level.isSmaller(Level.ERROR)) {
System.out.println(message);
} else {
System.err.println(message);
errors.add(message);
}
}

@Override
public void close() {
public synchronized void close(boolean lastRound) {
if (lastRound) {
for (String error : errors) {
System.err.println(error);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (C) 2010-2016 eBusiness Information, Excilys Group
* Copyright (C) 2016-2017 the AndroidAnnotations project
*
* 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
Expand Down Expand Up @@ -57,7 +58,7 @@ public synchronized void open() {
}

@Override
public synchronized void close() {
public synchronized void close(boolean lastRound) {
if (isStreamOpened()) {
try {
outputStream.close();
Expand Down
Loading