gt:         int parentType = name.getParent().getNodeType();
context: /*******************************************************************************
   * Copyright (c) 2000, 2011 IBM Corporation and others.
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the Eclipse Public License v1.0
   * which accompanies this distribution, and is available at
   * http://www.eclipse.org/legal/epl-v10.html
   *
   * Contributors:
   *     IBM Corporation - initial API and implementation
   *     Dmitry Stalnov (dstalnov@fusionone.com) - contributed fix for
   *       bug "inline method - doesn't handle implicit cast" (see
   *       https://bugs.eclipse.org/bugs/show_bug.cgi?id=24941).
   *     Dmitry Stalnov (dstalnov@fusionone.com) - contributed fix for
   *       bug Encapsulate field can fail when two variables in one variable declaration (see
   *       https://bugs.eclipse.org/bugs/show_bug.cgi?id=51540).
   *******************************************************************************/
  package org.eclipse.che.ide.ext.java.jdt.internal.corext.dom;
  
  import org.eclipse.che.ide.ext.java.jdt.core.compiler.IProblem;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ASTNode;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ASTVisitor;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.AbstractTypeDeclaration;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.AnonymousClassDeclaration;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ArrayType;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.Assignment;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.BodyDeclaration;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ChildListPropertyDescriptor;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ClassInstanceCreation;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.CompilationUnit;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.DoStatement;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.EnhancedForStatement;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.Expression;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.FieldAccess;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.FieldDeclaration;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ForStatement;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.IBinding;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.IExtendedModifier;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.IMethodBinding;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ITypeBinding;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.IVariableBinding;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.IfStatement;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.InfixExpression;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.Message;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.MethodDeclaration;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.MethodInvocation;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.Modifier;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.Name;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ParameterizedType;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.ParenthesizedExpression;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.PrimitiveType;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.QualifiedName;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.QualifiedType;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.SimpleName;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.SimpleType;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.SingleVariableDeclaration;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.StructuralPropertyDescriptor;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.Type;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.VariableDeclaration;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.VariableDeclarationExpression;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.VariableDeclarationFragment;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.VariableDeclarationStatement;
  import org.eclipse.che.ide.ext.java.jdt.core.dom.WhileStatement;
  import org.eclipse.che.ide.ext.java.jdt.internal.corext.codemanipulation.ASTResolving;
  import org.eclipse.che.ide.ext.java.jdt.internal.corext.util.CodeFormatterUtil;
  import org.eclipse.che.ide.ext.java.jdt.internal.ui.MembersOrderPreferenceCache;
  import org.eclipse.che.ide.ext.java.worker.WorkerDocument;
  import org.eclipse.che.ide.ext.java.jdt.text.Document;
  import org.eclipse.che.ide.ext.java.jdt.text.edits.TextEdit;
  import org.eclipse.che.ide.runtime.Assert;
  import org.eclipse.che.ide.api.text.BadLocationException;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  
  //import org.eclipse.jdt.internal.ui.JavaPlugin;
  //import org.eclipse.jdt.internal.ui.preferences.MembersOrderPreferenceCache;
  //import org.eclipse.jdt.internal.ui.text.correction.ASTResolving;
  
  public class ASTNodes {
  
      public static final int NODE_ONLY = 0;
  
      public static final int INCLUDE_FIRST_PARENT = 1;
  
      public static final int INCLUDE_ALL_PARENTS = 2;
  
      public static final int WARNING = 1 << 0;
  
      public static final int ERROR = 1 << 1;
  
      public static final int PROBLEMS = WARNING | ERROR;
  
      private static final Message[] EMPTY_MESSAGES = new Message[0];
  
      private static final IProblem[] EMPTY_PROBLEMS = new IProblem[0];
  
      private static final int CLEAR_VISIBILITY = ~(Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
  
      private ASTNodes() {
          // no instance;
      }
  
      public static String asString(ASTNode node) {
          ASTFlattener flattener = new ASTFlattener();
          node.accept(flattener);
          return flattener.getResult();
      }
  
      public static String asFormattedString(ASTNode node, int indent, String lineDelim, Map<String, String> options) {
          String unformatted = asString(node);
          TextEdit edit = CodeFormatterUtil.format2(node, unformatted, indent, lineDelim, options);
          if (edit != null) {
              Document document = new WorkerDocument(unformatted);
              try {
                  edit.apply(document, TextEdit.NONE);
              } catch (BadLocationException e) {
                  //TODO handle exception
                  e.printStackTrace();
                  //            JavaPlugin.log(e);
              }
              return document.get();
          }
          return unformatted; // unknown node
      }
  
      //   /**
      //    * Returns the source of the given node from the location where it was parsed.
      //    * @param node the node to get the source from
      //    * @param extendedRange if set, the extended ranges of the nodes should ne used
      //    * @param removeIndent if set, the indentation is removed.
      //    * @return return the source for the given node or null if accessing the source failed.
      //    */
      //   public static String getNodeSource(ASTNode node, boolean extendedRange, boolean removeIndent) {
      //      ASTNode root= node.getRoot();
      //      if (root instanceof CompilationUnit) {
      //         CompilationUnit astRoot= (CompilationUnit) root;
      //         ITypeRoot typeRoot= astRoot.getTypeRoot();
      //         try {
      //            if (typeRoot != null && typeRoot.getBuffer() != null) {
      //               IBuffer buffer= typeRoot.getBuffer();
      //               int offset= extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition();
      //               int length= extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
      //               String str= buffer.getText(offset, length);
      //               if (removeIndent) {
      //                  IJavaProject project= typeRoot.getJavaProject();
      //                  int indent= StubUtility.getIndentUsed(buffer, node.getStartPosition(), project);
      //                  str= Strings.changeIndent(str, indent, project, new String(), typeRoot.findRecommendedLineSeparator());
      //               }
      //               return str;
      //            }
      //         } catch (JavaModelException e) {
      //            // ignore
      //         }
      //      }
      //      return null;
      //   }
  
      /**
       * Returns the list that contains the given ASTNode. If the node
       * isn't part of any list, <code>null</code> is returned.
       *
       * @param node
       *         the node in question
       * @return the list that contains the node or <code>null</code>
       */
      public static List<? extends ASTNode> getContainingList(ASTNode node) {
          StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
          if (locationInParent != null && locationInParent.isChildListProperty()) {
              return (List<? extends ASTNode>)node.getParent().getStructuralProperty(locationInParent);
          }
          return null;
      }
  
      /**
       * Returns a list of the direct children of a node. The siblings are ordered by start offset.
       *
       * @param node
       *         the node to get the children for
       * @return the children
       */
      public static List<ASTNode> getChildren(ASTNode node) {
          ChildrenCollector visitor = new ChildrenCollector();
          node.accept(visitor);
          return visitor.result;
      }
  
      private static class ChildrenCollector extends GenericVisitor {
          public List<ASTNode> result;
  
          public ChildrenCollector() {
              super(true);
              result = null;
          }
  
          @Override
          protected boolean visitNode(ASTNode node) {
              if (result == null) { // first visitNode: on the node's parent: do nothing, return true
                  result = new ArrayList<ASTNode>();
                  return true;
              }
              result.add(node);
              return false;
          }
      }
  
      /**
       * Returns true if this is an existing node, i.e. it was created as part of
       * a parsing process of a source code file. Returns false if this is a newly
       * created node which has not yet been given a source position.
       *
       * @param node
       *         the node to be tested.
       * @return true if this is an existing node, false if not.
       */
      public static boolean isExistingNode(ASTNode node) {
          return node.getStartPosition() != -1;
      }
  
      /**
       * Returns the element type. This is a convenience method that returns its
       * argument if it is a simple type and the element type if the parameter is an array type.
       *
       * @param type
       *         The type to get the element type from.
       * @return The element type of the type or the type itself.
       */
      public static Type getElementType(Type type) {
          if (!type.isArrayType())
              return type;
          return ((ArrayType)type).getElementType();
      }
  
      public static ASTNode findDeclaration(IBinding binding, ASTNode root) {
          root = root.getRoot();
          if (root instanceof CompilationUnit) {
              return ((CompilationUnit)root).findDeclaringNode(binding);
          }
          return null;
      }
  
      public static VariableDeclaration findVariableDeclaration(IVariableBinding binding, ASTNode root) {
          if (binding.isField())
              return null;
          ASTNode result = findDeclaration(binding, root);
          if (result instanceof VariableDeclaration)
              return (VariableDeclaration)result;
  
          return null;
      }
  
      /**
       * Returns the type node for the given declaration.
       *
       * @param declaration
       *         the declaration
       * @return the type node
       */
      public static Type getType(VariableDeclaration declaration) {
          if (declaration instanceof SingleVariableDeclaration) {
              return ((SingleVariableDeclaration)declaration).getType();
          } else if (declaration instanceof VariableDeclarationFragment) {
              ASTNode parent = ((VariableDeclarationFragment)declaration).getParent();
              if (parent instanceof VariableDeclarationExpression)
                  return ((VariableDeclarationExpression)parent).getType();
              else if (parent instanceof VariableDeclarationStatement)
                  return ((VariableDeclarationStatement)parent).getType();
              else if (parent instanceof FieldDeclaration)
                  return ((FieldDeclaration)parent).getType();
          }
          Assert.isTrue(false, "Unknown VariableDeclaration"); //$NON-NLS-1$
          return null;
      }
  
      public static int getDimensions(VariableDeclaration declaration) {
          int dim = declaration.getExtraDimensions();
          Type type = getType(declaration);
          if (type instanceof ArrayType) {
              dim += ((ArrayType)type).getDimensions();
          }
          return dim;
      }
  
      public static List<IExtendedModifier> getModifiers(VariableDeclaration declaration) {
          Assert.isNotNull(declaration);
          if (declaration instanceof SingleVariableDeclaration) {
              return ((SingleVariableDeclaration)declaration).modifiers();
          } else if (declaration instanceof VariableDeclarationFragment) {
              ASTNode parent = declaration.getParent();
              if (parent instanceof VariableDeclarationExpression)
                  return ((VariableDeclarationExpression)parent).modifiers();
              else if (parent instanceof VariableDeclarationStatement)
                  return ((VariableDeclarationStatement)parent).modifiers();
          }
          return new ArrayList<IExtendedModifier>(0);
      }
  
      public static boolean isSingleDeclaration(VariableDeclaration declaration) {
          Assert.isNotNull(declaration);
          if (declaration instanceof SingleVariableDeclaration) {
              return true;
          } else if (declaration instanceof VariableDeclarationFragment) {
              ASTNode parent = declaration.getParent();
              if (parent instanceof VariableDeclarationExpression)
                  return ((VariableDeclarationExpression)parent).fragments().size() == 1;
              else if (parent instanceof VariableDeclarationStatement)
                  return ((VariableDeclarationStatement)parent).fragments().size() == 1;
          }
          return false;
      }
  
      public static boolean isLiteral(Expression expression) {
          int type = expression.getNodeType();
          return type == ASTNode.BOOLEAN_LITERAL || type == ASTNode.CHARACTER_LITERAL || type == ASTNode.NULL_LITERAL
                 || type == ASTNode.NUMBER_LITERAL || type == ASTNode.STRING_LITERAL || type == ASTNode.TYPE_LITERAL;
      }
  
      public static boolean isLabel(SimpleName name) {
  