List of usage examples for org.eclipse.jdt.core.dom StructuralPropertyDescriptor isChildListProperty
public final boolean isChildListProperty()
From source file:ch.acanda.eclipse.pmd.java.resolution.ASTUtil.java
License:Open Source License
/** * Replaces a node in an AST with another node. If the replacement is successful the original node is deleted. * * @param node The node to replace./* w w w. j a v a 2s. com*/ * @param replacement The replacement node. * @return <code>true</code> if the node was successfully replaced. */ public static boolean replace(final ASTNode node, final ASTNode replacement) { final ASTNode parent = node.getParent(); final StructuralPropertyDescriptor descriptor = node.getLocationInParent(); if (descriptor != null) { if (descriptor.isChildProperty()) { parent.setStructuralProperty(descriptor, replacement); node.delete(); return true; } else if (descriptor.isChildListProperty()) { @SuppressWarnings("unchecked") final List<ASTNode> children = (List<ASTNode>) parent.getStructuralProperty(descriptor); children.set(children.indexOf(node), replacement); node.delete(); return true; } } return false; }
From source file:ch.acanda.eclipse.pmd.java.resolution.ASTUtil.java
License:Open Source License
/** * Replaces a node in an AST with other nodes. If the replacement is successful the original node is deleted. * * @param node The node to replace.// w ww . j a v a2s . co m * @param replacement The replacement nodes. * @return <code>true</code> if the node was successfully replaced. */ public static boolean replace(final ASTNode node, final List<? extends ASTNode> replacement) { final ASTNode parent = node.getParent(); final StructuralPropertyDescriptor descriptor = node.getLocationInParent(); if (descriptor != null && descriptor.isChildListProperty()) { @SuppressWarnings("unchecked") final List<ASTNode> children = (List<ASTNode>) parent.getStructuralProperty(descriptor); children.addAll(children.indexOf(node), replacement); node.delete(); return true; } return false; }
From source file:changenodes.matching.BestLeafTreeMatcher.java
License:Apache License
@SuppressWarnings("unchecked") private void markMatchedNode(ASTNode left, ASTNode right) { if (left.getNodeType() != right.getNodeType()) { return;/*from w ww .ja v a2 s . com*/ } if (!(leftMatching.containsKey(left) || rightMatching.containsKey(right))) { leftMatching.put(left, right); rightMatching.put(right, left); List<StructuralPropertyDescriptor> props = (List<StructuralPropertyDescriptor>) left .structuralPropertiesForType(); for (StructuralPropertyDescriptor prop : props) { if (prop.isChildProperty()) { ASTNode newLeft, newRight; newLeft = (ASTNode) left.getStructuralProperty(prop); newRight = (ASTNode) right.getStructuralProperty(prop); if (newLeft != null && newRight != null) { markMatchedNode(newLeft, newRight); } } else if (prop.isChildListProperty()) { List<ASTNode> lefts, rights; lefts = (List<ASTNode>) left.getStructuralProperty(prop); rights = (List<ASTNode>) right.getStructuralProperty(prop); int times = lefts.size(); if (lefts.size() > rights.size()) { times = rights.size(); } Iterator<ASTNode> leftIt = lefts.iterator(); Iterator<ASTNode> rightIt = rights.iterator(); for (int i = 0; i < times; ++i) { ASTNode rNode = rightIt.next(); ASTNode lNode = leftIt.next(); if (lNode != null && rNode != null) { markMatchedNode(lNode, rNode); } } } //we dont handle simple props as they point to objects } } }
From source file:cideplus.ui.astview.ASTViewContentProvider.java
License:Open Source License
private Object[] getNodeChildren(ASTNode node) { ArrayList res = new ArrayList(); /*if (node instanceof Expression) { Expression expression= (Expression) node; ITypeBinding expressionTypeBinding= expression.resolveTypeBinding(); //res.add(createExpressionTypeBinding(node, expressionTypeBinding)); //from w w w .j a v a 2 s. co m // expressions: if (expression instanceof Name) { IBinding binding= ((Name) expression).resolveBinding(); if (binding != expressionTypeBinding){ //res.add(createBinding(expression, binding)); } } else if (expression instanceof MethodInvocation) { MethodInvocation methodInvocation= (MethodInvocation) expression; IMethodBinding binding= methodInvocation.resolveMethodBinding(); //res.add(createBinding(expression, binding)); String inferred= String.valueOf(methodInvocation.isResolvedTypeInferredFromExpectedType()); //res.add(new GeneralAttribute(expression, "ResolvedTypeInferredFromExpectedType", inferred)); //$NON-NLS-1$ } else if (expression instanceof SuperMethodInvocation) { SuperMethodInvocation superMethodInvocation= (SuperMethodInvocation) expression; IMethodBinding binding= superMethodInvocation.resolveMethodBinding(); //res.add(createBinding(expression, binding)); String inferred= String.valueOf(superMethodInvocation.isResolvedTypeInferredFromExpectedType()); //res.add(new GeneralAttribute(expression, "ResolvedTypeInferredFromExpectedType", inferred)); //$NON-NLS-1$ } else if (expression instanceof ClassInstanceCreation) { IMethodBinding binding= ((ClassInstanceCreation) expression).resolveConstructorBinding(); //res.add(createBinding(expression, binding)); } else if (expression instanceof FieldAccess) { IVariableBinding binding= ((FieldAccess) expression).resolveFieldBinding(); //res.add(createBinding(expression, binding)); } else if (expression instanceof SuperFieldAccess) { IVariableBinding binding= ((SuperFieldAccess) expression).resolveFieldBinding(); //res.add(createBinding(expression, binding)); } else if (expression instanceof Annotation) { IAnnotationBinding binding= ((Annotation) expression).resolveAnnotationBinding(); //res.add(createBinding(expression, binding)); } // Expression attributes: //res.add(new GeneralAttribute(expression, "Boxing: " + expression.resolveBoxing() + "; Unboxing: " + expression.resolveUnboxing())); //$NON-NLS-1$ //$NON-NLS-2$ //res.add(new GeneralAttribute(expression, "ConstantExpressionValue", expression.resolveConstantExpressionValue())); //$NON-NLS-1$ // references: } else if (node instanceof ConstructorInvocation) { IMethodBinding binding= ((ConstructorInvocation) node).resolveConstructorBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof SuperConstructorInvocation) { IMethodBinding binding= ((SuperConstructorInvocation) node).resolveConstructorBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof MethodRef) { IBinding binding= ((MethodRef) node).resolveBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof MemberRef) { IBinding binding= ((MemberRef) node).resolveBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof Type) { IBinding binding= ((Type) node).resolveBinding(); //res.add(createBinding(node, binding)); // declarations: } else if (node instanceof AbstractTypeDeclaration) { IBinding binding= ((AbstractTypeDeclaration) node).resolveBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof AnnotationTypeMemberDeclaration) { IBinding binding= ((AnnotationTypeMemberDeclaration) node).resolveBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof EnumConstantDeclaration) { IBinding binding= ((EnumConstantDeclaration) node).resolveVariable(); //res.add(createBinding(node, binding)); IBinding binding2= ((EnumConstantDeclaration) node).resolveConstructorBinding(); //res.add(createBinding(node, binding2)); } else if (node instanceof MethodDeclaration) { IBinding binding= ((MethodDeclaration) node).resolveBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof VariableDeclaration) { IBinding binding= ((VariableDeclaration) node).resolveBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof AnonymousClassDeclaration) { IBinding binding= ((AnonymousClassDeclaration) node).resolveBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof ImportDeclaration) { IBinding binding= ((ImportDeclaration) node).resolveBinding(); //res.add(createBinding(node, binding)); return new Object[0];//o import declaration } else if (node instanceof PackageDeclaration) { IBinding binding= ((PackageDeclaration) node).resolveBinding(); //res.add(createBinding(node, binding)); return new Object[0];//o package declaration no deve ter filhos } else if (node instanceof TypeParameter) { IBinding binding= ((TypeParameter) node).resolveBinding(); //res.add(createBinding(node, binding)); } else if (node instanceof MemberValuePair) { IBinding binding= ((MemberValuePair) node).resolveMemberValuePairBinding(); //res.add(createBinding(node, binding)); } else*/ /* if(node instanceof Modifier){ return new Object[0]; } else if(node instanceof TextElement){ return new Object[0]; } else if(node instanceof SimpleName){ return new Object[0]; } else if(node instanceof SimpleType){ return new Object[0]; } else if(node instanceof StringLiteral){ return new Object[0]; } else if(node instanceof NumberLiteral){ return new Object[0]; } else if(node instanceof PrimitiveType){ return new Object[0]; } else if(node instanceof PackageDeclaration){ return new Object[0]; } else if(node instanceof ImportDeclaration){ return new Object[0]; } else if(node instanceof EnumConstantDeclaration){ return new Object[0]; }*/ @SuppressWarnings("unchecked") List<Class<? extends ASTNode>> noChildNodes = Arrays.asList(Modifier.class, TextElement.class, SimpleName.class, SimpleType.class, StringLiteral.class, NumberLiteral.class, PrimitiveType.class, PackageDeclaration.class, ImportDeclaration.class, EnumConstantDeclaration.class); for (Class<? extends ASTNode> class1 : noChildNodes) { if (class1.isAssignableFrom(node.getClass())) { return new Object[0]; } } //essas propriedades no tero um parent (o filho ocupar o lugar do parent) List<String> childrenProperties = Arrays.asList("TAGS", "FRAGMENTS", "STATEMENTS", "BODY", "PACKAGE", "IMPORTS", "TYPES"); List list = node.structuralPropertiesForType(); for (int i = 0; i < list.size(); i++) { StructuralPropertyDescriptor curr = (StructuralPropertyDescriptor) list.get(i); NodeProperty nodeProperty = new NodeProperty(node, curr); if (childrenProperties.contains(nodeProperty.getPropertyName())) { res.addAll(Arrays.asList(nodeProperty.getChildren())); } else { if (!(curr.isChildListProperty() && nodeProperty.getChildren().length == 0) && !nodeProperty.getPropertyName().equals("INTERFACE") && !(nodeProperty.getNode() == null)) { res.add(nodeProperty); } } } if (node instanceof CompilationUnit) { CompilationUnit root = (CompilationUnit) node; //res.add(new JavaElement(root, root.getJavaElement())); //res.add(new CommentsProperty(root)); //res.add(new ProblemsProperty(root)); //res.add(new SettingsProperty(root)); //res.add(new WellKnownTypesProperty(root)); } return res.toArray(); }
From source file:com.google.gwt.eclipse.core.refactoring.regionupdater.EquivalentNodeFinder.java
License:Open Source License
@SuppressWarnings("unchecked") private int getIndex(ASTNode node) { StructuralPropertyDescriptor locationInParent = node.getLocationInParent(); if (locationInParent != null && locationInParent.isChildListProperty()) { List<ASTNode> parentsChildren = (List<ASTNode>) node.getParent() .getStructuralProperty(locationInParent); if (parentsChildren != null) { return parentsChildren.indexOf(node); }//from w w w. j a v a 2 s .co m } // The node is not contained within a list-based property on the parent return NOT_FROM_LIST; }
From source file:com.worldline.awltech.i18ntools.wizard.core.modules.ASTTreeCloner.java
License:Open Source License
/** * Clones the AST Tree.//from ww w . j a v a 2s . co m * * @param astNode * @return */ @SuppressWarnings("unchecked") public static <T extends ASTNode> T clone(final T astNode) { final AST ast = astNode.getAST(); final ASTNode createdInstance = ast.createInstance(astNode.getClass()); final List<?> structuralPropertiesForType = astNode.structuralPropertiesForType(); for (final Object o : structuralPropertiesForType) { final StructuralPropertyDescriptor descriptor = (StructuralPropertyDescriptor) o; if (descriptor.isChildListProperty()) { final List<Object> list = (List<Object>) astNode.getStructuralProperty(descriptor); for (final Object propertyValue : (List<Object>) astNode.getStructuralProperty(descriptor)) { list.add(propertyValue instanceof ASTNode ? ASTTreeCloner.clone((ASTNode) propertyValue) : propertyValue); } } else { final Object propertyValue = astNode.getStructuralProperty(descriptor); createdInstance.setStructuralProperty(descriptor, propertyValue instanceof ASTNode ? ASTTreeCloner.clone((ASTNode) propertyValue) : propertyValue); } } return (T) createdInstance; }
From source file:com.worldline.awltech.i18ntools.wizard.core.modules.ResourceBundleWrapper.java
License:Open Source License
/** * Refactors the source code to replace selected source by literal. * /*from w ww . j av a 2s . c om*/ * @param nodeFinder * @param literalName * @param resolver * @return */ @SuppressWarnings("unchecked") private boolean effectiveAddLiteral(final ASTNodeFinder nodeFinder, final String literalName, final ASTExpressionResolver resolver) { final ASTNode parent = nodeFinder.getParentNode(); final AST ast = parent.getAST(); final MethodInvocation replacement = ast.newMethodInvocation(); replacement.setExpression(ast.newName(this.resourceBundleName + "." + literalName)); replacement.setName(ast.newSimpleName("value")); final EnumDeclaration enumDeclaration = (EnumDeclaration) this.enumDomCompilationUnit.types().get(0); final EnumConstantDeclaration enumConstantDeclaration = enumDeclaration.getAST() .newEnumConstantDeclaration(); enumConstantDeclaration.setName(enumDeclaration.getAST().newSimpleName(literalName)); enumDeclaration.enumConstants().add(enumConstantDeclaration); boolean hasMessageKeyConstructor = false; for (final Iterator<Object> iterator = enumDeclaration.bodyDeclarations().iterator(); iterator.hasNext() && !hasMessageKeyConstructor;) { final Object next = iterator.next(); if (next instanceof MethodDeclaration) { final MethodDeclaration methodDeclaration = (MethodDeclaration) next; if (methodDeclaration.isConstructor() && methodDeclaration.parameters().size() > 0) { hasMessageKeyConstructor = true; } } } if (hasMessageKeyConstructor) { final StringLiteral literal = enumDeclaration.getAST().newStringLiteral(); literal.setLiteralValue(literalName); enumConstantDeclaration.arguments().add(literal); } StructuralPropertyDescriptor locationInParent = null; if (nodeFinder.getFoundNode() != null) { locationInParent = nodeFinder.getFoundNode().getLocationInParent(); } else { // TODO return false; } ResourceBundleWrapper.addImportToCompilationUnitIfMissing(nodeFinder.getParentNode(), this.packageName + "." + this.resourceBundleName); if (locationInParent.isChildListProperty()) { final List<Object> list = (List<Object>) parent.getStructuralProperty(locationInParent); final int index = list.indexOf(nodeFinder.getFoundNode()); list.remove(nodeFinder.getFoundNode()); list.add(index, replacement); } else { parent.setStructuralProperty(locationInParent, replacement); } for (final Expression parameter : resolver.getMessageParameters()) { final Expression newParameter = ASTTreeCloner.clone(parameter); replacement.arguments().add(newParameter); } String messagePattern = resolver.getMessagePattern(); if (this.prefixMessageByKey) { messagePattern = String.format("[%s] %s", literalName, messagePattern); } this.properties.put(literalName, messagePattern); return true; }
From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java
License:Open Source License
static void replaceStatement(Statement target, Statement replacement) { ASTNode p = target.getParent();/* w w w .j av a 2 s. c o m*/ if (target instanceof Block && !(replacement instanceof Block)) { Block b = replacement.getAST().newBlock(); b.statements().add(replacement); replacement = b; } StructuralPropertyDescriptor prop = target.getLocationInParent(); if (prop.isSimpleProperty() || prop.isChildProperty()) { p.setStructuralProperty(prop, replacement); } else if (prop.isChildListProperty()) { assert false; } }
From source file:de.ovgu.cide.export.physical.ahead.JakHookMethodHelper.java
License:Open Source License
private void replaceSubtreeRuleExceptionByPlaceholder() { ASTNode parent = subtreeRuleException.getParent(); StructuralPropertyDescriptor prop = subtreeRuleException.getLocationInParent(); if (prop.isSimpleProperty() || prop.isChildProperty()) { parent.setStructuralProperty(prop, exceptionPlaceholder); } else if (prop.isChildListProperty()) { assert false; }//from w ww . jav a2 s . c o m }
From source file:de.ovgu.cide.export.physical.ahead.JakHookMethodHelper.java
License:Open Source License
private void createRefinement() { refinement = createHookMethodSkeleton(); List<Statement> statementList = refinement.getBody().statements(); for (Statement stmt : statements) { Statement copyStmt = copyStatement(stmt); if (hasSubtreeRuleException && subtreeRuleExceptionParent == stmt) { assert returnValues.size() <= 1; Formal formal = null;//w w w .j a va2 s.c o m if (returnValues.size() == 1) formal = returnValues.get(0); Statement replacement = JakFeatureRefactorer.createSuperCall(refinement, ast, false, formal); /* * we place the supercall at the old position where currently * the placeholder is located */ if (subtreeRuleExceptionIsBlock) { Block b = ast.newBlock(); b.statements().add(replacement); replacement = b; } StructuralPropertyDescriptor prop = exceptionPlaceholder.getLocationInParent(); if (prop.isSimpleProperty() || prop.isChildProperty()) { copyStmt.setStructuralProperty(prop, replacement); } else if (prop.isChildListProperty()) { assert false; } } statementList.add(copyStmt); } if (subtreeRuleException == null) statementList.add(JakFeatureRefactorer.createSuperCall(refinement, ast, true, null)); else appendReturnStatement(refinement.getBody()); }