List of usage examples for org.eclipse.jdt.core.dom StructuralPropertyDescriptor isChildProperty
public final boolean isChildProperty()
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./*from w w w. j ava 2 s .c om*/ * @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:changenodes.matching.BestLeafTreeMatcher.java
License:Apache License
@SuppressWarnings("unchecked") private void markMatchedNode(ASTNode left, ASTNode right) { if (left.getNodeType() != right.getNodeType()) { return;/*www .ja v a 2s .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:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java
License:Open Source License
static void replaceStatement(Statement target, Statement replacement) { ASTNode p = target.getParent();/*from www .j a v a 2 s . c om*/ 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; }/* w ww .j a v 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;/*from w w w . ja 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()); }
From source file:de.ovgu.cide.language.jdt.ASTBridge.java
License:Open Source License
private ASTNode bridgeASTNode(org.eclipse.jdt.core.dom.ASTNode e_node) { ASTNode result = bridgeCache.get(e_node); if (result != null) return result; IToken c_firstToken = new PosToken(e_node.getStartPosition()); IToken c_lastToken = new PosToken(e_node.getStartPosition() + e_node.getLength()); IASTNode[] wrappee = null;//www .j a v a2 s. c om List<StructuralPropertyDescriptor> e_props = e_node.structuralPropertiesForType(); List<Property> c_props = new ArrayList<Property>(); for (StructuralPropertyDescriptor e_prop : e_props) { if (e_prop.isSimpleProperty()) { Property prop = bridgeSimpleProperty(e_node, (SimplePropertyDescriptor) e_prop); if (prop != null) { if (ASTColorInheritance.notInheritedProperties.contains(e_prop)) wrappee = join(wrappee, prop.getChildren()); c_props.add(prop); } } else if (e_prop.isChildListProperty()) { Property prop = bridgeChildListProperty(e_node, (ChildListPropertyDescriptor) e_prop); if (ASTColorInheritance.notInheritedProperties.contains(e_prop)) wrappee = join(wrappee, prop.getChildren()); c_props.add(prop); } else if (e_prop.isChildProperty()) { Property prop = bridgeChildProperty(e_node, (ChildPropertyDescriptor) e_prop); if (prop != null) { if (ASTColorInheritance.notInheritedProperties.contains(e_prop)) wrappee = join(wrappee, prop.getChildren()); c_props.add(prop); } } } result = new UnifiedASTNode(getDisplayName(e_node), ASTID.id(e_node), c_props, c_firstToken, c_lastToken, wrappee, getKind(e_node)); bridgeCache.put(e_node, result); return result; }
From source file:edu.virginia.aid.visitors.ControlFlowGraphVisitor.java
License:Apache License
/** find sequential next in case the statement is in a try-catch-block */ private Statement findNextStatementInParentList(ASTNode node) { assert node != null; Statement next;//w ww .jav a 2 s .co m ASTNode parent = node.getParent(); assert parent != null; StructuralPropertyDescriptor location = node.getLocationInParent(); assert location != null; if (location.isChildProperty()) { next = findNextStatementInParentList(parent); } else { assert location.isChildListProperty(); List<?> l = (List<?>) parent.getStructuralProperty(location); int index = l.indexOf(node); if (index + 1 < l.size()) { if (l.get(index + 1) instanceof Statement) { next = (Statement) l.get(index + 1); } else { assert l.get(index + 1) instanceof MethodDeclaration; next = last; } } else { next = findNextStatementInParentList(parent.getParent()); } } return next; }
From source file:net.sf.eclipsecs.ui.quickfixes.AbstractASTResolution.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//from w w w . j a va 2s . c o m * The node to replace. * @param replacement * The replacement node. * @return <code>true</code> if the node was successfully replaced. */ protected 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:net.sf.eclipsecs.ui.quickfixes.coding.EmptyStatementQuickfix.java
License:Open Source License
/** * {@inheritDoc}/*from w w w . ja va2s.co m*/ */ protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartPosition) { return new ASTVisitor() { public boolean visit(EmptyStatement node) { if (containsPosition(lineInfo, node.getStartPosition())) { // early exit if the statement is mandatory, e.g. only // statement in a for-statement without block StructuralPropertyDescriptor p = node.getLocationInParent(); if (p.isChildProperty() && ((ChildPropertyDescriptor) p).isMandatory()) { return false; } node.delete(); } return false; } }; }
From source file:org.eclipse.jet.internal.xpath.inspectors.jdt.InspectASTNode.java
License:Open Source License
public Object[] getChildren(final Object node) { final ASTNode astNode = (ASTNode) node; final List spds = getElementPropertyDescriptors(astNode); final List children = new ArrayList(spds.size()); for (final Iterator i = spds.iterator(); i.hasNext();) { final StructuralPropertyDescriptor spd = (StructuralPropertyDescriptor) i.next(); final Object structuralProperty = astNode.getStructuralProperty(spd); if (spd.isChildProperty() && structuralProperty != null) { children.add(structuralProperty); } else if (spd.isChildListProperty() && structuralProperty != null) { final List spList = (List) structuralProperty; children.addAll(spList);//www . java2s .c o m } } return children.toArray(); }