Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lang] Detection of the invalid and discouraged uses of occurrences i…
…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
1 parent
9bd7110
commit 960b07c
Showing
10 changed files
with
786 additions
and
144 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
main/coreplugins/io.sarl.lang/src/io/sarl/lang/typesystem/DefaultImmutableTypeValidator.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,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; | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
main/coreplugins/io.sarl.lang/src/io/sarl/lang/typesystem/IImmutableTypeValidator.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,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); | ||
|
||
} |
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
Oops, something went wrong.