Skip to content

Commit

Permalink
[all] Split @IssueOnCall into @ErrorOnCall, @WarningOnCall, `@I…
Browse files Browse the repository at this point in the history
…nfoOnCall`.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jun 17, 2020
1 parent 5503a4f commit 9174118
Show file tree
Hide file tree
Showing 14 changed files with 631 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

package io.sarl.core

import io.sarl.lang.annotation.WarningOnCall
import io.sarl.lang.core.Address
import io.sarl.lang.core.EventListener
import io.sarl.lang.core.EventSpace
import io.sarl.lang.annotation.IssueOnCall

/**
* Event driven interaction space where participants (agents, etc) are free register and unregister themselves.
Expand Down Expand Up @@ -58,7 +58,8 @@ interface OpenEventSpace extends EventSpace {
* @deprecated since 0.12, see {@link #registerStrongParticipant(EventListener)} and
* {@link #registerStrongParticipant(EventListener).
*/
@IssueOnCall("Prefer calling registerStrongParticipant() or registerWeakParticipant()")
@SuppressWarnings("use_reserved_sarl_annotation")
@WarningOnCall("Prefer calling registerStrongParticipant() or registerWeakParticipant()")
def register(participant : EventListener, weakParticipant : boolean = false) : Address fires ParticipantJoined {
if (weakParticipant) {
participant.registerWeakParticipant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** Annotation for marking an element with a SARL compiler issue than the element is invoked from the SARL code.
/** Annotation for marking an element with a SARL compiler's error when the element is invoked from the SARL code.
*
* @author $Author: sgalland$
* @version $FullVersion$
Expand All @@ -36,7 +36,7 @@
*/
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD })
@Retention(RetentionPolicy.CLASS)
public @interface IssueOnCall {
public @interface ErrorOnCall {

/** Replies the message to provide to the SARL developer.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2020 the original authors or authors.
*
* 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.sarl.lang.annotation;

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

/** Annotation for marking an element with a SARL compiler issue than the element is invoked from the SARL code.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.12
*/
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD })
@Retention(RetentionPolicy.CLASS)
public @interface InfoOnCall {

/** Replies the message to provide to the SARL developer.
*
* @return the issue message. If empty or {@code null}, no issue will be generated by the compiler.
*/
String value();

/** Replies if the issue is always an error.
* It not, the result of {@link #warning()} is used.
*
* @return {@code true} if it is an error; {@code false} if the error level depends on {@link #warning()}.
* @see #warning()
*/
boolean error() default false;

/** Replies if the issue is always a warning.
* If the function {@link #error()} replies {@code true}, the result of this function should be ignored.
* If this function replies {@code true}, the message is a warning message.
* It not, the result of {@link #info()} is used.
*
* @return {@code true} if it is a warning; {@code false} if the error level depends on {@link #info()}.
* @see #error()
* @see #info()
*/
boolean warning() default false;

/** Replies if the issue is always a warning.
* If one of the functions {@link #error()} or {@link #warning()} replies {@code true},
* the result of this function should be ignored.
* If this function replies {@code true}, the message is an information message.
* If this function replies {@code false}, the message level depends on the user preferences.
* In this last case, the error level could be "error", "warning", "info", or "ignore"
*
* @return {@code true} if it is an information message; {@code false} if the error level depends the user preferences.
* @see #error()
* @see #warning()
*/
boolean info() default false;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2020 the original authors or authors.
*
* 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.sarl.lang.annotation;

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

/** Annotation for marking an element with a SARL compiler's warning when the element is invoked from the SARL code.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.12
*/
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD })
@Retention(RetentionPolicy.CLASS)
public @interface WarningOnCall {

/** Replies the message to provide to the SARL developer.
*
* @return the issue message. If empty or {@code null}, no issue will be generated by the compiler.
*/
String value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private Messages() {
public static String SARLBuilderConfigurationBlock_6;
public static String SARLValidatorConfigurationBlock_0;
public static String SARLValidatorConfigurationBlock_2;
public static String SARLValidatorConfigurationBlock_20;
public static String SARLValidatorConfigurationBlock_3;
public static String SARLValidatorConfigurationBlock_4;
public static String SARLValidatorConfigurationBlock_5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ protected void fillRestrictedApiSection(ComboBoxBuilder builder) {
Messages.SARLValidatorConfigurationBlock_8);
builder.addComboBox(IssueCodes.USED_RESERVED_SARL_ANNOTATION,
Messages.SARLValidatorConfigurationBlock_7);
builder.addComboBox(IssueCodes.PROGRAMMATIC_ISSUE_ANNOTATION,
Messages.SARLValidatorConfigurationBlock_20);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ SARLBuilderConfigurationBlock_5=Generate clone functions
SARLBuilderConfigurationBlock_6=Generate serial number fields
SARLValidatorConfigurationBlock_0=Redundant implementation of interface:
SARLValidatorConfigurationBlock_2=Variable name shadowing:
SARLValidatorConfigurationBlock_20=Programmatic issue annotation:
SARLValidatorConfigurationBlock_3=Discouraged boolean expression:
SARLValidatorConfigurationBlock_4=Discouraged capacity definition:
SARLValidatorConfigurationBlock_5=Unreachable behavior unit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,17 +976,6 @@ public void fixDiscouragedAnnotationUse(final Issue issue, IssueResolutionAccept
AnnotationRemoveModification.accept(this, issue, acceptor);
}

/** Quick fix for the discouraged annotation uses.
*
* @param issue the issue.
* @param acceptor the quick fix acceptor.
* @since 0.12
*/
@Fix(io.sarl.lang.validation.IssueCodes.PROGRAMMATIC_ISSUE_ANNOTATION)
public void fixProgrammaticIssueAnnotation(final Issue issue, IssueResolutionAcceptor acceptor) {
AnnotationRemoveModification.accept(this, issue, acceptor);
}

/** Quick fix for the manual definition of inline statements.
*
* @param issue the issue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@

import io.sarl.lang.SARLVersion;
import io.sarl.lang.annotation.EarlyExit;
import io.sarl.lang.annotation.IssueOnCall;
import io.sarl.lang.annotation.ErrorOnCall;
import io.sarl.lang.annotation.InfoOnCall;
import io.sarl.lang.annotation.WarningOnCall;
import io.sarl.lang.controlflow.ISarlEarlyExitComputer;
import io.sarl.lang.core.Agent;
import io.sarl.lang.core.Behavior;
Expand Down Expand Up @@ -2265,7 +2267,9 @@ public void checkReservedAnnotation(XtendAnnotationTarget annotationTarget) {
final QualifiedName reservedPackage = this.qualifiedNameConverter.toQualifiedName(
EarlyExit.class.getPackage().getName());
final String earlyExitAnnotation = EarlyExit.class.getName();
final String issueOnCallAnnotation = IssueOnCall.class.getName();
final String errorOnCallAnnotation = ErrorOnCall.class.getName();
final String warningOnCallAnnotation = WarningOnCall.class.getName();
final String infoOnCallAnnotation = InfoOnCall.class.getName();
for (final XAnnotation annotation : annotationTarget.getAnnotations()) {
final JvmType type = annotation.getAnnotationType();
if (type != null && !type.eIsProxy()) {
Expand All @@ -2277,7 +2281,9 @@ public void checkReservedAnnotation(XtendAnnotationTarget annotationTarget) {
annotation,
USED_RESERVED_SARL_ANNOTATION);
}
} else if (!Objects.equal(type.getIdentifier(), issueOnCallAnnotation)) {
} else if (!Objects.equal(type.getIdentifier(), errorOnCallAnnotation)
&& !Objects.equal(type.getIdentifier(), warningOnCallAnnotation)
&& !Objects.equal(type.getIdentifier(), infoOnCallAnnotation)) {
final QualifiedName annotationName = this.qualifiedNameConverter.toQualifiedName(
type.getIdentifier());
if (annotationName.startsWith(reservedPackage)) {
Expand All @@ -2292,6 +2298,22 @@ public void checkReservedAnnotation(XtendAnnotationTarget annotationTarget) {
}
}

@SuppressWarnings({"checkstyle:cyclomaticcomplexity"})
private static String parseIssueOnCallAnnotation(List<JvmAnnotationValue> values) {
final StringBuilder message = new StringBuilder();
for (final JvmAnnotationValue value : values) {
if (value instanceof JvmStringAnnotationValue) {
message.append(((JvmStringAnnotationValue) value).getValues());
} else if (value instanceof JvmCustomAnnotationValue) {
for (final Object obj : ((JvmCustomAnnotationValue) value).getValues()) {
if (obj instanceof XStringLiteral) {
message.append(((XStringLiteral) obj).getValue());
}
}
}
}
return message.toString();
}

/** Check if element has an programmatic issue message.
*
Expand All @@ -2301,38 +2323,39 @@ public void checkReservedAnnotation(XtendAnnotationTarget annotationTarget) {
@Check(CheckType.FAST)
@SuppressWarnings("checkstyle:nestedifdepth")
public void checkProgrammaticIssueMessage(XAbstractFeatureCall expression) {
if (!isIgnored(PROGRAMMATIC_ISSUE_ANNOTATION)) {
if (expression != null && expression.getFeature() != null) {
final JvmIdentifiableElement feature = expression.getFeature();
if (feature instanceof JvmAnnotationTarget) {
final JvmAnnotationTarget target = (JvmAnnotationTarget) feature;
final String annoName = IssueOnCall.class.getName();
for (final JvmAnnotationReference annotation : target.getAnnotations()) {
if (Objects.equal(annoName, annotation.getAnnotation().getIdentifier())) {
final List<JvmAnnotationValue> values = annotation.getValues();
if (values.isEmpty()) {
break;
}
final String message;
if (values.get(0) instanceof JvmStringAnnotationValue) {
message = Strings.concat("", ((JvmStringAnnotationValue) values.get(0)).getValues()); //$NON-NLS-1$
} else if (values.get(0) instanceof JvmCustomAnnotationValue) {
final StringBuilder b = new StringBuilder();
for (final Object obj : ((JvmCustomAnnotationValue) values.get(0)).getValues()) {
if (obj instanceof XStringLiteral) {
b.append(((XStringLiteral) obj).getValue());
}
}
message = b.toString();
} else {
break;
}
if (Strings.isEmpty(message)) {
break;
}
addIssue(
if (expression != null && expression.getFeature() != null) {
final JvmIdentifiableElement feature = expression.getFeature();
if (feature instanceof JvmAnnotationTarget) {
final JvmAnnotationTarget target = (JvmAnnotationTarget) feature;
final String errorAnnoName = ErrorOnCall.class.getName();
final String warningAnnoName = WarningOnCall.class.getName();
final String infoAnnoName = InfoOnCall.class.getName();
for (final JvmAnnotationReference annotation : target.getAnnotations()) {
final List<JvmAnnotationValue> values = annotation.getValues();
if (!values.isEmpty()) {
if (Objects.equal(errorAnnoName, annotation.getAnnotation().getIdentifier())) {
final String message = parseIssueOnCallAnnotation(values);
error(
MessageFormat.format(Messages.SARLValidator_99, message),
expression,
null,
ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
PROGRAMMATIC_ISSUE_ANNOTATION);
} else if (Objects.equal(warningAnnoName, annotation.getAnnotation().getIdentifier())) {
final String message = parseIssueOnCallAnnotation(values);
warning(
MessageFormat.format(Messages.SARLValidator_99, message),
expression,
null,
ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
PROGRAMMATIC_ISSUE_ANNOTATION);
} else if (Objects.equal(infoAnnoName, annotation.getAnnotation().getIdentifier())) {
final String message = parseIssueOnCallAnnotation(values);
info(
MessageFormat.format(Messages.SARLValidator_99, message),
expression,
null,
ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
PROGRAMMATIC_ISSUE_ANNOTATION);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ protected void initialize(IAcceptor<PreferenceKey> acceptor) {
IssueCodes.USED_RESERVED_SARL_ANNOTATION,
SeverityConverter.SEVERITY_WARNING));

acceptor.accept(create(
IssueCodes.PROGRAMMATIC_ISSUE_ANNOTATION,
SeverityConverter.SEVERITY_ERROR));

acceptor.accept(create(
IssueCodes.MANUAL_INLINE_DEFINITION,
SeverityConverter.SEVERITY_WARNING));
Expand Down
Loading

0 comments on commit 9174118

Please sign in to comment.