/* * Copyright (c) 2013 - present Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ package codetoanalyze.java.eradicate; import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.Present; import com.google.common.base.Optional; public class PresentTest { void argPresent(@Present Optional present, Optional absent) { } void testPresent(@Present Optional present, Optional absent) { argPresent(present, absent); // OK argPresent(present, present); // OK argPresent(present, absent); // OK argPresent(absent, absent); // Bad } class TestPresentAnnotationBasic { void testBasicConditional(Optional o) { if (o.isPresent()) { o.get(); } } Optional absent = Optional.absent(); @Present Optional present = Optional.of("abc"); @Present Optional returnPresent() { if (absent.isPresent()) { return absent; } else return Optional.of("abc"); } @Present Optional returnPresentBad() { absent.get(); // Bad: get is unsafe return absent; // Bad: should return present } void expectPresent(@Present Optional x) { } void bar() { expectPresent(present); String s; s = returnPresent().get(); s = present.get(); Assertions.assertCondition(absent.isPresent()); expectPresent(absent); } void testOptionalAbsent() { expectPresent(Optional.absent()); // Bad } } class TestPresentFieldOfInnerClass { class D { @SuppressFieldNotInitialized Optional s; } class D1 { // Different bytecode generated when the field is private @SuppressFieldNotInitialized private Optional s; } void testD(D d) { if (d.s.isPresent()) { d.s.get(); } } void testD1(D1 d1) { if (d1.s.isPresent()) { d1.s.get(); } } void testD1Condition(D1 d1) { Assertions.assertCondition(d1.s.isPresent()); d1.s.get(); } } }