public static ReferenceType getGenericTypeForContainer(VariableElement field) { TypeMirror fieldType = field.asType(); TypeKind kind = fieldType.getKind(); if (kind != TypeKind.DECLARED) { return null; } List<? extends TypeMirror> args = ((DeclaredType) fieldType).getTypeArguments(); if (args.size() <= 0) { return null; } fieldType = args.get(0); kind = fieldType.getKind(); // We also support RealmList<byte[]> if (kind != TypeKind.DECLARED && kind != TypeKind.ARRAY) { return null; } return (ReferenceType) fieldType; }
public static String getRealmResultsType(VariableElement field) { if (!Utils.isRealmResults(field)) { return null; } ReferenceType type = getGenericTypeForContainer(field); if (null == type) { return null; } return type.toString(); }
public static String getRealmListType(VariableElement field) { if (!Utils.isRealmList(field)) { return null; } ReferenceType type = getGenericTypeForContainer(field); if (null == type) { return null; } return type.toString(); }
/** * @return the simple type name for a field. */ public static String getFieldTypeSimpleName(ReferenceType type) { return (null == type) ? null : getFieldTypeSimpleName(type.toString()); }
/** * Assignment conversion and method invocation conversion are almost * identical, except that assignment conversion allows narrowing. We * factor out the common logic here. * * @param node * a Node producing a value * @param varType * the type of a variable * @param contextAllowsNarrowing * whether to allow narrowing (for assignment conversion) or * not (for method invocation conversion) * @return a Node with the value converted to the type of the variable, * which may be the input node itself */ protected Node commonConvert(Node node, TypeMirror varType, boolean contextAllowsNarrowing) { // For assignment conversion, see JLS 5.2 // For method invocation conversion, see JLS 5.3 // Check for identical types or "identity conversion" TypeMirror nodeType = node.getType(); boolean isSameType = types.isSameType(nodeType, varType); if (isSameType) { return node; } boolean isRightNumeric = TypesUtils.isNumeric(nodeType); boolean isRightPrimitive = TypesUtils.isPrimitive(nodeType); boolean isRightBoxed = TypesUtils.isBoxedPrimitive(nodeType); boolean isRightReference = nodeType instanceof ReferenceType; boolean isLeftNumeric = TypesUtils.isNumeric(varType); boolean isLeftPrimitive = TypesUtils.isPrimitive(varType); // boolean isLeftBoxed = TypesUtils.isBoxedPrimitive(varType); boolean isLeftReference = varType instanceof ReferenceType; boolean isSubtype = types.isSubtype(nodeType, varType); if (isRightNumeric && isLeftNumeric && isSubtype) { node = widen(node, varType); nodeType = node.getType(); } else if (isRightReference && isLeftReference && isSubtype) { // widening reference conversion is a no-op, but if it // applies, then later conversions do not. } else if (isRightPrimitive && isLeftReference) { if (contextAllowsNarrowing && conversionRequiresNarrowing(varType, node)) { node = narrowAndBox(node, varType); nodeType = node.getType(); } else { node = box(node); nodeType = node.getType(); } } else if (isRightBoxed && isLeftPrimitive) { node = unbox(node); nodeType = node.getType(); if (types.isSubtype(nodeType, varType) && !types.isSameType(nodeType, varType)) { node = widen(node, varType); nodeType = node.getType(); } } else if (isRightPrimitive && isLeftPrimitive) { if (contextAllowsNarrowing && conversionRequiresNarrowing(varType, node)) { node = narrow(node, varType); nodeType = node.getType(); } } // TODO: if checkers need to know about null references of // a particular type, add logic for them here. return node; }
/** * Assignment conversion and method invocation conversion are almost identical, except that * assignment conversion allows narrowing. We factor out the common logic here. * * @param node a Node producing a value * @param varType the type of a variable * @param contextAllowsNarrowing whether to allow narrowing (for assignment conversion) or * not (for method invocation conversion) * @return a Node with the value converted to the type of the variable, which may be the * input node itself */ protected Node commonConvert( Node node, TypeMirror varType, boolean contextAllowsNarrowing) { // For assignment conversion, see JLS 5.2 // For method invocation conversion, see JLS 5.3 // Check for identical types or "identity conversion" TypeMirror nodeType = node.getType(); boolean isSameType = types.isSameType(nodeType, varType); if (isSameType) { return node; } boolean isRightNumeric = TypesUtils.isNumeric(nodeType); boolean isRightPrimitive = TypesUtils.isPrimitive(nodeType); boolean isRightBoxed = TypesUtils.isBoxedPrimitive(nodeType); boolean isRightReference = nodeType instanceof ReferenceType; boolean isLeftNumeric = TypesUtils.isNumeric(varType); boolean isLeftPrimitive = TypesUtils.isPrimitive(varType); // boolean isLeftBoxed = TypesUtils.isBoxedPrimitive(varType); boolean isLeftReference = varType instanceof ReferenceType; boolean isSubtype = types.isSubtype(nodeType, varType); if (isRightNumeric && isLeftNumeric && isSubtype) { node = widen(node, varType); nodeType = node.getType(); } else if (isRightReference && isLeftReference && isSubtype) { // widening reference conversion is a no-op, but if it // applies, then later conversions do not. } else if (isRightPrimitive && isLeftReference) { if (contextAllowsNarrowing && conversionRequiresNarrowing(varType, node)) { node = narrowAndBox(node, varType); nodeType = node.getType(); } else { node = box(node); nodeType = node.getType(); } } else if (isRightBoxed && isLeftPrimitive) { node = unbox(node); nodeType = node.getType(); if (types.isSubtype(nodeType, varType) && !types.isSameType(nodeType, varType)) { node = widen(node, varType); nodeType = node.getType(); } } else if (isRightPrimitive && isLeftPrimitive) { if (contextAllowsNarrowing && conversionRequiresNarrowing(varType, node)) { node = narrow(node, varType); nodeType = node.getType(); } } // TODO: if checkers need to know about null references of // a particular type, add logic for them here. return node; }