Java Code Examples for javax.swing.tree.DefaultMutableTreeNode#getFirstLeaf()

The following examples show how to use javax.swing.tree.DefaultMutableTreeNode#getFirstLeaf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IfStatementExpressionAnalyzer.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
public DefaultMutableTreeNode getRemainingExpression(PsiExpression expressionToBeRemoved) {
    DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode();
    processExpression(newRoot, completeExpression);
    DefaultMutableTreeNode leaf = newRoot.getFirstLeaf();
    while (leaf != null) {
        PsiExpression expression = (PsiExpression) leaf.getUserObject();
        if (expression.equals(expressionToBeRemoved)) {
            DefaultMutableTreeNode parent = (DefaultMutableTreeNode) leaf.getParent();
            if (parent != null) {
                DefaultMutableTreeNode grandParent = (DefaultMutableTreeNode) parent.getParent();
                DefaultMutableTreeNode sibling = null;
                if (leaf.getNextSibling() != null) {
                    sibling = leaf.getNextSibling();
                } else if (leaf.getPreviousSibling() != null) {
                    sibling = leaf.getPreviousSibling();
                }
                if (grandParent != null) {
                    int parentIndex = grandParent.getIndex(parent);
                    grandParent.remove(parent);
                    grandParent.insert(sibling, parentIndex);
                } else {
                    newRoot = sibling;
                }
            } else {
                newRoot = null;
            }
            break;
        }
        leaf = leaf.getNextLeaf();
    }
    return newRoot;
}
 
Example 2
Source File: AssociationDetection.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean checkForContainerAssociation(String from, String to) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(from);
    recurse(root);
    DefaultMutableTreeNode firstLeaf = root.getFirstLeaf();
    if(checkFullPathForContainerAssociation(firstLeaf,to))
        return true;
    DefaultMutableTreeNode leaf = firstLeaf.getNextLeaf();
    while(leaf != null) {
        if(checkFullPathForContainerAssociation(leaf,to))
            return true;
        leaf = leaf.getNextLeaf();
    }
    return false;
}
 
Example 3
Source File: IfStatementExpressionAnalyzer.java    From JDeodorant with MIT License 5 votes vote down vote up
public DefaultMutableTreeNode getRemainingExpression(Expression expressionToBeRemoved) {
	DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode();
	processExpression(newRoot, completeExpression);
	DefaultMutableTreeNode leaf = newRoot.getFirstLeaf();
	while(leaf != null) {
		Expression expression = (Expression)leaf.getUserObject();
		if(expression.equals(expressionToBeRemoved)) {
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode)leaf.getParent();
			if(parent != null) {
				DefaultMutableTreeNode grandParent = (DefaultMutableTreeNode)parent.getParent();
				DefaultMutableTreeNode sibling = null;
				if(leaf.getNextSibling() != null) {
					sibling = leaf.getNextSibling();
				}
				else if(leaf.getPreviousSibling() != null) {
					sibling = leaf.getPreviousSibling();
				}
				if(grandParent != null) {
					int parentIndex = grandParent.getIndex(parent);
					grandParent.remove(parent);
					grandParent.insert(sibling, parentIndex);
				}
				else {
					newRoot = sibling;
				}
				break;
			}
			else {
				newRoot = null;
				break;
			}
		}
		leaf = leaf.getNextLeaf();
	}
	return newRoot;
}
 
Example 4
Source File: TypeCheckElimination.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean isTypeCheckMethodStateSetter() {
	InheritanceTree tree = null;
	if(existingInheritanceTree != null)
		tree = existingInheritanceTree;
	else if(inheritanceTreeMatchingWithStaticTypes != null)
		tree = inheritanceTreeMatchingWithStaticTypes;
	if(tree != null) {
		DefaultMutableTreeNode root = tree.getRootNode();
		DefaultMutableTreeNode leaf = root.getFirstLeaf();
		List<String> subclassNames = new ArrayList<String>();
		while(leaf != null) {
			subclassNames.add((String)leaf.getUserObject());
			leaf = leaf.getNextLeaf();
		}
		Block typeCheckMethodBody = typeCheckMethod.getBody();
		List<Statement> statements = typeCheckMethodBody.statements();
		if(statements.size() > 0 && statements.get(0) instanceof SwitchStatement) {
			SwitchStatement switchStatement = (SwitchStatement)statements.get(0);
			List<Statement> statements2 = switchStatement.statements();
			ExpressionExtractor expressionExtractor = new ExpressionExtractor();
			int matchCounter = 0;
			for(Statement statement2 : statements2) {
				if(!(statement2 instanceof SwitchCase) && !(statement2 instanceof BreakStatement)) {
					List<Expression> classInstanceCreations = expressionExtractor.getClassInstanceCreations(statement2);
					if(classInstanceCreations.size() == 1) {
						ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)classInstanceCreations.get(0);
						Type classInstanceCreationType = classInstanceCreation.getType();
						if(subclassNames.contains(classInstanceCreationType.resolveBinding().getQualifiedName())) {
							matchCounter++;
						}
					}
				}
			}
			if(matchCounter == subclassNames.size())
				return true;
		}
	}
	return false;
}
 
Example 5
Source File: FilteredTree.java    From swing_library with MIT License 5 votes vote down vote up
/**
 * 
 * @param root
 * @return boolean bad leaves were returned
 */
private boolean removeBadLeaves(DefaultMutableTreeNode root) {

	// no bad leaves yet
	boolean badLeaves = false;

	// reference first leaf
	DefaultMutableTreeNode leaf = root.getFirstLeaf();

	// if leaf is root then its the only node
	if (leaf.isRoot())
		return false;

	int leafCount = root.getLeafCount(); // this get method changes if in for loop so have to define outside of it
	for (int i = 0; i < leafCount; i++) {

		DefaultMutableTreeNode nextLeaf = leaf.getNextLeaf();

		// if it does not start with the text then snip it off its parent
		if (!leaf.getUserObject().toString().startsWith(textToMatch)) {
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode) leaf.getParent();

			if (parent != null)
				parent.remove(leaf);

			badLeaves = true;
		}
		leaf = nextLeaf;
	}
	return badLeaves;
}
 
Example 6
Source File: FilterTreeModel.java    From swing_library with MIT License 5 votes vote down vote up
/**
 * 
 * @param root
 * @return boolean bad leaves were returned
 */
private boolean removeBadLeaves(DefaultMutableTreeNode root) {

	// no bad leaves yet
	boolean badLeaves = false;

	// reference first leaf
	DefaultMutableTreeNode leaf = root.getFirstLeaf();

	// if leaf is root then its the only node
	if (leaf.isRoot())
		return false;

	int leafCount = root.getLeafCount(); // this get method changes if in for loop so have to define outside of it
	for (int i = 0; i < leafCount; i++) {

		DefaultMutableTreeNode nextLeaf = leaf.getNextLeaf();

		// if it does not start with the text then snip it off its parent
		if (!filter.accepts(filterText, leaf.getUserObject().toString())) {
			DefaultMutableTreeNode parent = (DefaultMutableTreeNode) leaf.getParent();

			if (parent != null)
				parent.remove(leaf);

			badLeaves = true;
		}
		leaf = nextLeaf;
	}
	return badLeaves;
}
 
Example 7
Source File: ReplaceTypeCodeWithStateStrategy.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
private boolean typeObjectGetterMethodAlreadyExists() {
    InheritanceTree tree = typeCheckElimination.getInheritanceTreeMatchingWithStaticTypes();
    if (tree != null) {
        PsiMethod[] contextMethods = sourceTypeDeclaration.getMethods();
        DefaultMutableTreeNode rootNode = tree.getRootNode();
        String rootClassName = (String) rootNode.getUserObject();
        DefaultMutableTreeNode leaf = rootNode.getFirstLeaf();
        List<String> subclassNames = new ArrayList<>();
        while (leaf != null) {
            subclassNames.add((String) leaf.getUserObject());
            leaf = leaf.getNextLeaf();
        }
        for (PsiMethod contextMethod : contextMethods) {
            PsiType returnType = contextMethod.getReturnType();
            if (returnType != null) {
                if (returnType.getCanonicalText().equals(rootClassName)) {
                    PsiCodeBlock contextMethodBody = contextMethod.getBody();
                    if (contextMethodBody != null) {
                        PsiStatement[] statements = contextMethodBody.getStatements();
                        if (statements.length > 0 && statements[0] instanceof PsiSwitchStatement) {
                            PsiSwitchStatement switchStatement = (PsiSwitchStatement) statements[0];
                            PsiStatement[] statements2 = switchStatement.getBody().getStatements();
                            int matchCounter = 0;
                            for (PsiStatement statement2 : statements2) {
                                if (statement2 instanceof PsiReturnStatement) {
                                    PsiReturnStatement returnStatement = (PsiReturnStatement) statement2;
                                    PsiExpression returnStatementExpression = returnStatement.getReturnValue();
                                    if (returnStatementExpression instanceof PsiNewExpression) {
                                        PsiNewExpression classInstanceCreation = (PsiNewExpression) returnStatementExpression;
                                        PsiClass createdClass = (PsiClass) classInstanceCreation.getClassReference().resolve();
                                        if (createdClass != null && subclassNames.contains(createdClass.getQualifiedName())) {
                                            matchCounter++;
                                        }
                                    }
                                }
                            }
                            if (matchCounter == subclassNames.size())
                                return true;
                        }
                    }
                }
            }
        }
    }
    return false;
}
 
Example 8
Source File: TypeCheckElimination.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public List<String> getSubclassNames() {
    List<String> subclassNames = new ArrayList<>();
    for (PsiExpression expression : typeCheckMap.keySet()) {
        List<PsiField> simpleNameGroup = staticFieldMap.get(expression);
        if (simpleNameGroup != null) {
            for (PsiField simpleName : simpleNameGroup) {
                String staticFieldName = simpleName.getName();
                PsiType castingType = getCastingType(typeCheckMap.get(expression));
                StringBuilder subclassName;
                if (!staticFieldName.contains("_")) {
                    subclassName = new StringBuilder(staticFieldName.substring(0, 1).toUpperCase() +
                            staticFieldName.substring(1).toLowerCase());
                } else {
                    subclassName = new StringBuilder();
                    StringTokenizer tokenizer = new StringTokenizer(staticFieldName, "_");
                    while (tokenizer.hasMoreTokens()) {
                        String tempName = tokenizer.nextToken().toLowerCase();
                        subclassName.append(tempName.subSequence(0, 1).toString().toUpperCase())
                                .append(tempName.subSequence(1, tempName.length()).toString());
                    }
                }
                if (inheritanceTreeMatchingWithStaticTypes != null) {
                    subclassNames.add(staticFieldSubclassTypeMap.get(simpleName));
                } else if (existingInheritanceTree != null) {
                    DefaultMutableTreeNode root = existingInheritanceTree.getRootNode();
                    DefaultMutableTreeNode leaf = root.getFirstLeaf();
                    while (leaf != null) {
                        String childClassName = (String) leaf.getUserObject();
                        if (childClassName.endsWith(subclassName.toString())) {
                            subclassNames.add(childClassName);
                            break;
                        } else if (castingType != null && castingType.getCanonicalText().equals(childClassName)) {
                            subclassNames.add(childClassName);
                            break;
                        }
                        leaf = leaf.getNextLeaf();
                    }
                } else if (castingType != null) {
                    subclassNames.add(castingType.getCanonicalText());
                } else {
                    subclassNames.add(subclassName.toString());
                }
            }
        }
        List<PsiType> typeGroup = subclassTypeMap.get(expression);
        if (typeGroup != null) {
            for (PsiType type : typeGroup)
                subclassNames.add(type.getCanonicalText());
        }
    }
    return subclassNames;
}
 
Example 9
Source File: TypeCheckElimination.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
public boolean isTypeCheckMethodStateSetter() {
    InheritanceTree tree = null;
    if (existingInheritanceTree != null)
        tree = existingInheritanceTree;
    else if (inheritanceTreeMatchingWithStaticTypes != null)
        tree = inheritanceTreeMatchingWithStaticTypes;
    if (tree != null) {
        DefaultMutableTreeNode root = tree.getRootNode();
        DefaultMutableTreeNode leaf = root.getFirstLeaf();
        List<String> subclassNames = new ArrayList<>();
        while (leaf != null) {
            subclassNames.add((String) leaf.getUserObject());
            leaf = leaf.getNextLeaf();
        }
        PsiCodeBlock typeCheckMethodBody = getTypeCheckMethod().getBody();
        if (typeCheckMethodBody != null && typeCheckMethodBody.getStatements().length > 0
                && typeCheckMethodBody.getStatements()[0] instanceof PsiSwitchStatement) {
            PsiStatement[] statements = typeCheckMethodBody.getStatements();
            PsiSwitchStatement switchStatement = (PsiSwitchStatement) statements[0];
            PsiCodeBlock switchStatementBody = switchStatement.getBody();
            if (switchStatementBody != null) {
                ExpressionExtractor expressionExtractor = new ExpressionExtractor();
                int matchCounter = 0;
                for (PsiStatement psiStatement : switchStatementBody.getStatements()) {
                    if (!(psiStatement instanceof PsiSwitchLabelStatement) && !(psiStatement instanceof PsiBreakStatement)) {
                        List<PsiExpression> classInstanceCreations = expressionExtractor.getClassInstanceCreations(psiStatement);
                        if (classInstanceCreations.size() == 1) {
                            PsiNewExpression classInstanceCreation = (PsiNewExpression) classInstanceCreations.get(0);
                            if (classInstanceCreation.getClassReference() != null) {
                                String classInstanceCreationType = classInstanceCreation.getClassReference().getQualifiedName();
                                if (subclassNames.contains(classInstanceCreationType)) {
                                    matchCounter++;
                                }
                            }
                        }
                    }
                }
                return matchCounter == subclassNames.size();
            }
        }
    }
    return false;
}
 
Example 10
Source File: SystemObject.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
private void inheritanceHierarchyMatchingWithStaticTypes(TypeCheckElimination typeCheckElimination,
                                                         CompleteInheritanceDetection inheritanceDetection) {
    List<PsiField> staticFields = typeCheckElimination.getStaticFields();
    String abstractClassType = typeCheckElimination.getAbstractClassType();
    InheritanceTree tree = null;
    if (abstractClassType != null) {
        tree = inheritanceDetection.getTree(abstractClassType);
    }
    if (tree != null) {
        DefaultMutableTreeNode rootNode = tree.getRootNode();
        DefaultMutableTreeNode leaf = rootNode.getFirstLeaf();
        List<String> inheritanceHierarchySubclassNames = new ArrayList<>();
        while (leaf != null) {
            inheritanceHierarchySubclassNames.add((String) leaf.getUserObject());
            leaf = leaf.getNextLeaf();
        }
        int matchCounter = 0;
        for (PsiField staticField : staticFields) {
            for (String subclassName : inheritanceHierarchySubclassNames) {
                ClassObject classObject = getClassObject(subclassName);
                PsiElement abstractTypeDeclaration = classObject.getAbstractTypeDeclaration();
                if (abstractTypeDeclaration instanceof PsiClass) {
                    PsiClass typeDeclaration = (PsiClass) abstractTypeDeclaration;
                    PsiDocComment javadoc = typeDeclaration.getDocComment();
                    if (javadoc != null) {
                        PsiDocTag[] tagElements = javadoc.getTags();
                        for (PsiDocTag tagElement : tagElements) {
                            tagElement.getName();
                            if ("see".equals(tagElement.getName())) {
                                PsiElement[] fragments = tagElement.getDataElements();
                                for (PsiElement fragment : fragments) {
                                    if (!(fragment instanceof PsiDocMethodOrFieldRef)) {
                                        continue;
                                    }
                                    PsiReference memberRef = fragment.getReference();
                                    if (memberRef == null) {
                                        continue;
                                    }
                                    PsiElement resolvedRef = memberRef.resolve();
                                    if (staticField.equals(resolvedRef)) {
                                        typeCheckElimination.putStaticFieldSubclassTypeMapping(staticField, subclassName);
                                        matchCounter++;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (matchCounter == staticFields.size()) {
            typeCheckElimination.setInheritanceTreeMatchingWithStaticTypes(tree);
        }
    }
}
 
Example 11
Source File: SystemObject.java    From JDeodorant with MIT License 4 votes vote down vote up
private void inheritanceHierarchyMatchingWithStaticTypes(TypeCheckElimination typeCheckElimination,
		CompleteInheritanceDetection inheritanceDetection) {
	List<SimpleName> staticFields = typeCheckElimination.getStaticFields();
	String abstractClassType = typeCheckElimination.getAbstractClassType();
	InheritanceTree tree = null;
	if(abstractClassType != null)
		tree = inheritanceDetection.getTree(abstractClassType);
	if(tree != null) {
		DefaultMutableTreeNode rootNode = tree.getRootNode();
		DefaultMutableTreeNode leaf = rootNode.getFirstLeaf();
		List<String> inheritanceHierarchySubclassNames = new ArrayList<String>();
		while(leaf != null) {
			inheritanceHierarchySubclassNames.add((String)leaf.getUserObject());
			leaf = leaf.getNextLeaf();
		}
		int matchCounter = 0;
		for(SimpleName staticField : staticFields) {
			for(String subclassName : inheritanceHierarchySubclassNames) {
				ClassObject classObject = getClassObject(subclassName);
				AbstractTypeDeclaration abstractTypeDeclaration = classObject.getAbstractTypeDeclaration();
				if(abstractTypeDeclaration instanceof TypeDeclaration) {
					TypeDeclaration typeDeclaration = (TypeDeclaration)abstractTypeDeclaration;
					Javadoc javadoc = typeDeclaration.getJavadoc();
					if(javadoc != null) {
						List<TagElement> tagElements = javadoc.tags();
						for(TagElement tagElement : tagElements) {
							if(tagElement.getTagName() != null && tagElement.getTagName().equals(TagElement.TAG_SEE)) {
								List<ASTNode> fragments = tagElement.fragments();
								for(ASTNode fragment : fragments) {
									if(fragment instanceof MemberRef) {
										MemberRef memberRef = (MemberRef)fragment;
										IBinding staticFieldNameBinding = staticField.resolveBinding();
										ITypeBinding staticFieldNameDeclaringClass = null;
										if(staticFieldNameBinding != null && staticFieldNameBinding.getKind() == IBinding.VARIABLE) {
											IVariableBinding staticFieldNameVariableBinding = (IVariableBinding)staticFieldNameBinding;
											staticFieldNameDeclaringClass = staticFieldNameVariableBinding.getDeclaringClass();
										}
										if(staticFieldNameBinding.getName().equals(memberRef.getName().getIdentifier()) &&
												staticFieldNameDeclaringClass.getQualifiedName().equals(memberRef.getQualifier().getFullyQualifiedName())) {
											typeCheckElimination.putStaticFieldSubclassTypeMapping(staticField, subclassName);
											matchCounter++;
											break;
										}
									}
								}
							}
						}
					}
				}
			}
		}
		if(matchCounter == staticFields.size()) {
			typeCheckElimination.setInheritanceTreeMatchingWithStaticTypes(tree);
			return;
		}
	}
}
 
Example 12
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 4 votes vote down vote up
private boolean typeObjectGetterMethodAlreadyExists() {
	InheritanceTree tree = typeCheckElimination.getInheritanceTreeMatchingWithStaticTypes();
	if(tree != null) {
		MethodDeclaration[] contextMethods = sourceTypeDeclaration.getMethods();
		DefaultMutableTreeNode rootNode = tree.getRootNode();
		String rootClassName = (String)rootNode.getUserObject();
		DefaultMutableTreeNode leaf = rootNode.getFirstLeaf();
		List<String> subclassNames = new ArrayList<String>();
		while(leaf != null) {
			subclassNames.add((String)leaf.getUserObject());
			leaf = leaf.getNextLeaf();
		}
		for(MethodDeclaration contextMethod : contextMethods) {
			Type returnType = contextMethod.getReturnType2();
			if(returnType != null) {
				if(returnType.resolveBinding().getQualifiedName().equals(rootClassName)) {
					Block contextMethodBody = contextMethod.getBody();
					if(contextMethodBody != null) {
						List<Statement> statements = contextMethodBody.statements();
						if(statements.size() > 0 && statements.get(0) instanceof SwitchStatement) {
							SwitchStatement switchStatement = (SwitchStatement)statements.get(0);
							List<Statement> statements2 = switchStatement.statements();
							int matchCounter = 0;
							for(Statement statement2 : statements2) {
								if(statement2 instanceof ReturnStatement) {
									ReturnStatement returnStatement = (ReturnStatement)statement2;
									Expression returnStatementExpression = returnStatement.getExpression();
									if(returnStatementExpression instanceof ClassInstanceCreation) {
										ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)returnStatementExpression;
										Type classInstanceCreationType = classInstanceCreation.getType();
										if(subclassNames.contains(classInstanceCreationType.resolveBinding().getQualifiedName())) {
											matchCounter++;
										}
									}
								}
							}
							if(matchCounter == subclassNames.size())
								return true;
						}
					}
				}
			}
		}
	}
	return false;
}
 
Example 13
Source File: TypeCheckElimination.java    From JDeodorant with MIT License 4 votes vote down vote up
public List<String> getSubclassNames() {
	List<String> subclassNames = new ArrayList<String>();
	for(Expression expression : typeCheckMap.keySet()) {
		List<SimpleName> simpleNameGroup = staticFieldMap.get(expression);
		if(simpleNameGroup != null) {
			for(SimpleName simpleName : simpleNameGroup) {
				String staticFieldName = simpleName.getIdentifier();
				Type castingType = getCastingType(typeCheckMap.get(expression));
				String subclassName = null;
				if(!staticFieldName.contains("_")) {
					subclassName = staticFieldName.substring(0, 1).toUpperCase() + 
					staticFieldName.substring(1, staticFieldName.length()).toLowerCase();
				}
				else {
					subclassName = "";
					StringTokenizer tokenizer = new StringTokenizer(staticFieldName,"_");
					while(tokenizer.hasMoreTokens()) {
						String tempName = tokenizer.nextToken().toLowerCase().toString();
						subclassName += tempName.subSequence(0, 1).toString().toUpperCase() + 
						tempName.subSequence(1, tempName.length()).toString();
					}
				}
				if(inheritanceTreeMatchingWithStaticTypes != null) {
					subclassNames.add(staticFieldSubclassTypeMap.get(simpleName));
				}
				else if(existingInheritanceTree != null) {
					DefaultMutableTreeNode root = existingInheritanceTree.getRootNode();
					DefaultMutableTreeNode leaf = root.getFirstLeaf();
					while(leaf != null) {
						String childClassName = (String)leaf.getUserObject();
						if(childClassName.endsWith(subclassName)) {
							subclassNames.add(childClassName);
							break;
						}
						else if(castingType != null && castingType.resolveBinding().getQualifiedName().equals(childClassName)) {
							subclassNames.add(childClassName);
							break;
						}
						leaf = leaf.getNextLeaf();
					}
				}
				else if(castingType != null) {
					subclassNames.add(castingType.resolveBinding().getQualifiedName());
				}
				else {
					subclassNames.add(subclassName);
				}
			}
		}
		List<Type> typeGroup = subclassTypeMap.get(expression);
		if(typeGroup != null) {
			for(Type type : typeGroup)
				subclassNames.add(type.resolveBinding().getQualifiedName());
		}
	}
	return subclassNames;
}