use of com.sun.tools.javac.code.Types in project error-prone by google.
the class FunctionalInterfaceClash method functionalInterfaceSignature.
private static String functionalInterfaceSignature(VisitorState state, Type type) {
Types types = state.getTypes();
if (!maybeFunctionalInterface(type, types, state)) {
return Signatures.descriptor(type, types);
}
Type descriptorType = types.findDescriptorType(type);
List<Type> fiparams = descriptorType.getParameterTypes();
// Implicitly typed block-statement-bodied lambdas are potentially compatible with
// void-returning and value-returning functional interface types, so we don't consider return
// types in general. The except is nullary functional interfaces, since the lambda parameters
// will never be implicitly typed.
String result = fiparams.isEmpty() ? Signatures.descriptor(descriptorType.getReturnType(), types) : "_";
return String.format("(%s)->%s", fiparams.stream().map(t -> Signatures.descriptor(t, types)).collect(joining(",")), result);
}
use of com.sun.tools.javac.code.Types in project error-prone by google.
the class JUnit3FloatingPointComparisonWithoutDelta method removeMessageArgumentIfPresent.
/**
* Removes the message argument if it is present.
*/
private void removeMessageArgumentIfPresent(VisitorState state, List<Type> argumentTypes) {
if (argumentTypes.size() == 2) {
return;
}
Types types = state.getTypes();
Type firstType = argumentTypes.get(0);
if (types.isSameType(firstType, state.getSymtab().stringType)) {
argumentTypes.remove(0);
}
}
use of com.sun.tools.javac.code.Types in project j2objc by google.
the class TreeConverter method convertFunctionalExpression.
private TreeNode convertFunctionalExpression(JCFunctionalExpression node, TreePath parent, FunctionalExpression newNode) {
List<? extends TypeMirror> targets = getTargets(node, parent);
for (TypeMirror type : targets) {
newNode.addTargetType(type);
}
Types types = Types.instance(((com.sun.tools.javac.api.BasicJavacTask) env.task()).getContext());
return newNode.setTypeMirror(targets.iterator().next()).setDescriptor(new ExecutablePair((ExecutableElement) types.findDescriptorSymbol(((com.sun.tools.javac.code.Type) targets.get(0)).tsym), (ExecutableType) node.getDescriptorType(types)));
}
use of com.sun.tools.javac.code.Types in project bazel by bazelbuild.
the class InternalUtils method greatestLowerBound.
/**
* Returns the greatest lower bound of two {@link TypeMirror}s.
*
* @param processingEnv The {@link ProcessingEnvironment} to use.
* @param tm1 A {@link TypeMirror}.
* @param tm2 A {@link TypeMirror}.
* @return The greatest lower bound of {@code tm1} and {@code tm2}.
*/
public static TypeMirror greatestLowerBound(ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
Type t1 = (Type) tm1;
Type t2 = (Type) tm2;
JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
Types types = Types.instance(javacEnv.getContext());
if (types.isSameType(t1, t2)) {
// Special case if the two types are equal.
return t1;
}
// Handle the 'null' type manually.
if (t1.getKind() == TypeKind.NULL) {
return t1;
}
if (t2.getKind() == TypeKind.NULL) {
return t2;
}
// Special case for primitives.
if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
if (types.isAssignable(t1, t2)) {
return t1;
} else if (types.isAssignable(t2, t1)) {
return t2;
} else {
// instead.
return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
}
}
if (t1.getKind() == TypeKind.WILDCARD) {
return t2;
}
if (t2.getKind() == TypeKind.WILDCARD) {
return t1;
}
return types.glb(t1, t2);
}
use of com.sun.tools.javac.code.Types in project error-prone by google.
the class BundleDeserializationCast method matchTypeCast.
@Override
public Description matchTypeCast(TypeCastTree tree, VisitorState state) {
if (!BUNDLE_DESERIALIZATION_CAST_EXPRESSION.matches(tree, state)) {
return NO_MATCH;
}
Tree targetType = tree.getType();
// Casting to primitive types shouldn't cause issues since they extend no type and are final.
if (isPrimitiveType().matches(targetType, state)) {
return NO_MATCH;
}
/*
* Ordering of these checks determines precedence of types (which type *should* be cast to).
* Deduced by inspecting serialization code, see
* https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/Parcel.java#1377
* Simply allow specially handled final types, and check that other specially handled types
* are cast to their base types.
*
* There is no documentation of what types are safe to cast to, so this code is paralleling the
* code cited above to emulate the same logic in order to produce the correct behavior.
*/
if (isSameType("java.lang.String").matches(targetType, state)) {
return NO_MATCH;
}
if (isSubtypeOf("java.util.Map").matches(targetType, state)) {
// Make an exception for HashMap.
return anyOf(isSameType("java.util.Map"), isSameType("java.util.HashMap")).matches(targetType, state) ? NO_MATCH : getDescriptionForType(tree, "Map");
}
// All Parcelables handled after this point have their types preserved.
if (isSubtypeOf("android.os.Parcelable").matches(targetType, state)) {
return NO_MATCH;
}
if (isSubtypeOf("java.lang.CharSequence").matches(targetType, state)) {
return isSameType("java.lang.CharSequence").matches(targetType, state) ? NO_MATCH : getDescriptionForType(tree, "CharSequence");
}
if (isSubtypeOf("java.util.List").matches(targetType, state)) {
// Make an exception for ArrayList.
return anyOf(isSameType("java.util.List"), isSameType("java.util.ArrayList")).matches(targetType, state) ? NO_MATCH : getDescriptionForType(tree, "List");
}
if (isSubtypeOf("android.util.SparseArray").matches(targetType, state)) {
return isSameType("android.util.SparseArray").matches(targetType, state) ? NO_MATCH : getDescriptionForType(tree, "SparseArray");
}
// Check component types of arrays. The only type that may cause problems is CharSequence[].
if (isArrayType().matches(targetType, state)) {
Type componentType = ((ArrayType) getType(targetType)).getComponentType();
Types types = state.getTypes();
Type charSequenceType = typeFromString("java.lang.CharSequence").get(state);
Type stringType = typeFromString("java.lang.String").get(state);
// in the serialization code.
if (types.isSubtype(componentType, charSequenceType) && !types.isSameType(componentType, charSequenceType) && !types.isSameType(componentType, stringType)) {
return getDescriptionForType(tree, "CharSequence[]");
}
}
return NO_MATCH;
}
Aggregations