summaryrefslogtreecommitdiff
path: root/plugins/InspectionGadgets/test/com/siyeh/igtest/abstraction/weaken_type/TypeMayBeWeakened.java
blob: 370aba53a19c1d22b478b243d716b09e1a650f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.siyeh.igtest.abstraction.weaken_type;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class TypeMayBeWeakened {

    void weakness() {
        Map<Integer, String> map = new HashMap<Integer, String>();
        Integer key = 34;
        map.get(key);
    }

    public static String hashes(int len) {
        StringBuilder sb = new StringBuilder();
        for(int i=0;i<len;i++)
            sb.append('#');
        return sb.toString();
    }


    public static int exp(Iterable<String> list) {
        int i = 0;
        for (String s: list) {
            if ("some".equals(s)) {
                i++;
            }
        }
        return i;
    }

    class WeakBoolean {
        private Boolean myBool;

        WeakBoolean() {
            myBool = true;
        }

        public void inverse() {
            myBool = !myBool;
        }

        public void xor(boolean b) {
            myBool ^= b;
        }
    }

    void bar() {
        foo(new WeakBoolean());
    }

    void foo(WeakBoolean <warning descr="Type of parameter 'b' may be weakened to 'java.lang.Object'">b</warning>) {
        System.out.println("b: " + b);
    }

    String foo(String s) {
        return s + 1;
    }

    private static void method() throws IllegalArgumentException {
        try {
            FileInputStream fis=new FileInputStream("/etc/modules");
        }
        catch(FileNotFoundException fnfex) {
            IllegalArgumentException <warning descr="Type of variable 'iaex' may be weakened to 'java.lang.RuntimeException'">iaex</warning>=new IllegalArgumentException("Exception Message");
            iaex.initCause(fnfex);
            throw iaex;
        }
    }

    public static void method(String weakened) {
        acceptsArray(new String[] {weakened});
    }

    public static void acceptsArray(String[] sarray) {}

    class Test {
        int x;
        void foo() { Test f = new Test(); f.x++; }
    }

    void listy(ArrayList <warning descr="Type of parameter 'list' may be weakened to 'java.lang.Iterable'">list</warning>) {
        for (Object o : list) {

        }
    }

    void construct() {
        final TypeMayBeWeakened var = new TypeMayBeWeakened();
        var.new Test();
    }

    class Param2Weak<T> {
        public void method(T p) {}
    }

    public class Weak2Null extends Param2Weak<String> {
        public void context(String p) {
            method(p);
        }
    }

    void simpleIf(Boolean condition) {
        if (condition);
    }

    void simpleFor(Boolean condition) {
        for (;condition;);
    }

    void simpleSwitch(Integer value) {
        switch (value) {

        }
    }

    Integer simpleConditional(Boolean condition, Integer value1, Integer value2) {
        return condition ? value1 : value2;
    }

    private static <T, V extends T> java.util.concurrent.atomic.AtomicReference<T> nullSafeReference(java.util.concurrent.atomic.AtomicReference<T> ref, V value) {
        if (ref != null) ref.set(value);
        return ref;
    }
}
class MyClass  {

  public MyClass(java.util.Date date, String[] classNames) {}

  static MyClass readMyClass(final ObjectInputStream <warning descr="Type of parameter 'objectInput' may be weakened to 'com.siyeh.igtest.abstraction.weaken_type.DataInput'">objectInput</warning>) {
    final long time = objectInput.readLong();
    final int size = objectInput.readInt();
    final String[] classNames = new String[size];
    for (int i = 0; i < size; i++) {
      classNames[i] = objectInput.readUTF();
    }
    return new MyClass(new java.util.Date(time), classNames);
  }
}
interface DataInput {
  long readLong();
  int readInt();
  String readUTF();
}
abstract class ObjectInputStream implements DataInput {

  public String readUTF() {
    return null;
  }
}
class Test implements Foo2 {
  void test(Test <warning descr="Type of parameter 't' may be weakened to 'com.siyeh.igtest.abstraction.weaken_type.Foo2'">t</warning>) {
    t.bar();
  }
  public void bar() {
  }
}
@Deprecated
interface Foo {
  void bar();
}
interface Foo2 extends Foo {}