private Element nearestEnclosingExceptLocal(Tree tree){
  TreePath path=atypeFactory.getPath(tree);
  if (path == null) {
    Element method=atypeFactory.getEnclosingMethod(tree);
    if (method != null) {
      return method;
    }
 else {
      return InternalUtils.symbol(tree);
    }
  }
  Tree prev=null;
  for (  Tree t : path) {
switch (t.getKind()) {
case VARIABLE:
      VariableTree vtree=(VariableTree)t;
    ExpressionTree vtreeInit=vtree.getInitializer();
  if (vtreeInit != null && prev == vtreeInit) {
    Element elt=TreeUtils.elementFromDeclaration((VariableTree)t);
    DefaultQualifier d=elt.getAnnotation(DefaultQualifier.class);
    DefaultQualifiers ds=elt.getAnnotation(DefaultQualifiers.class);
    if (d == null && ds == null) {
      break;
    }
  }
if (prev != null && prev.getKind() == Tree.Kind.MODIFIERS) {
  break;
}
return TreeUtils.elementFromDeclaration((VariableTree)t);
case METHOD:
return TreeUtils.elementFromDeclaration((MethodTree)t);
case CLASS:
case ENUM:
case INTERFACE:
case ANNOTATION_TYPE:
return TreeUtils.elementFromDeclaration((ClassTree)t);
default :
}
prev=t;
}
return null;
}

private Element nearestEnclosingExceptLocal ( Tree tree ) { TreePath path = atypeFactory . getPath ( tree ) ; if ( path == null ) { Element method = atypeFactory . getEnclosingMethod ( tree ) ; if ( method != null ) { return method ; } else { return InternalUtils . symbol ( tree ) ; } } Tree prev = null ; for ( Tree t : path ) { switch ( t . getKind ( ) ) { case VARIABLE : VariableTree vtree = ( VariableTree ) t ; ExpressionTree vtreeInit = vtree . getInitializer ( ) ; if ( vtreeInit != null && prev == vtreeInit ) { Element elt = TreeUtils . elementFromDeclaration ( ( VariableTree ) t ) ; DefaultQualifier d = elt . getAnnotation ( DefaultQualifier . class ) ; DefaultQualifiers ds = elt . getAnnotation ( DefaultQualifiers . class ) ; if ( d == null && ds == null ) { break ; } } if ( prev != null && prev . getKind ( ) == Tree . Kind . MODIFIERS ) { break ; } return TreeUtils . elementFromDeclaration ( ( VariableTree ) t ) ; case METHOD : return TreeUtils . elementFromDeclaration ( ( MethodTree ) t ) ; case CLASS : case ENUM : case INTERFACE : case ANNOTATION_TYPE : return TreeUtils . elementFromDeclaration ( ( ClassTree ) t ) ; default : } prev = t ; } return null ; }
determines the nearest enclosing element for a tree by climbing the tree toward the root and obtaining the element for the first declaration ( variable , method , or class ) that encloses the tree . initializers of local variables are handled in a special way : within an initializer we look for the defaultqualifier ( s ) annotation and keep track of the previously visited tree . todo : explain the behavior better .
