title: How can out-of-context Java methods be analysed?
body: <p>I'm trying to do the following: for a given Java method I want to see whether or not this method can be used in certain context and be correct there, i.e. whether there exist certain definitions of classes, fields, methods, etc whose presence make this method correct. For example, the method</p>
  
  <pre><code>int m() {
      A a = new A();
      return a.value() + 1;
  }
  </code></pre>
  
  <p>is correct when, for example, the class <code>A</code> is defined, this class has a public method called <code>value</code> and the method returns <code>int</code>. On the other hand, for</p>
  
  <pre><code>int m() {
      A a1 = new A();
      int n = a1.value();
      A a2 = new A();
      String s = a2.value();
      return s.length() + n;
  }
  </code></pre>
  
  <p>there is no context in which the method can be correct. I know that this question can turn out to be a little bit too broad (and kind of theoretical one) but, in general, how can I approach this kind of problem? For one, I know that there should be some kind of type-checking and control-flow analysis. I'm not sure, though, if this is all that has to be done.</p>