Search in sources :

Example 6 with ProcessingEnvironment

use of javax.annotation.processing.ProcessingEnvironment in project spring-boot by spring-projects.

the class Trees method unwrap.

private static ProcessingEnvironment unwrap(ProcessingEnvironment wrapper) throws Exception {
    Field delegateField = wrapper.getClass().getDeclaredField("delegate");
    delegateField.setAccessible(true);
    return (ProcessingEnvironment) delegateField.get(wrapper);
}
Also used : Field(java.lang.reflect.Field) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment)

Example 7 with ProcessingEnvironment

use of javax.annotation.processing.ProcessingEnvironment in project dubbo by alibaba.

the class ServiceDefinitionBuilder method build.

static ServiceDefinition build(ProcessingEnvironment processingEnv, TypeElement type) {
    ServiceDefinition serviceDefinition = new ServiceDefinition();
    serviceDefinition.setCanonicalName(type.toString());
    serviceDefinition.setCodeSource(getResourceName(type.toString()));
    // Get all super types and interface excluding the specified type
    // and then the result will be added into ServiceDefinition#getTypes()
    getHierarchicalTypes(type.asType(), Object.class).stream().map(t -> TypeDefinitionBuilder.build(processingEnv, t)).forEach(serviceDefinition.getTypes()::add);
    // Get all declared methods that will be added into ServiceDefinition#getMethods()
    getPublicNonStaticMethods(type, Object.class).stream().map(method -> MethodDefinitionBuilder.build(processingEnv, method)).forEach(serviceDefinition.getMethods()::add);
    return serviceDefinition;
}
Also used : ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) MethodUtils.getPublicNonStaticMethods(org.apache.dubbo.metadata.annotation.processing.util.MethodUtils.getPublicNonStaticMethods) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) TypeUtils.getResourceName(org.apache.dubbo.metadata.annotation.processing.util.TypeUtils.getResourceName) TypeElement(javax.lang.model.element.TypeElement) TypeUtils.getHierarchicalTypes(org.apache.dubbo.metadata.annotation.processing.util.TypeUtils.getHierarchicalTypes) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition)

Example 8 with ProcessingEnvironment

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

the class LockVisitor method visitSynchronized.

/**
 * When visiting a synchronized block, issue an error if the expression has a type that implements
 * the java.util.concurrent.locks.Lock interface. This prevents explicit locks from being
 * accidentally used as built-in (monitor) locks. This is important because the Lock Checker does
 * not have a mechanism to separately keep track of the explicit lock and the monitor lock of an
 * expression that implements the Lock interface (i.e. there is a @LockHeld annotation used in
 * dataflow, but there are not distinct @MonitorLockHeld and @ExplicitLockHeld annotations). It is
 * assumed that both kinds of locks will never be held for any expression that implements Lock.
 *
 * <p>Additionally, a synchronized block may not be present in a method that has a @LockingFree
 * guarantee or stronger. An error is issued in this case.
 *
 * @param node the SynchronizedTree for the synchronized block being visited
 */
@Override
public Void visitSynchronized(SynchronizedTree node, Void p) {
    ProcessingEnvironment processingEnvironment = checker.getProcessingEnvironment();
    javax.lang.model.util.Types types = processingEnvironment.getTypeUtils();
    // TODO: make a type declaration annotation for this rather than looking for Lock.class
    // explicitly.
    TypeMirror lockInterfaceTypeMirror = TypesUtils.typeFromClass(Lock.class, types, processingEnvironment.getElementUtils());
    ExpressionTree synchronizedExpression = node.getExpression();
    ensureExpressionIsEffectivelyFinal(synchronizedExpression);
    TypeMirror expressionType = types.erasure(atypeFactory.getAnnotatedType(synchronizedExpression).getUnderlyingType());
    if (types.isSubtype(expressionType, lockInterfaceTypeMirror)) {
        checker.reportError(node, "explicit.lock.synchronized");
    }
    MethodTree enclosingMethod = TreePathUtil.enclosingMethod(atypeFactory.getPath(node));
    ExecutableElement methodElement = null;
    if (enclosingMethod != null) {
        methodElement = TreeUtils.elementFromDeclaration(enclosingMethod);
        SideEffectAnnotation seaOfContainingMethod = atypeFactory.methodSideEffectAnnotation(methodElement, false);
        if (!seaOfContainingMethod.isWeakerThan(SideEffectAnnotation.LOCKINGFREE)) {
            checker.reportError(node, "synchronized.block.in.lockingfree.method", seaOfContainingMethod);
        }
    }
    return super.visitSynchronized(node, p);
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) MethodTree(com.sun.source.tree.MethodTree) ExecutableElement(javax.lang.model.element.ExecutableElement) ExpressionTree(com.sun.source.tree.ExpressionTree) SideEffectAnnotation(org.checkerframework.checker.lock.LockAnnotatedTypeFactory.SideEffectAnnotation) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment)

Example 9 with ProcessingEnvironment

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

the class OptionalVisitor method isCallToIsPresent.

/**
 * Is the expression a call to {@code isPresent} or {@code isEmpty}? If not, returns null. If so,
 * returns a pair of (boolean, receiver expression). The boolean is true if the given expression
 * is a call to {@code isPresent} and is false if the given expression is a call to {@code
 * isEmpty}.
 *
 * @param expression an expression
 * @return a pair of a boolean (indicating whether the expression is a call to {@code
 *     Optional.isPresent} or to {@code Optional.isEmpty}) and its receiver; or null if not a call
 *     to either of the methods
 */
@Nullable
private Pair<Boolean, ExpressionTree> isCallToIsPresent(ExpressionTree expression) {
    ProcessingEnvironment env = checker.getProcessingEnvironment();
    boolean negate = false;
    while (true) {
        switch(expression.getKind()) {
            case PARENTHESIZED:
                expression = ((ParenthesizedTree) expression).getExpression();
                break;
            case LOGICAL_COMPLEMENT:
                expression = ((UnaryTree) expression).getExpression();
                negate = !negate;
                break;
            case METHOD_INVOCATION:
                if (TreeUtils.isMethodInvocation(expression, optionalIsPresent, env)) {
                    return Pair.of(!negate, TreeUtils.getReceiverTree(expression));
                } else if (optionalIsEmpty != null && TreeUtils.isMethodInvocation(expression, optionalIsEmpty, env)) {
                    return Pair.of(negate, TreeUtils.getReceiverTree(expression));
                } else {
                    return null;
                }
            default:
                return null;
        }
    }
}
Also used : ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 10 with ProcessingEnvironment

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

the class AnnotationFileElementTypes method parseStubFiles.

/**
 * Parses the stub files in the following order:
 *
 * <ol>
 *   <li>jdk.astub in the same directory as the checker, if it exists and ignorejdkastub option is
 *       not supplied
 *   <li>If parsing annotated JDK as stub files, all package-info.java files under the jdk/
 *       directory
 *   <li>Stub files listed in @StubFiles annotation on the checker; must be in same directory as
 *       the checker
 *   <li>Stub files returned by {@link BaseTypeChecker#getExtraStubFiles} (treated like those
 *       listed in @StubFiles annotation)
 *   <li>Stub files provided via {@code -Astubs} compiler option
 * </ol>
 *
 * <p>If a type is annotated with a qualifier from the same hierarchy in more than one stub file,
 * the qualifier in the last stub file is applied.
 *
 * <p>If using JDK 11, then the JDK stub files are only parsed if a type or declaration annotation
 * is requested from a class in that file.
 */
public void parseStubFiles() {
    parsing = true;
    BaseTypeChecker checker = factory.getChecker();
    ProcessingEnvironment processingEnv = factory.getProcessingEnv();
    // Only look in .jar files, and parse it right away.
    if (!checker.hasOption("ignorejdkastub")) {
        InputStream jdkStubIn = checker.getClass().getResourceAsStream("jdk.astub");
        if (jdkStubIn != null) {
            AnnotationFileParser.parseStubFile(checker.getClass().getResource("jdk.astub").toString(), jdkStubIn, factory, processingEnv, annotationFileAnnos, AnnotationFileType.BUILTIN_STUB);
        }
        String jdkVersionStub = "jdk" + annotatedJdkVersion + ".astub";
        InputStream jdkVersionStubIn = checker.getClass().getResourceAsStream(jdkVersionStub);
        if (jdkVersionStubIn != null) {
            AnnotationFileParser.parseStubFile(checker.getClass().getResource(jdkVersionStub).toString(), jdkVersionStubIn, factory, processingEnv, annotationFileAnnos, AnnotationFileType.BUILTIN_STUB);
        }
        // 2. Annotated JDK
        // This preps but does not parse the JDK files (except package-info.java files).
        // The JDK source code files will be parsed later, on demand.
        prepJdkStubs();
        // prepping the JDK parses all package-info.java files, which sets the `parsing` field to
        // false, so re-set it to true.
        parsing = true;
    }
    // 3. Stub files listed in @StubFiles annotation on the checker
    StubFiles stubFilesAnnotation = checker.getClass().getAnnotation(StubFiles.class);
    if (stubFilesAnnotation != null) {
        parseAnnotationFiles(Arrays.asList(stubFilesAnnotation.value()), AnnotationFileType.BUILTIN_STUB);
    }
    // 4. Stub files returned by the `getExtraStubFiles()` method
    parseAnnotationFiles(checker.getExtraStubFiles(), AnnotationFileType.BUILTIN_STUB);
    // 5. Stub files provided via -Astubs command-line option
    String stubsOption = checker.getOption("stubs");
    if (stubsOption != null) {
        parseAnnotationFiles(Arrays.asList(stubsOption.split(File.pathSeparator)), AnnotationFileType.COMMAND_LINE_STUB);
    }
    parsing = false;
}
Also used : StubFiles(org.checkerframework.framework.qual.StubFiles) BaseTypeChecker(org.checkerframework.common.basetype.BaseTypeChecker) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) 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