Search in sources :

Example 11 with ProcessingEnvironment

use of javax.annotation.processing.ProcessingEnvironment in project checker-framework by typetools.

the class SourceChecker method unwrapIntelliJ.

/**
 * Tries to unwrap ProcessingEnvironment from proxy in IntelliJ 2020.3 or later.
 *
 * @param env possibly a dynamic proxy wrapping processing environment
 * @return unwrapped processing environment, null if not successful
 */
@Nullable
private static ProcessingEnvironment unwrapIntelliJ(ProcessingEnvironment env) {
    if (!Proxy.isProxyClass(env.getClass())) {
        return null;
    }
    InvocationHandler handler = Proxy.getInvocationHandler(env);
    try {
        Field field = handler.getClass().getDeclaredField("val$delegateTo");
        field.setAccessible(true);
        Object o = field.get(handler);
        if (o instanceof ProcessingEnvironment) {
            return (ProcessingEnvironment) o;
        }
        return null;
    } catch (NoSuchFieldException | IllegalAccessException e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) InvocationHandler(java.lang.reflect.InvocationHandler) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 12 with ProcessingEnvironment

use of javax.annotation.processing.ProcessingEnvironment in project checker-framework by typetools.

the class CFAbstractValue method upperBound.

private V upperBound(@Nullable V other, boolean shouldWiden) {
    if (other == null) {
        @SuppressWarnings("unchecked") V v = (V) this;
        return v;
    }
    ProcessingEnvironment processingEnv = analysis.getTypeFactory().getProcessingEnv();
    TypeMirror lubTypeMirror = TypesUtils.leastUpperBound(this.getUnderlyingType(), other.getUnderlyingType(), processingEnv);
    ValueLub valueLub = new ValueLub(shouldWiden);
    Set<AnnotationMirror> lub = valueLub.combineSets(this.getUnderlyingType(), this.getAnnotations(), other.getUnderlyingType(), other.getAnnotations(), canBeMissingAnnotations(lubTypeMirror));
    return analysis.createAbstractValue(lub, lubTypeMirror);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment)

Example 13 with ProcessingEnvironment

use of javax.annotation.processing.ProcessingEnvironment in project checker-framework by typetools.

the class CFAbstractValue method greatestLowerBound.

/**
 * Compute the greatest lower bound of two values.
 *
 * <p><em>Important</em>: This method must fulfill the following contract:
 *
 * <ul>
 *   <li>Does not change {@code this}.
 *   <li>Does not change {@code other}.
 *   <li>Returns a fresh object which is not aliased yet.
 *   <li>Returns an object of the same (dynamic) type as {@code this}, even if the signature is
 *       more permissive.
 *   <li>Is commutative.
 * </ul>
 *
 * @param other another value
 * @return the greatest lower bound of two values
 */
public V greatestLowerBound(@Nullable V other) {
    if (other == null) {
        @SuppressWarnings("unchecked") V v = (V) this;
        return v;
    }
    ProcessingEnvironment processingEnv = analysis.getTypeFactory().getProcessingEnv();
    TypeMirror glbTypeMirror = TypesUtils.greatestLowerBound(this.getUnderlyingType(), other.getUnderlyingType(), processingEnv);
    ValueGlb valueGlb = new ValueGlb();
    Set<AnnotationMirror> glb = valueGlb.combineSets(this.getUnderlyingType(), this.getAnnotations(), other.getUnderlyingType(), other.getAnnotations(), canBeMissingAnnotations(glbTypeMirror));
    return analysis.createAbstractValue(glb, glbTypeMirror);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment)

Example 14 with ProcessingEnvironment

use of javax.annotation.processing.ProcessingEnvironment in project checker-framework by typetools.

the class SourceChecker method unwrapGradle.

/**
 * Tries to unwrap processing environment in Gradle incremental processing. Inspired by project
 * Lombok.
 *
 * @param delegateClass a class in which to find a {@code delegate} field
 * @param env a processing environment wrapper
 * @return unwrapped processing environment, null if not successful
 */
@Nullable
private static ProcessingEnvironment unwrapGradle(Class<?> delegateClass, ProcessingEnvironment env) {
    try {
        Field field = delegateClass.getDeclaredField("delegate");
        field.setAccessible(true);
        Object o = field.get(env);
        if (o instanceof ProcessingEnvironment) {
            return (ProcessingEnvironment) o;
        }
        return null;
    } catch (NoSuchFieldException | IllegalAccessException e) {
        return null;
    }
}
Also used : Field(java.lang.reflect.Field) JavacProcessingEnvironment(com.sun.tools.javac.processing.JavacProcessingEnvironment) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 15 with ProcessingEnvironment

use of javax.annotation.processing.ProcessingEnvironment in project butterknife by JakeWharton.

the class ButterKnifeProcessor method init.

@Override
public synchronized void init(ProcessingEnvironment env) {
    super.init(env);
    String sdk = env.getOptions().get(OPTION_SDK_INT);
    if (sdk != null) {
        try {
            this.sdk = Integer.parseInt(sdk);
        } catch (NumberFormatException e) {
            env.getMessager().printMessage(Kind.WARNING, "Unable to parse supplied minSdk option '" + sdk + "'. Falling back to API 1 support.");
        }
    }
    debuggable = !"false".equals(env.getOptions().get(OPTION_DEBUGGABLE));
    typeUtils = env.getTypeUtils();
    filer = env.getFiler();
    try {
        trees = Trees.instance(processingEnv);
    } catch (IllegalArgumentException ignored) {
        try {
            // Get original ProcessingEnvironment from Gradle-wrapped one or KAPT-wrapped one.
            for (Field field : processingEnv.getClass().getDeclaredFields()) {
                if (field.getName().equals("delegate") || field.getName().equals("processingEnv")) {
                    field.setAccessible(true);
                    ProcessingEnvironment javacEnv = (ProcessingEnvironment) field.get(processingEnv);
                    trees = Trees.instance(javacEnv);
                    break;
                }
            }
        } catch (Throwable ignored2) {
        }
    }
}
Also used : Field(java.lang.reflect.Field) BindString(butterknife.BindString) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment)

Aggregations

ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)38 TypeMirror (javax.lang.model.type.TypeMirror)12 TypeElement (javax.lang.model.element.TypeElement)11 ExecutableElement (javax.lang.model.element.ExecutableElement)8 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)7 JavacProcessingEnvironment (com.sun.tools.javac.processing.JavacProcessingEnvironment)6 Set (java.util.Set)6 Modifier (javax.lang.model.element.Modifier)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 Element (javax.lang.model.element.Element)5 Field (java.lang.reflect.Field)4 VariableElement (javax.lang.model.element.VariableElement)4 Elements (javax.lang.model.util.Elements)4 Types (javax.lang.model.util.Types)4 Collections (java.util.Collections)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3