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

src.samples.java.ex.WOC_Sample Maven / Gradle / Ivy

Go to download

An auxiliary findbugs.sourceforge.net plugin for java bug detectors that fall outside the narrow scope of detectors to be packaged with the product itself.

The newest version!
package ex;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import com.google.common.collect.Sets;

public class WOC_Sample {
    // tag WOC_WRITE_ONLY_COLLECTION_FIELD
    private final Set memberSet = new HashSet();
    private Set fpSet;
    private final List fpList = new ArrayList();
    private List fp1 = new ArrayList();
    private List fp2 = new ArrayList();
    private List fp3;

    public WOC_Sample(List x) {
        fp3 = x;
    }

    public void testWOCSimple() {
        // tag WOC_WRITE_ONLY_COLLECTION_LOCAL
        Set s = new HashSet();
        s.add("Foo");
        memberSet.add("fee");
        if (fpSet.retainAll(new HashSet())) {
            System.out.println("woops");
        }
    }

    public void testGuavaWOCSimple() {
        // tag WOC_WRITE_ONLY_COLLECTION_LOCAL
        Set s = Sets.newHashSet();
        s.add("Foo");
        memberSet.add("fee");
        if (fpSet.retainAll(new HashSet())) {
            System.out.println("woops");
        }
    }

    public void testIterateEmpty() {
        Set ss = new HashSet();

        for (String s : ss) {
        }
    }

    public Map testFPWOCReturn() {
        // no tag, value is returned
        Map m = new HashMap();
        m.put("Foo", "Bar");
        memberSet.add("fi");
        fpSet = new HashSet();
        return m;
    }

    public void testFPWOCAsParm() {
        // no tag, passed to helper function
        Map m = new HashMap();
        m.put("Foo", "Bar");
        memberSet.add("fo");
        fpSet.add("boo");
        helper(0, m);

        List l = new ArrayList();

        l.add("a");
        l.add("b");

        doSomethingWithList(l);
    }

    public void testFPWOCCopy() {
        // no tag, reference is copied
        Set s = new LinkedHashSet();
        s.add("foo");
        @SuppressWarnings("unused")
        Set c = s;
        memberSet.add("fum");
    }

    public void testFPWOCInArray() {
        // no tag, object is added to array
        Vector v = new Vector();
        v.add(Integer.valueOf(0));
        Object[] o = new Object[] { v };
    }

    public void testFPWOCUseReturnVal() {
        // no tag, return value was looked at
        LinkedList l = new LinkedList();
        l.add("Foo");
        l.add("Bar");

        if (l.remove("Foo")) {
            System.out.println("Dont' report");
        }
    }

    public Set testFPTernary(boolean b) {
        Set s = new HashSet();
        s.add("foo");
        s.add("bar");

        return b ? s : Collections.emptySet();
    }

    private void helper(int i, Map x) {
        System.out.println(x.get(String.valueOf(i)));
    }

    private void doSomethingWithList(List l) {
        for (String s : l) {
            System.out.println(s);
        }
    }

    // no tag, put in anonymous class
    public void testFPInnerClass(final Set data) {

        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                data.add("Woot");
            }
        };
        System.out.println(al);
    }

    public List fpOtherInstance(WOC_Sample ws) {
        return ws.fpList;
    }

    public String fpCheckReference(boolean b) {
        // no tag, null check done
        List s = null;

        if (b) {
            s = new ArrayList();
            s.add("foo");
        }

        String result;
        if (s != null) {
            result = "yes";
        } else {
            result = "no";
        }

        return result;
    }

    public List fpWOCTernary(boolean a, boolean b) {

        if (a) {
            return b ? fp1 : fp2;
        }

        // no tag, either could be returned in the ternary operator
        List used1 = new ArrayList();
        List used2 = new ArrayList();

        return b ? used1 : used2;
    }

    public void fpWOCAllowToMap(Map> m, List l) {
        if (l == null) {
            m.put("FP", l = new ArrayList());
        }
        l.add("Hello there");
    }

    public void fpClone(List l) {
        HashSet s = new HashSet<>();
        for (Data d : l) {
            d.ss = (Set) s.clone();
        }
    }

    public static class FpContains {
        private List fpSetList;

        public FpContains() {
            fpSetList = new ArrayList();
        }

        public void add() {
            fpSetList.add("Foo");
        }

        protected void contains() {
            for (int i = 0; i < 10; i++) {
                if (fpSetList.get(i) != null) {
                    System.out.println("Contains");
                }
            }
        }
    }

    public void fpAddToCtorParm(String x) {
        fp3.add(x);
    }

    private class Data {
        Set ss;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy