List of usage examples for org.eclipse.jdt.core.dom ASTNode setStructuralProperty
public final void setStructuralProperty(StructuralPropertyDescriptor property, Object value)
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./*ww w . j a v a 2s.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:com.google.devtools.j2cpp.translate.ClassConverter.java
License:Open Source License
@SuppressWarnings("unchecked") public static void setProperty(ASTNode node, Expression expr) { ASTNode parent = node.getParent(); StructuralPropertyDescriptor locator = node.getLocationInParent(); if (locator instanceof ChildPropertyDescriptor) { parent.setStructuralProperty(locator, expr); } else {//w w w . j a v a 2s .c om // JDT doesn't directly support ChildListProperty replacement. List<Expression> args; if (parent instanceof MethodInvocation) { args = ((MethodInvocation) parent).arguments(); } else if (parent instanceof ClassInstanceCreation) { args = ((ClassInstanceCreation) parent).arguments(); } else if (parent instanceof InfixExpression) { args = ((InfixExpression) parent).extendedOperands(); } else if (parent instanceof SynchronizedStatement) { SynchronizedStatement stmt = (SynchronizedStatement) parent; if (node.equals(stmt.getExpression())) { stmt.setExpression((Expression) node); } return; } else if (parent instanceof SuperConstructorInvocation) { args = ((SuperConstructorInvocation) parent).arguments(); } else if (parent instanceof ArrayCreation) { args = ((ArrayCreation) parent).dimensions(); } else { throw new AssertionError("unknown parent node type: " + parent.getClass().getSimpleName()); } for (int i = 0; i < args.size(); i++) { if (node.equals(args.get(i))) { args.set(i, expr); } } } }
From source file:com.google.devtools.j2cpp.translate.ClassConverter.java
License:Open Source License
@SuppressWarnings("unchecked") public static void setProperty(ASTNode node, Statement stmt) { ASTNode parent = node.getParent(); StructuralPropertyDescriptor locator = node.getLocationInParent(); if (locator instanceof ChildPropertyDescriptor) { parent.setStructuralProperty(locator, stmt); } else {//from ww w. j av a 2s . c o m // JDT doesn't directly support ChildListProperty replacement. List<Statement> args; if (parent instanceof Block) { args = ((Block) parent).statements(); } else { throw new AssertionError("unknown parent node type: " + parent.getClass().getSimpleName()); } for (int i = 0; i < args.size(); i++) { if (node.equals(args.get(i))) { args.set(i, stmt); } } } }
From source file:com.google.devtools.j2objc.util.ASTUtil.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void setProperty(ASTNode node, ASTNode newNode) {
ASTNode parent = node.getParent();
StructuralPropertyDescriptor locator = node.getLocationInParent();
if (locator instanceof ChildPropertyDescriptor) {
parent.setStructuralProperty(locator, newNode);
} else {/*w w w . ja v a 2 s.com*/
// JDT doesn't directly support ChildListProperty replacement.
List args;
if (parent instanceof ArrayCreation) {
args = ((ArrayCreation) parent).dimensions();
} else if (parent instanceof ArrayInitializer) {
args = ((ArrayInitializer) parent).expressions();
} else if (parent instanceof Block) {
args = ((Block) parent).statements();
} else if (parent instanceof ClassInstanceCreation) {
args = ((ClassInstanceCreation) parent).arguments();
} else if (parent instanceof ConstructorInvocation) {
args = ((ConstructorInvocation) parent).arguments();
} else if (parent instanceof EnumConstantDeclaration) {
args = ((EnumConstantDeclaration) parent).arguments();
} else if (parent instanceof InfixExpression) {
args = ((InfixExpression) parent).extendedOperands();
} else if (parent instanceof MethodInvocation) {
args = ((MethodInvocation) parent).arguments();
} else if (parent instanceof SuperConstructorInvocation) {
args = ((SuperConstructorInvocation) parent).arguments();
} else if (parent instanceof SuperMethodInvocation) {
args = ((SuperMethodInvocation) parent).arguments();
} else if (parent instanceof SwitchStatement) {
args = ((SwitchStatement) parent).statements();
} else if (parent instanceof TypeDeclaration) {
args = ((TypeDeclaration) parent).superInterfaceTypes();
} else {
throw new AssertionError("unknown parent node type: " + parent.getClass().getSimpleName());
}
for (int i = 0; i < args.size(); i++) {
if (node.equals(args.get(i))) {
args.set(i, newNode);
}
}
}
}
From source file:com.worldline.awltech.i18ntools.wizard.core.modules.ASTTreeCloner.java
License:Open Source License
/** * Clones the AST Tree./* w ww .j ava 2 s.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 w w . j ava2 s. c o m * @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(); if (target instanceof Block && !(replacement instanceof Block)) { Block b = replacement.getAST().newBlock(); b.statements().add(replacement); replacement = b;//from w ww . jav a 2s.c o m } 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; }/*www. j ava 2 s .com*/ }
From source file:de.ovgu.cide.export.physical.ahead.RefactoringUtils.java
License:Open Source License
public static void replaceASTNode(ASTNode target, ASTNode replacement) { ASTNode parent = target.getParent(); if (target.getLocationInParent().isChildListProperty()) { List<ASTNode> list = (List<ASTNode>) parent.getStructuralProperty(target.getLocationInParent()); int p = list.indexOf(target); list.set(p, replacement);//from w ww .ja v a 2 s . c om } else parent.setStructuralProperty(target.getLocationInParent(), replacement); }
From source file:net.atos.optimus.common.tools.ltk.ImportsGenerationVisitor.java
License:Open Source License
/** * Checks Qualified Type, represented by its use in parent node, its name, * and its type binding./*from ww w.ja v a 2 s.co m*/ * * @param node * : Parent Node * @param qualifiedName * : Qualified Name * @param typeBinding * : Type Binding */ @SuppressWarnings("unchecked") private void checkQualifiedType(ASTNode node, QualifiedName qualifiedName, ITypeBinding typeBinding) { // At first we extract package name & type for Type, by : // - Splitting the String if Type Binding has been deduced (recovered) // - Taking it from Binding otherwise String fullyQualifiedName = qualifiedName.getFullyQualifiedName(); String typeName = null; String packageName = null; if (typeBinding == null || typeBinding.isRecovered()) { typeName = qualifiedName.getFullyQualifiedName() .substring(qualifiedName.getFullyQualifiedName().lastIndexOf(".") + 1); packageName = qualifiedName.getFullyQualifiedName().substring(0, qualifiedName.getFullyQualifiedName().lastIndexOf(".")); } else { typeBinding = typeBinding.getErasure(); typeName = typeBinding.getName(); IPackageBinding packageBinding = typeBinding.getPackage(); if (packageBinding == null) return; packageName = packageBinding.getName(); } // Checks if name should be trimmed (if class with same name but // different package has already been registered), and trims it if // needed if (shouldTrimName(packageName, typeName)) { StructuralPropertyDescriptor locationInParent = qualifiedName.getLocationInParent(); if (locationInParent == null) return; if (locationInParent.isChildListProperty()) { ChildListPropertyDescriptor clpd = (ChildListPropertyDescriptor) locationInParent; List<ASTNode> astNodes = (List<ASTNode>) node.getStructuralProperty(clpd); astNodes.remove(qualifiedName); astNodes.add(node.getAST().newName(typeName)); } else { node.setStructuralProperty(locationInParent, node.getAST().newName(typeName)); } hasModifications = true; } // Checks if import should be added (e.g. package is not java.lang) and // does it if needed if (shouldAddImport(node, typeName, packageName, fullyQualifiedName)) { this.tryAddImport(node.getAST(), fullyQualifiedName); if (!typesToPackageBinding.containsKey(typeName)) typesToPackageBinding.put(typeName, packageName); } else if (this.currentPackageName.equals(packageName)) if (!typesToPackageBinding.containsKey(typeName)) typesToPackageBinding.put(typeName, packageName); }