Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 Martin Skurla
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
   * the License. You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  * specific language governing permissions and limitations under the License.
  */
 package org.crazyjavahacking.astvisualizer.impl.javac;
 
 
 import java.util.List;
 
 
 
 import  com.sun.source.tree.UnionTypeTree;
 
 import static org.crazyjavahacking.astvisualizer.impl.javac.ASTTransitionNames.*;


Represents all processed Tree types.

Author(s):
Martin
 
 enum TreeTypeEnum {
 
     //-----------------------------------------------------------------------------------------------------------------
     // Constants.
     //-----------------------------------------------------------------------------------------------------------------
     ANNOTATION_TREE(JCTree.JCAnnotation.class) {
         @Override
         List<IEdgeDataedgeDataInternal(Tree tree) {
             AnnotationTree annotationTree = (AnnotationTreetree;
             final int numberOfArguments = annotationTree.getArguments().size();
 
             List<IEdgeDataedgeData = new ArrayList<IEdgeData>(numberOfArguments + 1); // 1 for annotation type
             edgeData.add   (EdgeDataFactory.fromTree());
             edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfArguments));
 
             return edgeData;
         }
     },
     ARRAY_ACCESS_TREE(2, JCTree.JCArrayAccess.class,   ),
     ARRAY_TYPE_TREE  (1, JCTree.JCArrayTypeTree.class),
     ASSERT_TREE(JCTree.JCAssert.class) {
         @Override
         List<IEdgeDataedgeDataInternal(Tree tree) {
             AssertTree assertTree = (AssertTreetree;
 
             return assertTree.getDetail() == null ? Collections.singletonList(EdgeDataFactory.fromTree())
                                                   : CollectionUtils.listOf(EdgeDataFactory.fromTree(),
                                                                            EdgeDataFactory.fromTree());
         }
     },
     ASSIGNMENT_TREE(2, JCTree.JCAssign.class,     ),
     BINARY_TREE    (2, JCTree.JCBinary.class),
     BLOCK_TREE(JCTree.JCBlock.class) {
         @Override
         List<IEdgeDataedgeDataInternal(Tree tree) {
            BlockTree blockTree = (BlockTreetree;
            return EdgeDataFactory.fromTreeMultipleTimes(blockTree.getStatements().size());
        }
    },
    BREAK_TREE(JCTree.JCBreak.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            BreakTree breakTree = (BreakTreetree;
            return breakTree.getLabel() == null
                    ? Collections.<IEdgeData>emptyList()
                    : Collections.singletonList(EdgeDataFactory.fromName(breakTree.getLabel()));
        }
    },
    CASE_TREE(JCTree.JCCase.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            CaseTree caseTree = (CaseTreetree;
            final int numberOfStatements = caseTree.getStatements().size();
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>(numberOfStatements + 1); // for expression
            edgeData.add   (EdgeDataFactory.fromTree());
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfStatements));
            return edgeData;
        }
    },
    CATCH_TREE(2, JCTree.JCCatch.class),
    CLASS_TREE(JCTree.JCClassDecl.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            ClassTree classTree = (ClassTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            if (classTree.getModifiers() != null) {
                ModifiersTree modifiersTree = classTree.getModifiers();
                if (ModifiersTreeHelper.hasFlagsOrAnnotations(modifiersTree)) {
                    edgeData.add(EdgeDataFactory.fromTree());
                }
            }
            edgeData.add(EdgeDataFactory.fromName(classTree.getSimpleName()));
            int numberOfTypeParameters = classTree.getTypeParameters().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfTypeParameters));
            if (classTree.getExtendsClause() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            int numberOfImplementClauses = classTree.getImplementsClause().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfImplementClauses));
            int numberOfMembers = classTree.getMembers().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfMembers));
            return edgeData;
        }
    },
    COMPILATION_UNIT_TREE(JCTree.JCCompilationUnit.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            CompilationUnitTree compilationUnitTree = (CompilationUnitTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            // just package could have package annotations
            int numberOfPackageAnnotations = compilationUnitTree.getPackageAnnotations().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfPackageAnnotations));
            // class could have package declaration, package has to have package declaration
            if (compilationUnitTree.getPackageName() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            // both class and package could have import
            int numberOfImports = compilationUnitTree.getImports().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfImports));
            // just class could have type declarations
            int numberOfTypeDeclarations = compilationUnitTree.getTypeDecls().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfTypeDeclarations));
            return edgeData;
        }
    },
    COMPOUND_ASSIGNMENT_TREE   (2, JCTree.JCAssignOp.class,    ,  ),
    CONDITIONAL_EXPRESSION_TREE(3, JCTree.JCConditional.class),
    CONTINUE_TREE(JCTree.JCContinue.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            ContinueTree continueTree = (ContinueTreetree;
            return continueTree.getLabel() == null
                    ? Collections.<IEdgeData>emptyList()
                    : Collections.singletonList(EdgeDataFactory.fromName(continueTree.getLabel()));
        }
    },
    DO_WHILE_LOOP_TREE    (2, JCTree.JCDoWhileLoop.class,     ),
    EMPTY_STATEMENT_TREE  (0, JCTree.JCSkip.class),
    ENHANCED_FOR_LOOP_TREE(3, JCTree.JCEnhancedForLoop.class,  ),
    ERRONEOUS_TREE(JCTree.JCErroneous.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            ErroneousTree erroneousTree = (ErroneousTreetree;
            return EdgeDataFactory.fromTreeMultipleTimes(erroneousTree.getErrorTrees().size());
        }
    },
    EXPRESSION_STATEMENT_TREE(1, JCTree.JCExpressionStatement.class),
    FOR_LOOP_TREE(JCTree.JCForLoop.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            ForLoopTree forLoopTree = (ForLoopTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            int numberOfInitializers = forLoopTree.getInitializer().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfInitializers));
            if (forLoopTree.getCondition() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            int numberOfUpdates = forLoopTree.getUpdate().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfUpdates));
            edgeData.add(EdgeDataFactory.fromTree());
            return edgeData;
        }
    },
    IDENTIFIER_TREE(JCTree.JCIdent.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            IdentifierTree identifierTree = (IdentifierTreetree;
            return Collections.singletonList(EdgeDataFactory.fromName(identifierTree.getName()));
        }
    },
    IF_TREE(JCTree.JCIf.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            IfTree ifTree = (IfTreetree;
            return ifTree.getElseStatement() == null ? CollectionUtils.listOf(EdgeDataFactory.fromTree(),
                                                                              EdgeDataFactory.fromTree())
                                                     : CollectionUtils.listOf(EdgeDataFactory.fromTree(),
                                                                              EdgeDataFactory.fromTree(),
                                                                              EdgeDataFactory.fromTree());
        }
    },
    IMPORT_TREE     (1, JCTree.JCImport.class,     ),
    INSTANCE_OF_TREE(2, JCTree.JCInstanceOf.class),
    LABELED_STATEMENT_TREE(JCTree.JCLabeledStatement.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            LabeledStatementTree labeledStatementTree = (LabeledStatementTreetree;
            return CollectionUtils.listOf(EdgeDataFactory.fromName(labeledStatementTree.getLabel()),
                                          EdgeDataFactory.fromTree());
        }
    },
    LITERAL_TREE(0, JCTree.JCLiteral.class),
    MEMBER_SELECT_TREE(JCTree.JCFieldAccess.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            MemberSelectTree memberSelectTree = (MemberSelectTreetree;
            return CollectionUtils.listOf(EdgeDataFactory.fromTree(),
                                          EdgeDataFactory.fromName(memberSelectTree.getIdentifier()));
        }
    },
    METHOD_INVOCATION_TREE(JCTree.JCMethodInvocation.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            MethodInvocationTree methodInvocationTree = (MethodInvocationTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            int numberOfTypeArguments = methodInvocationTree.getTypeArguments().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfTypeArguments));
            edgeData.add(EdgeDataFactory.fromTree());
            int numberOfArguments = methodInvocationTree.getArguments().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfArguments));
            return edgeData;
        }
    },
    METHOD_TREE(JCTree.JCMethodDecl.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            MethodTree methodTree = (MethodTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            // all type of methods (class, interface, annotation) has modifiers + type + name
            if (methodTree.getModifiers() != null) {
                ModifiersTree modifiersTree = methodTree.getModifiers();
                if (ModifiersTreeHelper.hasFlagsOrAnnotations(modifiersTree)) {
                    edgeData.add(EdgeDataFactory.fromTree());
                }
            }
            // constructor does not have return type
            if (methodTree.getReturnType() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            edgeData.add(EdgeDataFactory.fromName(methodTree.getName()));
            // class + interface methods could have type parameters
            int numberOfTypeParameters = methodTree.getTypeParameters().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfTypeParameters));
            // class + interface methods could have parameters
            int numberOfParameters = methodTree.getParameters().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfParameters));
            // class + interface methods could have throw clause
            int numberOfThrows = methodTree.getThrows().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfThrows));
            // class method has body, but interface and annotation do not
            if (methodTree.getBody() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            // annotation method could have optional default return value
            if (methodTree.getDefaultValue() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            return edgeData;
        }
    },
    MODIFIERS_TREE(JCTree.JCModifiers.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            ModifiersTree modifiersTree = (ModifiersTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            for (Modifier modifier : modifiersTree.getFlags()) {
                edgeData.add(EdgeDataFactory.fromModifier(modifier));
            }
            int numberOfAnnotations = modifiersTree.getAnnotations().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfAnnotations));
            return edgeData;
        }
    },
    NEW_ARRAY_TREE(JCTree.JCNewArray.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            NewArrayTree newArrayTree = (NewArrayTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            // not every array has to have type, for example array initializer => Object[] array = {"", ""};
            if (newArrayTree.getType() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            int numberOfDimensions = newArrayTree.getDimensions().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfDimensions));
            if (newArrayTree.getInitializers() != null) {
                int numberOfInitializers = newArrayTree.getInitializers().size();
                edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfInitializers));
            }
            return edgeData;
        }
    },
    NEW_CLASS_TREE(JCTree.JCNewClass.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            NewClassTree newClassTree = (NewClassTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            if (newClassTree.getEnclosingExpression() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            edgeData.add(EdgeDataFactory.fromTree());
            int numberOfTypeArguments = newClassTree.getTypeArguments().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfTypeArguments));
            int numberOfArguments = newClassTree.getArguments().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfArguments));
            if (newClassTree.getClassBody() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            return edgeData;
        }
    },
    PARAMETERIZED_TYPE_TREE(JCTree.JCTypeApply.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            ParameterizedTypeTree parameterizedTypeTree = (ParameterizedTypeTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            edgeData.add(EdgeDataFactory.fromTree());
            int numberOfTypeArguments = parameterizedTypeTree.getTypeArguments().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfTypeArguments));
            return edgeData;
        }
    },
    PARENTHESIZED_TREE(1, JCTree.JCParens.class),
    PRIMITIVE_TYPE_TREE(JCTree.JCPrimitiveTypeTree.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            PrimitiveTypeTree primitiveTypeTree = (PrimitiveTypeTreetree;
            return Collections.singletonList(EdgeDataFactory.fromTypeKind(,
                                                                          primitiveTypeTree.getPrimitiveTypeKind()));
        }
    },
    RETURN_TREE(JCTree.JCReturn.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
             ReturnTree returnTree = (ReturnTreetree;
            return returnTree.getExpression() == null ? Collections.<IEdgeData>emptyList()
                                                      : Collections.singletonList(EdgeDataFactory.fromTree());
        }
    },
    SWITCH_TREE(JCTree.JCSwitch.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            SwitchTree switchTree = (SwitchTreetree;
            final int numberOfCases = switchTree.getCases().size();
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>(numberOfCases + 1); // 1 for expression
            edgeData.add(EdgeDataFactory.fromTree());
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfCases));
            return edgeData;
        }
    },
    SYNCHRONIZED_TREE(2, JCTree.JCSynchronized.class),
    THROW_TREE       (1, JCTree.JCThrow.class,        ),
    TRY_TREE(JCTree.JCTry.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            TryTree tryTree = (TryTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            // starting from Java 7
            int numberOfResources = tryTree.getResources().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfResources));
            edgeData.add(EdgeDataFactory.fromTree());
            int numberOfCatches = tryTree.getCatches().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfCatches));
            if (tryTree.getFinallyBlock() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            return edgeData;
        }
    },
    TYPE_CAST_TREE(2, JCTree.JCTypeCast.class),
    TYPE_PARAMETER_TREE(JCTree.JCTypeParameter.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            TypeParameterTree typeParameterTree = (TypeParameterTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            edgeData.add(EdgeDataFactory.fromName(typeParameterTree.getName()));
            int numberOfBounds = typeParameterTree.getBounds().size();
            edgeData.addAll(EdgeDataFactory.fromTreeMultipleTimes(numberOfBounds));
            return edgeData;
        }
    },
    UNARY_TREE(1, JCTree.JCUnary.class),
    // starting from Java 7
    UNION_TYPE_TREE(JCTree.JCTypeUnion.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            UnionTypeTree unionTypeTree = (UnionTypeTree) tree;
            int numberOfTypeAlternatives = unionTypeTree.getTypeAlternatives().size();
            return EdgeDataFactory.fromTreeMultipleTimes(numberOfTypeAlternatives);
        }
    },
    VARIABLE_TREE(JCTree.JCVariableDecl.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            VariableTree variableTree = (VariableTreetree;
            List<IEdgeDataedgeData = new ArrayList<IEdgeData>();
            if (variableTree.getModifiers() != null) {
                ModifiersTree modifiersTree = variableTree.getModifiers();
                if (ModifiersTreeHelper.hasFlagsOrAnnotations(modifiersTree)) {
                    edgeData.add(EdgeDataFactory.fromTree());
                }
            }
            edgeData.add(EdgeDataFactory.fromTree());
            edgeData.add(EdgeDataFactory.fromName(variableTree.getName()));
            if (variableTree.getInitializer() != null) {
                edgeData.add(EdgeDataFactory.fromTree());
            }
            return edgeData;
        }
    },
    WHILE_LOOP_TREE(2, JCTree.JCWhileLoop.class),
    WILDCARD_TREE(JCTree.JCWildcard.class) {
        @Override
        List<IEdgeDataedgeDataInternal(Tree tree) {
            WildcardTree wildcardTree = (WildcardTreetree;
            return wildcardTree.getBound() == null ? Collections.<IEdgeData>emptyList()
                                                   : Collections.singletonList(EdgeDataFactory.fromTree());
        }
    };
    //-----------------------------------------------------------------------------------------------------------------
    // Members.
    //-----------------------------------------------------------------------------------------------------------------
    private final List<IEdgeDataedgeData;
    private final Class<? extends JCTreetreeTypeClass;
    //-----------------------------------------------------------------------------------------------------------------
    // Constructors.
    //-----------------------------------------------------------------------------------------------------------------
    private TreeTypeEnum(Class<? extends JCTreetreeTypeClass) {
        this. = treeTypeClass;
        this. = null;
    }
    private TreeTypeEnum(int edgesClass<? extends JCTreetreeTypeClassString... transitionNames) {
        if (edges != transitionNames.length) {
            throw new IllegalArgumentException("For TypeTree: " + this);
        }
        this. = treeTypeClass;
        List<IEdgeDataedgeDataList = new ArrayList<IEdgeData>(transitionNames.length);
        for (String transitionName : transitionNames) {
            edgeDataList.add(EdgeDataFactory.fromTree(transitionName));
        }
        this. = edgeDataList;
    }
    //-----------------------------------------------------------------------------------------------------------------
    // Public Methods.
    //-----------------------------------------------------------------------------------------------------------------
    
Returns unmodifiable list of IEdgeData objects.

Parameters:
tree tree
Returns:
unmodifiable list of IEdgeData objects
    public final List<IEdgeDatagetEdgeData(Tree tree) {
        return Collections.unmodifiableList(edgeDataInternal(tree));
    }

    
Gets instance by given JCTree type class.

Parameters:
treeTypeClass JCTree type class
Returns:
TreeTypeEnum instance
Throws:
IllegalArgumentException if the given type is not supported
    public static TreeTypeEnum getByClass(Class<? extends JCTreetreeTypeClass) {
        for (TreeTypeEnum treeType : values()) {
            if (treeType.treeTypeClass == treeTypeClass) {
                return treeType;
            }
        }
        throw new IllegalArgumentException("Given type is not supported: " + treeTypeClass);
    }
    //-----------------------------------------------------------------------------------------------------------------
    // Private Helpers.
    //-----------------------------------------------------------------------------------------------------------------
    
Returns list of IEdgeData objects.

Parameters:
tree tree
Returns:
list of IEdgeData objects
        return ;
    }
New to GrepCode? Check out our FAQ X