use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.
the class ImmutableAnalysis method checkSuper.
private Violation checkSuper(ImmutableSet<String> immutableTyParams, ClassType type) {
ClassType superType = (ClassType) state.getTypes().supertype(type);
if (superType.getKind() == TypeKind.NONE || state.getTypes().isSameType(state.getSymtab().objectType, superType)) {
return Violation.absent();
}
if (WellKnownMutability.isAnnotation(state, type)) {
// TODO(b/25630189): add enforcement
return Violation.absent();
}
AnnotationInfo superannotation = getImmutableAnnotation(superType.tsym, state);
String message = String.format("'%s' extends '%s'", threadSafety.getPrettyName(type.tsym), threadSafety.getPrettyName(superType.tsym));
if (superannotation != null) {
// If the superclass does happen to be immutable, we don't need to recursively
// inspect it. We just have to check that it's instantiated correctly:
Violation info = threadSafety.checkSuperInstantiation(immutableTyParams, superannotation, superType);
if (!info.isPresent()) {
return Violation.absent();
}
return info.plus(message);
}
// Recursive case: check if the supertype is 'effectively' immutable.
Violation info = checkForImmutability(Optional.<ClassTree>empty(), immutableTyParams, superType, new ViolationReporter() {
@Override
public Description.Builder describe(Tree tree, Violation info) {
return BugChecker.buildDescriptionFromChecker(tree, bugChecker).setMessage(info.plus(info.message()).message());
}
});
if (!info.isPresent()) {
return Violation.absent();
}
return info.plus(message);
}
use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.
the class ImmutableAnalysis method checkForImmutability.
/**
* Check that an {@code @Immutable}-annotated class:
*
* <ul>
* <li>does not declare or inherit any mutable fields,
* <li>any immutable supertypes are instantiated with immutable type arguments as required by
* their containerOf spec, and
* <li>any enclosing instances are immutable.
* </ul>
*
* requiring supertypes to be annotated immutable would be too restrictive.
*/
public Violation checkForImmutability(Optional<ClassTree> tree, ImmutableSet<String> immutableTyParams, ClassType type, ViolationReporter reporter) {
Violation info = areFieldsImmutable(tree, immutableTyParams, type, reporter);
if (info.isPresent()) {
return info;
}
for (Type interfaceType : state.getTypes().interfaces(type)) {
AnnotationInfo interfaceAnnotation = getImmutableAnnotation(interfaceType.tsym, state);
if (interfaceAnnotation == null) {
continue;
}
info = threadSafety.checkSuperInstantiation(immutableTyParams, interfaceAnnotation, interfaceType);
if (info.isPresent()) {
return info.plus(String.format("'%s' extends '%s'", threadSafety.getPrettyName(type.tsym), threadSafety.getPrettyName(interfaceType.tsym)));
}
}
if (!type.asElement().isEnum()) {
// don't check enum super types here to avoid double-reporting errors
info = checkSuper(immutableTyParams, type);
if (info.isPresent()) {
return info;
}
}
Type mutableEnclosing = threadSafety.mutableEnclosingInstance(tree, type);
if (mutableEnclosing != null) {
return info.plus(String.format("'%s' has mutable enclosing instance '%s'", threadSafety.getPrettyName(type.tsym), mutableEnclosing));
}
return Violation.absent();
}
use of com.sun.tools.javac.code.Type.ClassType in project error-prone by google.
the class ImmutableAnalysis method isFieldImmutable.
/**
* Check a single field for immutability.
*/
private Violation isFieldImmutable(Optional<Tree> tree, ImmutableSet<String> immutableTyParams, ClassSymbol classSym, ClassType classType, VarSymbol var, ViolationReporter reporter) {
if (bugChecker.isSuppressed(var)) {
return Violation.absent();
}
if (!var.getModifiers().contains(Modifier.FINAL) && !ASTHelpers.hasAnnotation(var, LazyInit.class, state)) {
Violation info = Violation.of(String.format("'%s' has non-final field '%s'", threadSafety.getPrettyName(classSym), var.getSimpleName()));
if (tree.isPresent()) {
// If we have a tree to attach diagnostics to, report the error immediately instead of
// accumulating the path to the error from the top-level class being checked
state.reportMatch(reporter.report(tree.get(), info, SuggestedFixes.addModifiers(tree.get(), state, Modifier.FINAL)));
return Violation.absent();
}
return info;
}
Type varType = state.getTypes().memberType(classType, var);
Violation info = threadSafety.isThreadSafeType(/* allowContainerTypeParameters= */
true, immutableTyParams, varType);
if (info.isPresent()) {
info = info.plus(String.format("'%s' has field '%s' of type '%s'", threadSafety.getPrettyName(classSym), var.getSimpleName(), varType));
if (tree.isPresent()) {
// If we have a tree to attach diagnostics to, report the error immediately instead of
// accumulating the path to the error from the top-level class being checked
state.reportMatch(reporter.report(tree.get(), info, Optional.empty()));
return Violation.absent();
}
return info;
}
return Violation.absent();
}
use of com.sun.tools.javac.code.Type.ClassType in project glide by bumptech.
the class ProcessorUtil method findClassValuesFromAnnotationOnClassAsNames.
Set<String> findClassValuesFromAnnotationOnClassAsNames(Element clazz, Class<? extends Annotation> annotationClass) {
String annotationClassName = annotationClass.getName();
AnnotationValue excludedModuleAnnotationValue = null;
for (AnnotationMirror annotationMirror : clazz.getAnnotationMirrors()) {
// instead. This check is necessary because a given class may have multiple Annotations.
if (!annotationClassName.equals(annotationMirror.getAnnotationType().toString())) {
continue;
}
Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> values = annotationMirror.getElementValues().entrySet();
// (usually value).
if (values.size() != 1) {
throw new IllegalArgumentException("Expected single value, but found: " + values);
}
excludedModuleAnnotationValue = values.iterator().next().getValue();
if (excludedModuleAnnotationValue == null || excludedModuleAnnotationValue instanceof Attribute.UnresolvedClass) {
throw new IllegalArgumentException("Failed to find value for: " + annotationClass + " from mirrors: " + clazz.getAnnotationMirrors());
}
}
if (excludedModuleAnnotationValue == null) {
return Collections.emptySet();
}
Object value = excludedModuleAnnotationValue.getValue();
if (value instanceof List) {
List<?> values = (List<?>) value;
Set<String> result = new HashSet<>(values.size());
for (Object current : values) {
result.add(getExcludedModuleClassFromAnnotationAttribute(clazz, current));
}
return result;
} else {
ClassType classType = (ClassType) value;
return Collections.singleton(classType.toString());
}
}
use of com.sun.tools.javac.code.Type.ClassType in project ceylon by eclipse.
the class T6889255 method getExpectedName.
String getExpectedName(VarSymbol v, int i) {
// synthetic method
if (((v.owner.owner.flags() & Flags.ENUM) != 0) && v.owner.name.toString().equals("valueOf"))
return "name";
// -- no Code attribute for the LocalVariableTable attribute
if ((v.owner.owner.flags() & Flags.INTERFACE) != 0)
return "arg" + (i - 1);
// -- no Code attribute for the LocalVariableTable attribute
if ((v.owner.flags() & Flags.ABSTRACT) != 0)
return "arg" + (i - 1);
// bridge methods use xN
if ((v.owner.flags() & Flags.BRIDGE) != 0)
return "x" + (i - 1);
// The rest of this method assumes the local conventions in the test program
Type t = v.type;
String s;
if (t.tag == TypeTags.CLASS)
s = ((ClassType) t).tsym.name.toString();
else
s = t.toString();
return String.valueOf(Character.toLowerCase(s.charAt(0))) + i;
}
Aggregations