下面列出了怎么用org.eclipse.jdt.core.dom.SuperConstructorInvocation的API类实例代码及写法,或者点击链接到github查看源代码。
private SuperConstructorInv visit(SuperConstructorInvocation node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
SuperConstructorInv superConstructorInv = new SuperConstructorInv(startLine, endLine, node);
if(node.getExpression() != null){
Expr expression = (Expr) process(node.getExpression());
expression.setParent(superConstructorInv);
superConstructorInv.setExpression(expression);
}
List<Expr> arguments = new ArrayList<>();
for(Object object : node.arguments()){
Expr arg = (Expr) process((ASTNode) object);
arg.setParent(superConstructorInv);
arguments.add(arg);
}
superConstructorInv.setArguments(arguments);
return superConstructorInv;
}
private SuperConstructorInvocation addEnclosingInstanceAccess(ASTRewrite rewrite, ImportRewriteContext importRewriteContext, List<SingleVariableDeclaration> parameters, String[] paramNames, ITypeBinding enclosingInstance) {
AST ast= rewrite.getAST();
SuperConstructorInvocation invocation= ast.newSuperConstructorInvocation();
SingleVariableDeclaration var= ast.newSingleVariableDeclaration();
var.setType(getImportRewrite().addImport(enclosingInstance, ast, importRewriteContext, TypeLocation.PARAMETER));
String[] enclosingArgNames= StubUtility.getArgumentNameSuggestions(getCompilationUnit().getJavaProject(), enclosingInstance.getTypeDeclaration().getName(), 0, paramNames);
String firstName= enclosingArgNames[0];
var.setName(ast.newSimpleName(firstName));
parameters.add(var);
Name enclosing= ast.newSimpleName(firstName);
invocation.setExpression(enclosing);
String key= "arg_name_" + firstName; //$NON-NLS-1$
addLinkedPosition(rewrite.track(enclosing), false, key);
for (int i= 0; i < enclosingArgNames.length; i++) {
addLinkedPositionProposal(key, enclosingArgNames[i]); // alternative names
}
return invocation;
}
@Override
public boolean visit(final SuperConstructorInvocation node) {
Expression _expression = node.getExpression();
boolean _tripleNotEquals = (_expression != null);
if (_tripleNotEquals) {
node.getExpression().accept(this);
this.appendToBuffer(".");
}
boolean _isEmpty = node.typeArguments().isEmpty();
boolean _not = (!_isEmpty);
if (_not) {
this.appendTypeParameters(node.typeArguments());
}
this.appendToBuffer("super(");
this.visitAllSeparatedByComma(node.arguments());
this.appendToBuffer(")");
return false;
}
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) {
AST ast= subclass.getAST();
MethodDeclaration newConstructor= ast.newMethodDeclaration();
newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier()));
newConstructor.setConstructor(true);
newConstructor.setJavadoc(null);
newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass)));
newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
Block body= ast.newBlock();
newConstructor.setBody(body);
SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation();
addArgumentsToNewSuperConstructorCall(superCall, cuRewrite);
body.statements().add(superCall);
String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor;
TextEditGroup description= cuRewrite.createGroupDescription(msg);
cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description);
// TODO use AbstractTypeDeclaration
}
public static List<Expression> getArguments(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).arguments();
case ASTNode.SUPER_METHOD_INVOCATION:
return ((SuperMethodInvocation)invocation).arguments();
case ASTNode.CONSTRUCTOR_INVOCATION:
return ((ConstructorInvocation)invocation).arguments();
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).arguments();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).arguments();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return ((EnumConstantDeclaration)invocation).arguments();
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static ChildListPropertyDescriptor getArgumentsProperty(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return MethodInvocation.ARGUMENTS_PROPERTY;
case ASTNode.SUPER_METHOD_INVOCATION:
return SuperMethodInvocation.ARGUMENTS_PROPERTY;
case ASTNode.CONSTRUCTOR_INVOCATION:
return ConstructorInvocation.ARGUMENTS_PROPERTY;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return SuperConstructorInvocation.ARGUMENTS_PROPERTY;
case ASTNode.CLASS_INSTANCE_CREATION:
return ClassInstanceCreation.ARGUMENTS_PROPERTY;
case ASTNode.ENUM_CONSTANT_DECLARATION:
return EnumConstantDeclaration.ARGUMENTS_PROPERTY;
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static Expression getExpression(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).getExpression();
case ASTNode.SUPER_METHOD_INVOCATION:
return null;
case ASTNode.CONSTRUCTOR_INVOCATION:
return null;
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).getExpression();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).getExpression();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return null;
default:
throw new IllegalArgumentException(invocation.toString());
}
}
public static IMethodBinding resolveBinding(ASTNode invocation) {
switch (invocation.getNodeType()) {
case ASTNode.METHOD_INVOCATION:
return ((MethodInvocation)invocation).resolveMethodBinding();
case ASTNode.SUPER_METHOD_INVOCATION:
return ((SuperMethodInvocation)invocation).resolveMethodBinding();
case ASTNode.CONSTRUCTOR_INVOCATION:
return ((ConstructorInvocation)invocation).resolveConstructorBinding();
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
return ((SuperConstructorInvocation)invocation).resolveConstructorBinding();
case ASTNode.CLASS_INSTANCE_CREATION:
return ((ClassInstanceCreation)invocation).resolveConstructorBinding();
case ASTNode.ENUM_CONSTANT_DECLARATION:
return ((EnumConstantDeclaration)invocation).resolveConstructorBinding();
default:
throw new IllegalArgumentException(invocation.toString());
}
}
private static IBinding resolveBinding(ASTNode node) {
if (node instanceof SimpleName) {
SimpleName simpleName= (SimpleName) node;
// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
ASTNode normalized= ASTNodes.getNormalizedNode(simpleName);
if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
ClassInstanceCreation cic= (ClassInstanceCreation) normalized.getParent();
IMethodBinding constructorBinding= cic.resolveConstructorBinding();
if (constructorBinding == null)
return null;
ITypeBinding declaringClass= constructorBinding.getDeclaringClass();
if (!declaringClass.isAnonymous())
return constructorBinding;
ITypeBinding superTypeDeclaration= declaringClass.getSuperclass().getTypeDeclaration();
return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
}
return simpleName.resolveBinding();
} else if (node instanceof SuperConstructorInvocation) {
return ((SuperConstructorInvocation) node).resolveConstructorBinding();
} else if (node instanceof ConstructorInvocation) {
return ((ConstructorInvocation) node).resolveConstructorBinding();
} else {
return null;
}
}
private SuperConstructorInvocation addEnclosingInstanceAccess(ASTRewrite rewrite, ImportRewriteContext importRewriteContext, List<SingleVariableDeclaration> parameters, String[] paramNames, ITypeBinding enclosingInstance) {
AST ast= rewrite.getAST();
SuperConstructorInvocation invocation= ast.newSuperConstructorInvocation();
SingleVariableDeclaration var= ast.newSingleVariableDeclaration();
var.setType(getImportRewrite().addImport(enclosingInstance, ast, importRewriteContext));
String[] enclosingArgNames= StubUtility.getArgumentNameSuggestions(getCompilationUnit().getJavaProject(), enclosingInstance.getTypeDeclaration().getName(), 0, paramNames);
String firstName= enclosingArgNames[0];
var.setName(ast.newSimpleName(firstName));
parameters.add(var);
Name enclosing= ast.newSimpleName(firstName);
invocation.setExpression(enclosing);
String key= "arg_name_" + firstName; //$NON-NLS-1$
addLinkedPosition(rewrite.track(enclosing), false, key);
for (int i= 0; i < enclosingArgNames.length; i++) {
addLinkedPositionProposal(key, enclosingArgNames[i], null); // alternative names
}
return invocation;
}
public boolean visit(SuperConstructorInvocation stmnt){
/*
* [ Expression . ]
[ < Type { , Type } > ]
super ( [ Expression { , Expression } ] ) ;
*/
if (stmnt.getExpression() != null){
handleExpression((Expression) stmnt.getExpression());
appendPeriod();
}
handleTypeArguments(stmnt.typeArguments());
styledString.append("super", new StyledStringStyler(keywordStyle));
handleParameters(stmnt.arguments());
appendSemicolon();
return false;
}
public boolean visit(SuperConstructorInvocation node) {
int start = _unit.getLineNumber(node.getStartPosition());
if(start == _extendedLine){
_extendedStatement = node;
return false;
}
return true;
}
private void populateBodyWithSuperConstructorCall(AST ast, TypeDeclaration builderType, Block body, List<ConstructorParameterSetterBuilderField> builderFields) {
SuperConstructorInvocation superInvocation = ast.newSuperConstructorInvocation();
builderFields.stream()
.sorted((first, second) -> first.getIndex().compareTo(second.getIndex()))
.forEach(constructorParameter -> addConstructorParameter(ast, builderType, superInvocation, constructorParameter));
if (!builderFields.isEmpty()) {
body.statements().add(superInvocation);
}
}
@Override
public boolean visit(SuperConstructorInvocation node) {
// XXX Hack for performance reasons (should loop over fJobSemanticHighlightings can call consumes(*))
if (fJobDeprecatedMemberHighlighting != null) {
IMethodBinding constructorBinding= node.resolveConstructorBinding();
if (constructorBinding != null && constructorBinding.isDeprecated()) {
int offset= node.getStartPosition();
int length= 5;
if (offset > -1 && length > 0) {
addPosition(offset, length, fJobDeprecatedMemberHighlighting);
}
}
}
return true;
}
private static IBinding resolveBinding(ASTNode node) {
if (node instanceof SimpleName) {
SimpleName simpleName = (SimpleName) node;
// workaround for https://bugs.eclipse.org/62605 (constructor name resolves to type, not method)
ASTNode normalized = ASTNodes.getNormalizedNode(simpleName);
if (normalized.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
ClassInstanceCreation cic = (ClassInstanceCreation) normalized.getParent();
IMethodBinding constructorBinding = cic.resolveConstructorBinding();
if (constructorBinding == null) {
return null;
}
ITypeBinding declaringClass = constructorBinding.getDeclaringClass();
if (!declaringClass.isAnonymous()) {
return constructorBinding;
}
ITypeBinding superTypeDeclaration = declaringClass.getSuperclass().getTypeDeclaration();
return resolveSuperclassConstructor(superTypeDeclaration, constructorBinding);
}
return simpleName.resolveBinding();
} else if (node instanceof SuperConstructorInvocation) {
return ((SuperConstructorInvocation) node).resolveConstructorBinding();
} else if (node instanceof ConstructorInvocation) {
return ((ConstructorInvocation) node).resolveConstructorBinding();
} else if (node instanceof LambdaExpression) {
return ((LambdaExpression) node).resolveMethodBinding();
} else {
return null;
}
}
private boolean isUsedInExplicitConstructorCall() throws JavaModelException {
Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
if (ASTNodes.getParent(selectedExpression, ConstructorInvocation.class) != null) {
return true;
}
if (ASTNodes.getParent(selectedExpression, SuperConstructorInvocation.class) != null) {
return true;
}
return false;
}
private boolean isUsedInExplicitConstructorCall() throws JavaModelException {
Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
if (ASTNodes.getParent(selectedExpression, ConstructorInvocation.class) != null) {
return true;
}
if (ASTNodes.getParent(selectedExpression, SuperConstructorInvocation.class) != null) {
return true;
}
return false;
}
@Override
public boolean visit(SuperConstructorInvocation node) {
if (!isSelected(node)) {
return false;
}
return handleExceptions(node.resolveConstructorBinding(), node);
}
private static Expression getBooleanExpression(ASTNode node) {
if (!(node instanceof Expression)) {
return null;
}
// check if the node is a location where it can be negated
StructuralPropertyDescriptor locationInParent = node.getLocationInParent();
if (locationInParent == QualifiedName.NAME_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
while (locationInParent == ParenthesizedExpression.EXPRESSION_PROPERTY) {
node = node.getParent();
locationInParent = node.getLocationInParent();
}
Expression expression = (Expression) node;
if (!isBoolean(expression)) {
return null;
}
if (expression.getParent() instanceof InfixExpression) {
return expression;
}
if (locationInParent == Assignment.RIGHT_HAND_SIDE_PROPERTY || locationInParent == IfStatement.EXPRESSION_PROPERTY || locationInParent == WhileStatement.EXPRESSION_PROPERTY || locationInParent == DoStatement.EXPRESSION_PROPERTY
|| locationInParent == ReturnStatement.EXPRESSION_PROPERTY || locationInParent == ForStatement.EXPRESSION_PROPERTY || locationInParent == AssertStatement.EXPRESSION_PROPERTY
|| locationInParent == MethodInvocation.ARGUMENTS_PROPERTY || locationInParent == ConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == SuperMethodInvocation.ARGUMENTS_PROPERTY
|| locationInParent == EnumConstantDeclaration.ARGUMENTS_PROPERTY || locationInParent == SuperConstructorInvocation.ARGUMENTS_PROPERTY || locationInParent == ClassInstanceCreation.ARGUMENTS_PROPERTY
|| locationInParent == ConditionalExpression.EXPRESSION_PROPERTY || locationInParent == PrefixExpression.OPERAND_PROPERTY) {
return expression;
}
return null;
}
public OperationInvocation(CompilationUnit cu, String filePath, SuperConstructorInvocation invocation) {
this.locationInfo = new LocationInfo(cu, filePath, invocation, CodeElementType.SUPER_CONSTRUCTOR_INVOCATION);
this.methodName = "super";
this.typeArguments = invocation.arguments().size();
this.arguments = new ArrayList<String>();
List<Expression> args = invocation.arguments();
for(Expression argument : args) {
this.arguments.add(argument.toString());
}
if(invocation.getExpression() != null) {
this.expression = invocation.getExpression().toString();
processExpression(invocation.getExpression(), this.subExpressions);
}
}
/**
* handles super(parameter)
*/
@SuppressWarnings("unchecked")
@Override
public boolean visit(SuperConstructorInvocation node) {
Invocation invocation = importer.createInvocationFromMethodBinding(node.resolveConstructorBinding(),
node.toString().trim());
importer.createLightweightSourceAnchor(invocation, node);
node.arguments().stream().forEach(arg -> importer.createAccessFromExpression((Expression) arg));
return true;
}
private Expression createEnclosingInstanceCreationString(final ASTNode node, final ICompilationUnit cu) throws JavaModelException {
Assert.isTrue((node instanceof ClassInstanceCreation) || (node instanceof SuperConstructorInvocation));
Assert.isNotNull(cu);
Expression expression= null;
if (node instanceof ClassInstanceCreation)
expression= ((ClassInstanceCreation) node).getExpression();
else
expression= ((SuperConstructorInvocation) node).getExpression();
final AST ast= node.getAST();
if (expression != null)
return expression;
else if (JdtFlags.isStatic(fType))
return null;
else if (isInsideSubclassOfDeclaringType(node))
return ast.newThisExpression();
else if ((node.getStartPosition() >= fType.getSourceRange().getOffset() && ASTNodes.getExclusiveEnd(node) <= fType.getSourceRange().getOffset() + fType.getSourceRange().getLength())) {
if (fCodeGenerationSettings.useKeywordThis || fEnclosingInstanceFieldName.equals(fNameForEnclosingInstanceConstructorParameter)) {
final FieldAccess access= ast.newFieldAccess();
access.setExpression(ast.newThisExpression());
access.setName(ast.newSimpleName(fEnclosingInstanceFieldName));
return access;
} else
return ast.newSimpleName(fEnclosingInstanceFieldName);
} else if (isInsideTypeNestedInDeclaringType(node)) {
final ThisExpression qualified= ast.newThisExpression();
qualified.setQualifier(ast.newSimpleName(fType.getDeclaringType().getElementName()));
return qualified;
}
return null;
}
private boolean insertExpressionAsParameter(SuperConstructorInvocation sci, ASTRewrite rewrite, ICompilationUnit cu, TextEditGroup group) throws JavaModelException {
final Expression expression= createEnclosingInstanceCreationString(sci, cu);
if (expression == null)
return false;
rewrite.getListRewrite(sci, SuperConstructorInvocation.ARGUMENTS_PROPERTY).insertFirst(expression, group);
return true;
}
private boolean isInsideSubclassOfDeclaringType(ASTNode node) {
Assert.isTrue((node instanceof ClassInstanceCreation) || (node instanceof SuperConstructorInvocation));
final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) ASTNodes.getParent(node, AbstractTypeDeclaration.class);
Assert.isNotNull(declaration);
final AnonymousClassDeclaration anonymous= (AnonymousClassDeclaration) ASTNodes.getParent(node, AnonymousClassDeclaration.class);
boolean isAnonymous= anonymous != null && ASTNodes.isParent(anonymous, declaration);
if (isAnonymous)
return anonymous != null && isSubclassBindingOfEnclosingType(anonymous.resolveBinding());
return isSubclassBindingOfEnclosingType(declaration.resolveBinding());
}
private boolean isInsideTypeNestedInDeclaringType(ASTNode node) {
Assert.isTrue((node instanceof ClassInstanceCreation) || (node instanceof SuperConstructorInvocation));
final AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) ASTNodes.getParent(node, AbstractTypeDeclaration.class);
Assert.isNotNull(declaration);
ITypeBinding enclosing= declaration.resolveBinding();
while (enclosing != null) {
if (isCorrespondingTypeBinding(enclosing, fType.getDeclaringType()))
return true;
enclosing= enclosing.getDeclaringClass();
}
return false;
}
private void updateConstructorReference(ITypeBinding[] parameters, ASTNode reference, CompilationUnitRewrite targetRewrite, ICompilationUnit cu) throws CoreException {
final TextEditGroup group= targetRewrite.createGroupDescription(RefactoringCoreMessages.MoveInnerToTopRefactoring_update_constructor_reference);
if (reference instanceof SuperConstructorInvocation)
updateConstructorReference((SuperConstructorInvocation) reference, targetRewrite, cu, group);
else if (reference instanceof ClassInstanceCreation)
updateConstructorReference((ClassInstanceCreation) reference, targetRewrite, cu, group);
else if (reference.getParent() instanceof ClassInstanceCreation)
updateConstructorReference((ClassInstanceCreation) reference.getParent(), targetRewrite, cu, group);
else if (reference.getParent() instanceof ParameterizedType && reference.getParent().getParent() instanceof ClassInstanceCreation)
updateConstructorReference(parameters, (ParameterizedType) reference.getParent(), targetRewrite, cu, group);
}
private void updateConstructorReference(final SuperConstructorInvocation invocation, final CompilationUnitRewrite targetRewrite, final ICompilationUnit unit, TextEditGroup group) throws CoreException {
Assert.isNotNull(invocation);
Assert.isNotNull(targetRewrite);
Assert.isNotNull(unit);
final ASTRewrite rewrite= targetRewrite.getASTRewrite();
if (fCreateInstanceField)
insertExpressionAsParameter(invocation, rewrite, unit, group);
final Expression expression= invocation.getExpression();
if (expression != null) {
rewrite.remove(expression, null);
targetRewrite.getImportRemover().registerRemovedNode(expression);
}
}
private static SuperConstructorInvocation getSuperConstructorCallNode(IMethod constructor, CompilationUnit cuNode) throws JavaModelException {
Assert.isTrue(constructor.isConstructor());
MethodDeclaration constructorNode= ASTNodeSearchUtil.getMethodDeclarationNode(constructor, cuNode);
Assert.isTrue(constructorNode.isConstructor());
Block body= constructorNode.getBody();
Assert.isNotNull(body);
List<Statement> statements= body.statements();
if (! statements.isEmpty() && statements.get(0) instanceof SuperConstructorInvocation)
return (SuperConstructorInvocation)statements.get(0);
return null;
}
private void addExplicitSuperConstructorCall(MethodDeclaration constructor, CompilationUnitRewrite cuRewrite) {
SuperConstructorInvocation superCall= constructor.getAST().newSuperConstructorInvocation();
addArgumentsToNewSuperConstructorCall(superCall, cuRewrite);
String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_super_call;
TextEditGroup description= cuRewrite.createGroupDescription(msg);
cuRewrite.getASTRewrite().getListRewrite(constructor.getBody(), Block.STATEMENTS_PROPERTY).insertFirst(superCall, description);
}
private void addArgumentsToNewSuperConstructorCall(SuperConstructorInvocation superCall, CompilationUnitRewrite cuRewrite) {
Iterator<ParameterInfo> iter= getNotDeletedInfos().iterator();
while (iter.hasNext()) {
ParameterInfo info= iter.next();
Expression newExpression= createNewExpression(info, getParameterInfos(), superCall.arguments(), cuRewrite, (MethodDeclaration) ASTNodes.getParent(superCall, MethodDeclaration.class));
if (newExpression != null)
superCall.arguments().add(newExpression);
}
}