All Downloads are FREE. Search and download functionalities are using the official Maven repository.

checker.src.org.checkerframework.checker.nullness.SystemGetPropertyHandler Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java’s type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.42.0
Show newest version
package org.checkerframework.checker.nullness;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Arrays;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.*;

import org.checkerframework.framework.type.AnnotatedTypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType;
import org.checkerframework.javacutil.TreeUtils;

import com.sun.source.tree.*;

/**
 * Utility class for handling {@link java.lang.System#getProperty(String)}
 * invocations.
 *
 * 

* If the argument is a literal key that is guaranteed to be present in the * system properties may (according to the documentation of * {@link java.lang.System#getProperty(String)}), as in * {@code System.getProperties("line.separator")}, then the result of the method * call is assumed to be non-null. */ public class SystemGetPropertyHandler { protected final ProcessingEnvironment env; protected final NullnessAnnotatedTypeFactory factory; protected final ExecutableElement systemGetProperty; // This list is from the Javadoc of System.getProperties. Collection systemProperties = new HashSet<>(Arrays.asList( "java.version", "java.vendor", "java.vendor.url", "java.home", "java.vm.specification.version", "java.vm.specification.vendor", "java.vm.specification.name", "java.vm.version", "java.vm.vendor", "java.vm.name", "java.specification.version", "java.specification.vendor", "java.specification.name", "java.class.version", "java.class.path", "java.library.path", "java.io.tmpdir", "java.compiler", "java.ext.dirs", "os.name", "os.arch", "os.version", "file.separator", "path.separator", "line.separator", "user.name", "user.home", "user.dir")); public SystemGetPropertyHandler(ProcessingEnvironment env, NullnessAnnotatedTypeFactory factory) { this.env = env; this.factory = factory; systemGetProperty = TreeUtils.getMethod("java.lang.System", "getProperty", 1, env); } public void handle(MethodInvocationTree tree, AnnotatedExecutableType method) { if (TreeUtils.isMethodInvocation(tree, systemGetProperty, env)) { List args = tree.getArguments(); assert args.size() == 1; ExpressionTree arg = args.get(0); if (arg.getKind() == Tree.Kind.STRING_LITERAL) { String literal = (String) ((LiteralTree) arg).getValue(); if (systemProperties.contains(literal)) { AnnotatedTypeMirror type = method.getReturnType(); type.replaceAnnotation(factory.NONNULL); } } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy