下面列出了怎么用org.eclipse.jdt.core.dom.ChildPropertyDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* {@inheritDoc}
*/
@Override
protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo,
final int markerStartPosition) {
return new ASTVisitor() {
@Override
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;
}
};
}
@Override
public ASTVisitor getCorrectingASTVisitor(IRegion lineInfo, int markerStartOffset) {
return new ASTVisitor() {
@Override
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
final StructuralPropertyDescriptor p = node.getLocationInParent();
if (p.isChildProperty() && ((ChildPropertyDescriptor) p).isMandatory()) {
return false;
}
node.delete();
}
return false;
}
};
}
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 void handle(Statement body, ChildPropertyDescriptor bodyProperty) {
if ((body.getFlags() & ASTNode.RECOVERED) != 0)
return;
Statement parent= (Statement)body.getParent();
if ((parent.getFlags() & ASTNode.RECOVERED) != 0)
return;
if (fRemoveUnnecessaryBlocksOnlyWhenReturnOrThrow) {
if (!(body instanceof Block)) {
if (body.getNodeType() != ASTNode.IF_STATEMENT && body.getNodeType() != ASTNode.RETURN_STATEMENT && body.getNodeType() != ASTNode.THROW_STATEMENT) {
fResult.add(new AddBlockOperation(bodyProperty, body, parent));
}
} else {
if (RemoveBlockOperation.satisfiesCleanUpPrecondition(parent, bodyProperty, true)) {
fResult.add(new RemoveBlockOperation(parent, bodyProperty));
}
}
} else if (fFindControlStatementsWithoutBlock) {
if (!(body instanceof Block)) {
fResult.add(new AddBlockOperation(bodyProperty, body, parent));
}
} else if (fRemoveUnnecessaryBlocks) {
if (RemoveBlockOperation.satisfiesCleanUpPrecondition(parent, bodyProperty, false)) {
fResult.add(new RemoveBlockOperation(parent, bodyProperty));
}
}
}
@Override
protected ChildPropertyDescriptor getJavaDocProperty() {
return MethodDeclaration.JAVADOC_PROPERTY;
}
@Override
protected ChildPropertyDescriptor getBodyProperty() {
return MethodDeclaration.BODY_PROPERTY;
}
@Override
protected ChildPropertyDescriptor getJavaDocProperty() {
return FieldDeclaration.JAVADOC_PROPERTY;
}
@Override
protected ChildPropertyDescriptor getBodyProperty() {
return VariableDeclarationFragment.INITIALIZER_PROPERTY;
}
@Override
protected ChildPropertyDescriptor getJavaDocProperty() {
return MethodDeclaration.JAVADOC_PROPERTY;
}
@Override
protected ChildPropertyDescriptor getBodyProperty() {
return MethodDeclaration.BODY_PROPERTY;
}
@Override
protected ChildPropertyDescriptor getJavaDocProperty() {
return FieldDeclaration.JAVADOC_PROPERTY;
}
@Override
protected ChildPropertyDescriptor getBodyProperty() {
return VariableDeclarationFragment.INITIALIZER_PROPERTY;
}
public AddBlockOperation(ChildPropertyDescriptor bodyProperty, Statement body, Statement controlStatement) {
fBodyProperty= bodyProperty;
fBody= body;
fControlStatement= controlStatement;
}
public RemoveBlockOperation(Statement controlStatement, ChildPropertyDescriptor child) {
fStatement= controlStatement;
fChild= child;
}
public static boolean satisfiesCleanUpPrecondition(Statement controlStatement, ChildPropertyDescriptor childDescriptor, boolean onlyReturnAndThrows) {
return satisfiesPrecondition(controlStatement, childDescriptor, onlyReturnAndThrows, true);
}
public static boolean satisfiesQuickAssistPrecondition(Statement controlStatement, ChildPropertyDescriptor childDescriptor) {
return satisfiesPrecondition(controlStatement, childDescriptor, false, false);
}
private static boolean satisfiesPrecondition(Statement controlStatement, ChildPropertyDescriptor childDescriptor, boolean onlyReturnAndThrows, boolean cleanUpCheck) {
Object child= controlStatement.getStructuralProperty(childDescriptor);
if (!(child instanceof Block))
return false;
Block block= (Block)child;
List<Statement> list= block.statements();
if (list.size() != 1)
return false;
ASTNode singleStatement= list.get(0);
if (onlyReturnAndThrows)
if (!(singleStatement instanceof ReturnStatement) && !(singleStatement instanceof ThrowStatement))
return false;
if (controlStatement instanceof IfStatement) {
// if (true) {
// while (true)
// if (false)
// ;
// } else
// ;
if (((IfStatement)controlStatement).getThenStatement() != child)
return true;//can always remove blocks in else part
IfStatement ifStatement= (IfStatement)controlStatement;
if (ifStatement.getElseStatement() == null)
return true;//can always remove if no else part
return !hasUnblockedIf((Statement)singleStatement, onlyReturnAndThrows, cleanUpCheck);
} else {
//if (true)
// while (true) {
// if (false)
// ;
// }
//else
// ;
if (!hasUnblockedIf((Statement)singleStatement, onlyReturnAndThrows, cleanUpCheck))
return true;
ASTNode currentChild= controlStatement;
ASTNode parent= currentChild.getParent();
while (true) {
Statement body= null;
if (parent instanceof IfStatement) {
body= ((IfStatement)parent).getThenStatement();
if (body == currentChild && ((IfStatement)parent).getElseStatement() != null)//->currentChild is an unblocked then part
return false;
} else if (parent instanceof WhileStatement) {
body= ((WhileStatement)parent).getBody();
} else if (parent instanceof DoStatement) {
body= ((DoStatement)parent).getBody();
} else if (parent instanceof ForStatement) {
body= ((ForStatement)parent).getBody();
} else if (parent instanceof EnhancedForStatement) {
body= ((EnhancedForStatement)parent).getBody();
} else {
return true;
}
if (body != currentChild)//->parents child is a block
return true;
currentChild= parent;
parent= currentChild.getParent();
}
}
}
/**
* Returns the javadoc property descriptor. The javadoc will be added using
* this descriptor.
*
* @return property descriptor
*/
protected abstract ChildPropertyDescriptor getJavaDocProperty();
/**
* Returns the body property descriptor. The body of the delegate will be
* added using this descriptor.
*
* @return property descriptor
*/
protected abstract ChildPropertyDescriptor getBodyProperty();
/**
* Returns the javadoc property descriptor. The javadoc will be added using
* this descriptor.
*
* @return property descriptor
*/
protected abstract ChildPropertyDescriptor getJavaDocProperty();
/**
* Returns the body property descriptor. The body of the delegate will be
* added using this descriptor.
*
* @return property descriptor
*/
protected abstract ChildPropertyDescriptor getBodyProperty();