Search in sources :

Example 31 with Var

use of com.google.errorprone.annotations.Var in project java-common-lib by sosy-lab.

the class OptionCollector method getSourcePath.

/**
 * This method tries to get Source-Path. This path is used to get default values for options
 * without instantiating the classes.
 */
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
private static Path getSourcePath(CodeSource codeSource) throws URISyntaxException {
    // Get base folder for classes, go via URI to handle escaping
    @Var Path basePath = Paths.get(codeSource.getLocation().toURI());
    if (basePath.endsWith("bin")) {
        // this could be a usual eclipse environment, therefore src is the appropriate
        // folder to search for sources
        basePath = basePath.getParent();
    } else if (basePath.endsWith("build/classes/main")) {
        // this is a typical project layout for gradle, the sources should be in
        // src/main/java/
        basePath = basePath.getParent().getParent().getParent();
    }
    // gradle projects do also in eclipse have another folder for sources
    // so check which folder is the actual source folder
    List<Path> candidates = ImmutableList.of(Paths.get("src", "main", "java"), Paths.get("src"));
    for (Path candidate : candidates) {
        Path sourcePath = basePath.resolve(candidate);
        if (Files.isDirectory(sourcePath)) {
            return sourcePath;
        }
    }
    return basePath;
}
Also used : ClassPath(com.google.common.reflect.ClassPath) Path(java.nio.file.Path) Var(com.google.errorprone.annotations.Var) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 32 with Var

use of com.google.errorprone.annotations.Var in project infrautils by opendaylight.

the class LogRule method apply.

@Override
// findbugs-slf4j cannot understand  what we are doing here with a Logger variable instead of class field, so:
@SuppressFBWarnings({ "SLF4J_FORMAT_SHOULD_BE_CONST", "SLF4J_LOGGER_SHOULD_BE_PRIVATE" })
public Statement apply(Statement statement, Description description) {
    Logger testLog = LoggerFactory.getLogger(description.getTestClass());
    return new Statement() {

        @Override
        @SuppressWarnings("checkstyle:IllegalCatch")
        @SuppressFBWarnings("SLF4J_FORMAT_SHOULD_BE_CONST")
        public void evaluate() throws Throwable {
            testLog.info(MARKER, HEADER);
            testLog.info(MARKER, "BEGIN @Test {}()", description.getMethodName());
            long startTimeInMS = System.currentTimeMillis();
            @Var Throwable caughtThrowable = null;
            try {
                statement.evaluate();
            } catch (Throwable throwable) {
                caughtThrowable = throwable;
                throw throwable;
            } finally {
                long durationInMS = System.currentTimeMillis() - startTimeInMS;
                if (caughtThrowable == null) {
                    testLog.info(MARKER, MESSAGE, "ENDED", durationInMS, description.getMethodName());
                } else {
                    testLog.error(MARKER, MESSAGE, "FAILED", durationInMS, description.getMethodName(), caughtThrowable);
                }
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) Var(com.google.errorprone.annotations.Var) Logger(org.slf4j.Logger) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 33 with Var

use of com.google.errorprone.annotations.Var in project infrautils by opendaylight.

the class MBeanUtils method invokeMBeanFunction.

public static Object invokeMBeanFunction(String objName, String functionName) {
    @Var Object udpated = "";
    try {
        ObjectName objectName = new ObjectName(objName);
        MBeanServer mplatformMbeanServer = ManagementFactory.getPlatformMBeanServer();
        udpated = mplatformMbeanServer.invoke(objectName, functionName, null, null);
    } catch (InstanceNotFoundException | MBeanException | ReflectionException | MalformedObjectNameException t) {
        LOG.info("CRITICAL : Exception in executing MBean function");
    }
    return udpated;
}
Also used : ReflectionException(javax.management.ReflectionException) MalformedObjectNameException(javax.management.MalformedObjectNameException) Var(com.google.errorprone.annotations.Var) InstanceNotFoundException(javax.management.InstanceNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer)

Example 34 with Var

use of com.google.errorprone.annotations.Var in project infrautils by opendaylight.

the class MBeanUtils method readMBeanAttribute.

@Nullable
public static Object readMBeanAttribute(String objName, String attribute) {
    @Var Object attributeObj = null;
    try {
        ObjectName objectName = new ObjectName(objName);
        MBeanServer platformMbeanServer = ManagementFactory.getPlatformMBeanServer();
        attributeObj = platformMbeanServer.getAttribute(objectName, attribute);
    } catch (AttributeNotFoundException | InstanceNotFoundException | MBeanException | ReflectionException | MalformedObjectNameException t) {
        LOG.info("CRITICAL : Exception in executing MXBean function");
    }
    return attributeObj;
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MalformedObjectNameException(javax.management.MalformedObjectNameException) Var(com.google.errorprone.annotations.Var) InstanceNotFoundException(javax.management.InstanceNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) MBeanException(javax.management.MBeanException) UnicastRemoteObject(java.rmi.server.UnicastRemoteObject) ObjectName(javax.management.ObjectName) MBeanServer(javax.management.MBeanServer) Nullable(javax.annotation.Nullable)

Example 35 with Var

use of com.google.errorprone.annotations.Var in project infrautils by opendaylight.

the class AbstractMXBean method registerMBean.

/**
 * Registers this bean with the platform MBean server with the domain defined by
 * {@link #BASE_JMX_PREFIX}.
 *
 * @return true is successfully registered, false otherwise.
 */
protected final boolean registerMBean() {
    @Var boolean registered = false;
    try {
        // Object to identify MBean
        ObjectName mbeanObjectName = this.getMBeanObjectName();
        LOG.debug("Register MBean {}", mbeanObjectName);
        // unregistered if already registered
        if (server.isRegistered(mbeanObjectName)) {
            LOG.debug("MBean {} found to be already registered", mbeanObjectName);
            try {
                unregisterMBean(mbeanObjectName);
            } catch (MBeanRegistrationException | InstanceNotFoundException e) {
                LOG.warn("unregister mbean {} caused exception", mbeanObjectName, e);
            }
        }
        server.registerMBean(this, mbeanObjectName);
        registered = true;
        LOG.debug("MBean {} registered successfully", mbeanObjectName.getCanonicalName());
    } catch (InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException | MalformedObjectNameException e) {
        LOG.error("MBean {} registration failed", mbeanName, e);
    }
    return registered;
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) Var(com.google.errorprone.annotations.Var) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceNotFoundException(javax.management.InstanceNotFoundException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) ObjectName(javax.management.ObjectName)

Aggregations

Var (com.google.errorprone.annotations.Var)37 Test (org.junit.Test)9 Path (java.nio.file.Path)7 InstanceNotFoundException (javax.management.InstanceNotFoundException)6 MalformedObjectNameException (javax.management.MalformedObjectNameException)6 ObjectName (javax.management.ObjectName)6 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)5 ArrayList (java.util.ArrayList)4 Nullable (javax.annotation.Nullable)4 MBeanException (javax.management.MBeanException)4 MBeanServer (javax.management.MBeanServer)4 ReflectionException (javax.management.ReflectionException)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 IOException (java.io.IOException)3 AccessibleObject (java.lang.reflect.AccessibleObject)3 ClassPath (com.google.common.reflect.ClassPath)2 EqualsTester (com.google.common.testing.EqualsTester)2 UnicastRemoteObject (java.rmi.server.UnicastRemoteObject)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2