Skip to content

Commit

Permalink
[lang] Detection of the invalid and discouraged uses of occurrences i…
Browse files Browse the repository at this point in the history
…s refactored.

In order to take into account variadic arguments, the validation
algorithm that is related to the invalid or discouraged uses of
ocurrence is refactored.

The tool IImmutableTypeValidator is provided in order to determine if a
given type is assumed to be immutable (primitive types, String, etc.).

close #847

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Aug 20, 2018
1 parent 9bd7110 commit 960b07c
Show file tree
Hide file tree
Showing 10 changed files with 786 additions and 144 deletions.
@@ -0,0 +1,81 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2018 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.typesystem;

import java.io.File;
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

import com.google.inject.Singleton;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;

/**
* Tool for validating the types against their immutability.
*
* <p>An immutable type is a type those state cannot be changed after it is constructed.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8
*/
@Singleton
public class DefaultImmutableTypeValidator implements IImmutableTypeValidator {

private static final Class<?>[] IMMUTABLE_TYPES = {
String.class,
UUID.class,
URL.class,
URI.class,
Enum.class,
Number.class,
Date.class,
File.class,
Locale.class,
InetAddress.class,
StackTraceElement.class,
};

@Override
public boolean isImmutable(LightweightTypeReference type) {
assert type != null;
final LightweightTypeReference ref = type.getPrimitiveIfWrapperType();
if (ref.isArray()) {
return false;
}
if (ref.isPrimitive() || ref.isPrimitiveVoid()) {
return true;
}
for (final Class<?> jvmType : IMMUTABLE_TYPES) {
if (type.isSubtypeOf(jvmType)) {
return true;
}
}
return false;
}

}
@@ -0,0 +1,50 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2018 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.typesystem;

import com.google.inject.ImplementedBy;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;

/**
* Tool for validating the types against their immutability.
*
* <p>An immutable type is a type those state cannot be changed after it is constructed.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.8
*/
@ImplementedBy(DefaultImmutableTypeValidator.class)
public interface IImmutableTypeValidator {

/** Replies if the given type is associated to an immutable type.
*
* <p>An unmodifiable type is a primitive type or an object type those state cannot
* be changed after is it created in memory, e.g. {@link String}.
* @param type the type to test.
* @return {@code true} if the given type is known as unmodifiable. Otherwise {@code false}.
*/
boolean isImmutable(LightweightTypeReference type);

}
42 changes: 42 additions & 0 deletions main/coreplugins/io.sarl.lang/src/io/sarl/lang/util/Utils.java
Expand Up @@ -1261,6 +1261,48 @@ public static boolean getContainerNotOfType(EObject element, Class<? extends EOb
return false;
}

/**
* Returns the closest {@link EObject#eContainer() container object} that is of one of the requested type.
*
* @param element the element to start from.
* @param container the container.
* @param directContainerChild the child of the container that is or contains the given element.
* @param types the unexpected types.
* @return {@code true} if the container was found.
* @since 0.8
* @see EcoreUtil2#getContainerOfType(EObject, Class)
*/
@SafeVarargs
public static boolean getContainerOfType(EObject element,
OutParameter<EObject> container, OutParameter<EObject> directContainerChild,
Class<? extends EObject>... types) {
EObject previous = element;
EObject elt = element.eContainer();
while (elt != null) {
if (isInstance(types, elt)) {
if (directContainerChild != null) {
directContainerChild.set(previous);
}
if (container != null) {
container.set(elt);
}
return true;
}
previous = elt;
elt = elt.eContainer();
}
return false;
}

private static boolean isInstance(Class<? extends EObject>[] types, Object element) {
for (final Class<? extends EObject> type : types) {
if (type.isInstance(element)) {
return true;
}
}
return false;
}

/**
* Returns the closest {@link EObject#eContainer() container object} that is validating the predicate.
*
Expand Down
Expand Up @@ -128,6 +128,7 @@ private Messages() {
public static String SARLValidator_9;
public static String SARLValidator_90;
public static String SARLValidator_91;
public static String SARLValidator_92;
public static String SARLSyntaxErrorMessageProvider_0;
public static String SARLSyntaxErrorMessageProvider_1;
}

0 comments on commit 960b07c

Please sign in to comment.