org.eclipse.jdt.core.dom.ASTNode#getStructuralProperty ( )源码实例Demo

下面列出了org.eclipse.jdt.core.dom.ASTNode#getStructuralProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: eclipse-cs   文件: AbstractASTResolution.java
/**
 * 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.
 * @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;
}
 
源代码2 项目: vscode-checkstyle   文件: BaseQuickFix.java
/**
 * 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.
 * @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;
}
 
源代码3 项目: eclipse.jdt.ls   文件: TypeAnnotationRewrite.java
/**
 * Removes all {@link Annotation} whose only {@link Target} is {@link ElementType#TYPE_USE} from
 * <code>node</code>'s <code>childListProperty</code>.
 * <p>
 * In a combination of {@link ElementType#TYPE_USE} and {@link ElementType#TYPE_PARAMETER}
 * the latter is ignored, because this is implied by the former and creates no ambiguity.</p>
 *
 * @param node ASTNode
 * @param childListProperty child list property
 * @param rewrite rewrite that removes the nodes
 * @param editGroup the edit group in which to collect the corresponding text edits, or null if
 *            ungrouped
 */
public static void removePureTypeAnnotations(ASTNode node, ChildListPropertyDescriptor childListProperty, ASTRewrite rewrite, TextEditGroup editGroup) {
	CompilationUnit root= (CompilationUnit) node.getRoot();
	if (!JavaModelUtil.is18OrHigher(root.getJavaElement().getJavaProject())) {
		return;
	}
	ListRewrite listRewrite= rewrite.getListRewrite(node, childListProperty);
	@SuppressWarnings("unchecked")
	List<? extends ASTNode> children= (List<? extends ASTNode>) node.getStructuralProperty(childListProperty);
	for (ASTNode child : children) {
		if (child instanceof Annotation) {
			Annotation annotation= (Annotation) child;
			if (isPureTypeAnnotation(annotation)) {
				listRewrite.remove(child, editGroup);
			}
		}
	}
}
 
源代码4 项目: eclipse.jdt.ls   文件: ExtractTempRefactoring.java
private ASTNode getEnclosingBodyNode() throws JavaModelException {
	ASTNode node = getSelectedExpression().getAssociatedNode();

	// expression must be in a method, lambda or initializer body
	// make sure it is not in method or parameter annotation
	StructuralPropertyDescriptor location = null;
	while (node != null && !(node instanceof BodyDeclaration)) {
		location = node.getLocationInParent();
		node = node.getParent();
		if (node instanceof LambdaExpression) {
			break;
		}
	}
	if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) {
		return (ASTNode) node.getStructuralProperty(location);
	}
	return null;
}
 
源代码5 项目: eclipse.jdt.ls   文件: ExtractFieldRefactoring.java
private ASTNode getEnclosingBodyNode() throws JavaModelException {
	ASTNode node = getSelectedExpression().getAssociatedNode();

	// expression must be in a method, lambda or initializer body.
	// make sure it is not in method or parameter annotation
	StructuralPropertyDescriptor location = null;
	while (node != null && !(node instanceof BodyDeclaration)) {
		location = node.getLocationInParent();
		node = node.getParent();
		if (node instanceof LambdaExpression) {
			break;
		}
	}
	if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) {
		return (ASTNode) node.getStructuralProperty(location);
	}
	return null;
}
 
源代码6 项目: eclipse.jdt.ls   文件: DimensionRewrite.java
/**
 * Removes all children in <code>node</code>'s <code>childListProperty</code>.
 *
 * @param node ASTNode
 * @param childListProperty child list property
 * @param rewrite rewrite that removes the nodes
 * @param editGroup the edit group in which to collect the corresponding text edits, or null if ungrouped
 */
public static void removeAllChildren(ASTNode node, ChildListPropertyDescriptor childListProperty, ASTRewrite rewrite, TextEditGroup editGroup) {
	ListRewrite listRewrite= rewrite.getListRewrite(node, childListProperty);
	@SuppressWarnings("unchecked")
	List<? extends ASTNode> children= (List<? extends ASTNode>) node.getStructuralProperty(childListProperty);
	for (ASTNode child : children) {
		listRewrite.remove(child, editGroup);
	}
}
 
源代码7 项目: xtext-xtend   文件: ASTFlattenerUtils.java
public List<ASTNode> genericChildListProperty(final ASTNode node, final String propertyName) {
  final Function1<ChildListPropertyDescriptor, Boolean> _function = (ChildListPropertyDescriptor it) -> {
    String _id = it.getId();
    return Boolean.valueOf(Objects.equal(propertyName, _id));
  };
  final ChildListPropertyDescriptor property = IterableExtensions.<ChildListPropertyDescriptor>head(IterableExtensions.<ChildListPropertyDescriptor>filter(Iterables.<ChildListPropertyDescriptor>filter(node.structuralPropertiesForType(), ChildListPropertyDescriptor.class), _function));
  if ((property != null)) {
    Object _structuralProperty = node.getStructuralProperty(property);
    return ((List<ASTNode>) _structuralProperty);
  }
  return null;
}
 
源代码8 项目: xtext-xtend   文件: ASTFlattenerUtils.java
public ASTNode genericChildProperty(final ASTNode node, final String propertyName) {
  final Function1<ChildPropertyDescriptor, Boolean> _function = (ChildPropertyDescriptor it) -> {
    String _id = it.getId();
    return Boolean.valueOf(Objects.equal(propertyName, _id));
  };
  final ChildPropertyDescriptor property = IterableExtensions.<ChildPropertyDescriptor>head(IterableExtensions.<ChildPropertyDescriptor>filter(Iterables.<ChildPropertyDescriptor>filter(node.structuralPropertiesForType(), ChildPropertyDescriptor.class), _function));
  if ((property != null)) {
    Object _structuralProperty = node.getStructuralProperty(property);
    return ((ASTNode) _structuralProperty);
  }
  return null;
}
 
public boolean isDangligIf() {
	List<Statement> statements= fDeclaration.getBody().statements();
	if (statements.size() != 1)
		return false;

	ASTNode p= statements.get(0);

	while (true) {
		if (p instanceof IfStatement) {
			return ((IfStatement) p).getElseStatement() == null;
		} else {

			ChildPropertyDescriptor childD;
			if (p instanceof WhileStatement) {
				childD= WhileStatement.BODY_PROPERTY;
			} else if (p instanceof ForStatement) {
				childD= ForStatement.BODY_PROPERTY;
			} else if (p instanceof EnhancedForStatement) {
				childD= EnhancedForStatement.BODY_PROPERTY;
			} else if (p instanceof DoStatement) {
				childD= DoStatement.BODY_PROPERTY;
			} else if (p instanceof LabeledStatement) {
				childD= LabeledStatement.BODY_PROPERTY;
			} else {
				return false;
			}
			Statement body= (Statement) p.getStructuralProperty(childD);
			if (body instanceof Block) {
				return false;
			} else {
				p= body;
			}
		}
	}
}
 
private Block getEnclosingBodyNode() throws JavaModelException {
	ASTNode node= getSelectedExpression().getAssociatedNode();

	// expression must be in a method or initializer body
	// make sure it is not in method or parameter annotation
	StructuralPropertyDescriptor location= null;
	while (node != null && !(node instanceof BodyDeclaration)) {
		location= node.getLocationInParent();
		node= node.getParent();
	}
	if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY) {
		return (Block) node.getStructuralProperty(location);
	}
	return null;
}
 
/**
 * Removes all children in <code>node</code>'s <code>childListProperty</code>.
 * 
 * @param node ASTNode
 * @param childListProperty child list property
 * @param rewrite rewrite that removes the nodes
 * @param editGroup the edit group in which to collect the corresponding text edits, or null if ungrouped
 */
public static void removeAllChildren(ASTNode node, ChildListPropertyDescriptor childListProperty, ASTRewrite rewrite, TextEditGroup editGroup) {
	ListRewrite listRewrite= rewrite.getListRewrite(node, childListProperty);
	@SuppressWarnings("unchecked")
	List<? extends ASTNode> children= (List<? extends ASTNode>) node.getStructuralProperty(childListProperty);
	for (ASTNode child : children) {
		listRewrite.remove(child, editGroup);
	}
}
 
private Object accessOriginalValue(ASTNode parent, StructuralPropertyDescriptor childProperty) {
	if (this.nodePropertyMapper != null) {
		return this.nodePropertyMapper.getOriginalValue(parent, childProperty);
	}

	return parent.getStructuralProperty(childProperty);
}
 
源代码13 项目: junion   文件: BaseTranslator.java
public void replace(ASTNode old, ASTNode neww) {
		
//		if(!copyStack.isEmpty()) {
//			if(copyStack.get(copyStack.size()-1) == old) {
//				copyStack.set(copyStack.size()-1, neww);
//				Log.err("COPY STACK REPLACE");
//				Object oldProp = old.getProperty(TYPEBIND_PROP);
//				if(oldProp != null && neww.getProperty(TYPEBIND_PROP) == null) {
//					Log.err("   copy old prop");
//					neww.setProperty(TYPEBIND_PROP, oldProp);
//				}
//				Object oldProp2 = old.getProperty(TYPEBIND_METHOD_TMP);
//				if(oldProp2 != null && neww.getProperty(TYPEBIND_METHOD_TMP) == null) {
//					Log.err("   copy old prop2");
//					neww.setProperty(TYPEBIND_METHOD_TMP, oldProp2);
//				}
//				return;
//			}
//		}
		old.setProperty(IS_REPLACE, neww);
		ASTNode parent = old.getParent();
		StructuralPropertyDescriptor desc = old.getLocationInParent();
		if(desc instanceof ChildListPropertyDescriptor) {
			ChildListPropertyDescriptor ch = (ChildListPropertyDescriptor)desc;
			List<ASTNode> list = (List)parent.getStructuralProperty(ch);
			
			int index = list.indexOf(old);
			list.set(index, neww);			
		}
		else {
			if(parent instanceof QualifiedName && 
					QualifiedName.QUALIFIER_PROPERTY.getId().equals(desc.getId()) &&
				!(neww instanceof SimpleName)) {
				if(!(neww instanceof Expression))throw new IllegalArgumentException();
				//QualifiedName has to be changed to FieldAccess
				
//				throw new IllegalArgumentException("qual name expression");
				FieldAccess fa = ast.newFieldAccess();
				fa.setExpression((Expression)neww);
				fa.setName((SimpleName)copySubtreeIfHasParent(((QualifiedName)parent).getName()));
				
//				for(Map.Entry ee : (Set<Map.Entry>)parent.properties().entrySet()) {
//					Log.err("ee " + ee.getKey());
//				}
				if(parent.getProperty(TYPEBIND_PROP) == null) 
					fa.setProperty(TYPEBIND_PROP, parent.getProperty(TYPEBIND_PROP));
					
				replace(parent, fa);
				return;
			}
			parent.setStructuralProperty(desc, neww);
		}		
	}
 
public List<ResourceChange> createTopLevelParameterObject(IPackageFragmentRoot packageFragmentRoot, CreationListener listener) throws CoreException {
	List<ResourceChange> changes= new ArrayList<ResourceChange>();
	IPackageFragment packageFragment= packageFragmentRoot.getPackageFragment(getPackage());
	if (!packageFragment.exists()) {
		changes.add(new CreatePackageChange(packageFragment));
	}
	ICompilationUnit unit= packageFragment.getCompilationUnit(getClassName() + JavaModelUtil.DEFAULT_CU_SUFFIX);
	Assert.isTrue(!unit.exists());
	IJavaProject javaProject= unit.getJavaProject();
	ICompilationUnit workingCopy= unit.getWorkingCopy(null);

	try {
		// create stub with comments and dummy type
		String lineDelimiter= StubUtility.getLineDelimiterUsed(javaProject);
		String fileComment= getFileComment(workingCopy, lineDelimiter);
		String typeComment= getTypeComment(workingCopy, lineDelimiter);
		String content= CodeGeneration.getCompilationUnitContent(workingCopy, fileComment, typeComment, "class " + getClassName() + "{}", lineDelimiter); //$NON-NLS-1$ //$NON-NLS-2$
		workingCopy.getBuffer().setContents(content);

		CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(workingCopy);
		ASTRewrite rewriter= cuRewrite.getASTRewrite();
		CompilationUnit root= cuRewrite.getRoot();
		AST ast= cuRewrite.getAST();
		ImportRewrite importRewrite= cuRewrite.getImportRewrite();

		// retrieve&replace dummy type with real class
		ListRewrite types= rewriter.getListRewrite(root, CompilationUnit.TYPES_PROPERTY);
		ASTNode dummyType= (ASTNode) types.getOriginalList().get(0);
		String newTypeName= JavaModelUtil.concatenateName(getPackage(), getClassName());
		TypeDeclaration classDeclaration= createClassDeclaration(newTypeName, cuRewrite, listener);
		classDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
		Javadoc javadoc= (Javadoc) dummyType.getStructuralProperty(TypeDeclaration.JAVADOC_PROPERTY);
		rewriter.set(classDeclaration, TypeDeclaration.JAVADOC_PROPERTY, javadoc, null);
		types.replace(dummyType, classDeclaration, null);

		// Apply rewrites and discard workingcopy
		// Using CompilationUnitRewrite.createChange() leads to strange
		// results
		String charset= ResourceUtil.getFile(unit).getCharset(false);
		Document document= new Document(content);
		try {
			rewriter.rewriteAST().apply(document);
			TextEdit rewriteImports= importRewrite.rewriteImports(null);
			rewriteImports.apply(document);
		} catch (BadLocationException e) {
			throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), RefactoringCoreMessages.IntroduceParameterObjectRefactoring_parameter_object_creation_error, e));
		}
		String docContent= document.get();
		CreateCompilationUnitChange compilationUnitChange= new CreateCompilationUnitChange(unit, docContent, charset);
		changes.add(compilationUnitChange);
	} finally {
		workingCopy.discardWorkingCopy();
	}
	return changes;
}
 
源代码15 项目: Eclipse-Postfix-Code-Completion   文件: ASTNodes.java
/**
 * Variant of {@link ASTNode#getStructuralProperty(StructuralPropertyDescriptor)} that avoids
 * unchecked casts in the caller.
 * <p>
 * To improve type-safety, callers can add the expected element type as explicit type argument, e.g.:
 * <p>
 * {@code ASTNodes.<BodyDeclaration>getChildListProperty(typeDecl, bodyDeclarationsProperty)}
 * 
 * @param node the node
 * @param propertyDescriptor the child list property to get
 * @return the child list
 * @exception RuntimeException if this node does not have the given property
 */
@SuppressWarnings("unchecked")
public static <T extends ASTNode> List<T> getChildListProperty(ASTNode node, ChildListPropertyDescriptor propertyDescriptor) {
	return (List<T>) node.getStructuralProperty(propertyDescriptor);
}