Search in sources :

Example 1 with Implements

use of org.robolectric.annotation.Implements in project robolectric by robolectric.

the class ShadowMap method getShadowInfo.

public static ShadowInfo getShadowInfo(Class<?> clazz) {
    Implements annotation = clazz.getAnnotation(Implements.class);
    if (annotation == null) {
        throw new IllegalArgumentException(clazz + " is not annotated with @Implements");
    }
    String className = annotation.className();
    if (className.isEmpty()) {
        className = annotation.value().getName();
    }
    return new ShadowInfo(className, new ShadowConfig(clazz.getName(), annotation));
}
Also used : Implements(org.robolectric.annotation.Implements)

Example 2 with Implements

use of org.robolectric.annotation.Implements in project robolectric by robolectric.

the class ShadowWrangler method getShadowedClass.

private Class<?> getShadowedClass(Method shadowMethod) {
    Class<?> shadowingClass = shadowMethod.getDeclaringClass();
    if (shadowingClass.equals(Object.class)) {
        return Object.class;
    }
    Implements implementsAnnotation = shadowingClass.getAnnotation(Implements.class);
    if (implementsAnnotation == null) {
        throw new RuntimeException(shadowingClass + " has no @" + Implements.class.getSimpleName() + " annotation");
    }
    String shadowedClassName = implementsAnnotation.className();
    if (shadowedClassName.isEmpty()) {
        return implementsAnnotation.value();
    } else {
        try {
            return shadowingClass.getClassLoader().loadClass(shadowedClassName);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : Implements(org.robolectric.annotation.Implements) RealObject(org.robolectric.annotation.RealObject)

Example 3 with Implements

use of org.robolectric.annotation.Implements in project robolectric by robolectric.

the class ShadowMap method obtainShadowInfo.

static ShadowInfo obtainShadowInfo(Class<?> clazz, boolean mayBeNonShadow) {
    Implements annotation = clazz.getAnnotation(Implements.class);
    if (annotation == null) {
        if (mayBeNonShadow) {
            return null;
        } else {
            throw new IllegalArgumentException(clazz + " is not annotated with @Implements");
        }
    }
    String className = annotation.className();
    if (className.isEmpty()) {
        className = annotation.value().getName();
    }
    return new ShadowInfo(className, clazz.getName(), annotation);
}
Also used : Implements(org.robolectric.annotation.Implements)

Example 4 with Implements

use of org.robolectric.annotation.Implements in project robolectric by robolectric.

the class ShadowProviderGenerator method generate.

void generate(String shadowPackage, PrintWriter writer) {
    writer.print("package " + shadowPackage + ";\n");
    for (String name : model.getImports()) {
        writer.println("import " + name + ';');
    }
    writer.println();
    writer.println("/**");
    writer.println(" * Shadow mapper. Automatically generated by the Robolectric Annotation Processor.");
    writer.println(" */");
    writer.println("@Generated(\"" + RobolectricProcessor.class.getCanonicalName() + "\")");
    writer.println("@SuppressWarnings({\"unchecked\",\"deprecation\"})");
    writer.println("public class " + GEN_CLASS + " implements ShadowProvider {");
    final int shadowSize = model.getAllShadowTypes().size();
    writer.println("  private static final Map<String, String> SHADOW_MAP = new HashMap<>(" + shadowSize + ");");
    writer.println();
    writer.println("  static {");
    for (Map.Entry<TypeElement, TypeElement> entry : model.getAllShadowTypes().entrySet()) {
        final String shadow = elements.getBinaryName(entry.getKey()).toString();
        final String actual = entry.getValue().getQualifiedName().toString();
        writer.println("    SHADOW_MAP.put(\"" + actual + "\", \"" + shadow + "\");");
    }
    for (Map.Entry<String, String> entry : model.getExtraShadowTypes().entrySet()) {
        final String shadow = entry.getKey();
        final String actual = entry.getValue();
        writer.println("    SHADOW_MAP.put(\"" + actual + "\", \"" + shadow + "\");");
    }
    writer.println("  }");
    writer.println();
    for (Map.Entry<TypeElement, TypeElement> entry : model.getShadowOfMap().entrySet()) {
        final TypeElement shadowType = entry.getKey();
        final TypeElement actualType = entry.getValue();
        if (!actualType.getModifiers().contains(Modifier.PUBLIC)) {
            continue;
        }
        int paramCount = 0;
        StringBuilder paramDef = new StringBuilder("<");
        StringBuilder paramUse = new StringBuilder("<");
        for (TypeParameterElement typeParam : entry.getValue().getTypeParameters()) {
            if (paramCount > 0) {
                paramDef.append(',');
                paramUse.append(',');
            }
            boolean first = true;
            paramDef.append(typeParam);
            paramUse.append(typeParam);
            for (TypeMirror bound : model.getExplicitBounds(typeParam)) {
                if (first) {
                    paramDef.append(" extends ");
                    first = false;
                } else {
                    paramDef.append(" & ");
                }
                paramDef.append(model.getReferentFor(bound));
            }
            paramCount++;
        }
        String paramDefStr = "";
        String paramUseStr = "";
        if (paramCount > 0) {
            paramDefStr = paramDef.append("> ").toString();
            paramUseStr = paramUse.append('>').toString();
        }
        final String actual = model.getReferentFor(actualType) + paramUseStr;
        final String shadow = model.getReferentFor(shadowType) + paramUseStr;
        if (shadowType.getAnnotation(Deprecated.class) != null) {
            writer.println("  @Deprecated");
        }
        writer.println("  public static " + paramDefStr + shadow + " shadowOf(" + actual + " actual) {");
        writer.println("    return (" + shadow + ") ShadowExtractor.extract(actual);");
        writer.println("  }");
        writer.println();
    }
    writer.println("  public void reset() {");
    for (Map.Entry<TypeElement, ExecutableElement> entry : model.getResetters().entrySet()) {
        Implements annotation = entry.getKey().getAnnotation(Implements.class);
        int minSdk = annotation.minSdk();
        int maxSdk = annotation.maxSdk();
        String ifClause;
        if (minSdk != -1 && maxSdk != -1) {
            ifClause = "if (org.robolectric.RuntimeEnvironment.getApiLevel() >= " + minSdk + " && org.robolectric.RuntimeEnvironment.getApiLevel() <= " + maxSdk + ") ";
        } else if (maxSdk != -1) {
            ifClause = "if (org.robolectric.RuntimeEnvironment.getApiLevel() <= " + maxSdk + ") ";
        } else if (minSdk != -1) {
            ifClause = "if (org.robolectric.RuntimeEnvironment.getApiLevel() >= " + minSdk + ") ";
        } else {
            ifClause = "";
        }
        writer.println("    " + ifClause + model.getReferentFor(entry.getKey()) + "." + entry.getValue().getSimpleName() + "();");
    }
    writer.println("  }");
    writer.println();
    writer.println("  @Override");
    writer.println("  public Map<String, String> getShadowMap() {");
    writer.println("    return SHADOW_MAP;");
    writer.println("  }");
    writer.println();
    writer.println("  @Override");
    writer.println("  public String[] getProvidedPackageNames() {");
    writer.println("    return new String[] {");
    if (shouldInstrumentPackages) {
        writer.println("      " + Joiner.on(",\n      ").join(model.getShadowedPackages()));
    }
    writer.println("    };");
    writer.println("  }");
    writer.println('}');
}
Also used : TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) RobolectricProcessor(org.robolectric.annotation.processing.RobolectricProcessor) TypeParameterElement(javax.lang.model.element.TypeParameterElement) Implements(org.robolectric.annotation.Implements) TypeMirror(javax.lang.model.type.TypeMirror) Map(java.util.Map)

Example 5 with Implements

use of org.robolectric.annotation.Implements in project robolectric by robolectric.

the class ShadowProviderGeneratorTest method type.

private TypeElement type(String shadowClassName, int minSdk, int maxSdk) {
    TypeElement shadowType = mock(TypeElement.class);
    when(model.getReferentFor(shadowType)).thenReturn(shadowClassName);
    Implements implAnnotation = mock(Implements.class);
    when(implAnnotation.minSdk()).thenReturn(minSdk);
    when(implAnnotation.maxSdk()).thenReturn(maxSdk);
    when(shadowType.getAnnotation(Implements.class)).thenReturn(implAnnotation);
    return shadowType;
}
Also used : Implements(org.robolectric.annotation.Implements) TypeElement(javax.lang.model.element.TypeElement)

Aggregations

Implements (org.robolectric.annotation.Implements)7 TypeElement (javax.lang.model.element.TypeElement)2 Map (java.util.Map)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeParameterElement (javax.lang.model.element.TypeParameterElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1 RealObject (org.robolectric.annotation.RealObject)1 RobolectricProcessor (org.robolectric.annotation.processing.RobolectricProcessor)1