下面列出了org.eclipse.jdt.core.dom.MethodDeclaration#isConstructor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @see HierarchicalASTVisitor#visit(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)
*/
@Override
public boolean visit(AbstractTypeDeclaration node) {
progressMonitorWorked(1);
if (!isFurtherTraversalNecessary(node)) {
return false;
}
if (isNodeWithinMethod(node)) {
List<BodyDeclaration> bodyDeclarations= node.bodyDeclarations();
for (Iterator<BodyDeclaration> iter= bodyDeclarations.iterator(); iter.hasNext(); ) {
BodyDeclaration bodyDeclaration= iter.next();
if (bodyDeclaration instanceof MethodDeclaration) {
MethodDeclaration child= (MethodDeclaration) bodyDeclaration;
if (child.isConstructor()) {
addMethodCall(child.resolveBinding(), child.getName());
}
}
}
return false;
}
return true;
}
private void checkMethodDeclaration(RefactoringStatus result, int severity) {
MethodDeclaration methodDeclaration= fSourceProvider.getDeclaration();
// it is not allowed to inline constructor invocation only if it is used for class instance creation
// if constructor is invoked from another constructor then we can inline such invocation
if (fInvocation.getNodeType() != ASTNode.CONSTRUCTOR_INVOCATION && methodDeclaration.isConstructor()) {
result.addEntry(new RefactoringStatusEntry(
severity,
RefactoringCoreMessages.CallInliner_constructors,
JavaStatusContext.create(fCUnit, fInvocation)));
}
if (fSourceProvider.hasSuperMethodInvocation() && fInvocation.getNodeType() == ASTNode.METHOD_INVOCATION) {
Expression receiver= ((MethodInvocation)fInvocation).getExpression();
if (receiver instanceof ThisExpression) {
result.addEntry(new RefactoringStatusEntry(
severity,
RefactoringCoreMessages.CallInliner_super_into_this_expression,
JavaStatusContext.create(fCUnit, fInvocation)));
}
}
}
/**
* @param node
* @return
*/
public static String getMethodType(final MethodDeclaration node) {
final StringBuffer typeSb = new StringBuffer();
if (node.getReturnType2() != null) {
typeSb.append(node.getReturnType2().toString()).append("(");
} else if (node.isConstructor()) {
typeSb.append("constructor(");
} else {
typeSb.append("void(");
}
for (final Object svd : node.parameters()) {
final SingleVariableDeclaration decl = (SingleVariableDeclaration) svd;
typeSb.append(decl.getType().toString());
typeSb.append(",");
}
typeSb.append(")");
final String methodType = typeSb.toString();
return methodType;
}
public static String getSignatureFromMethodDeclaration(MethodDeclaration methodDeclaration) {
String methodName = methodDeclaration.isConstructor() ? "" : methodDeclaration.getName().getIdentifier();
// if (methodName.equals("allObjectsSorted")) {
// System.out.println();
// }
StringBuilder sb = new StringBuilder();
sb.append(methodName);
sb.append('(');
Iterator<SingleVariableDeclaration> parameters = methodDeclaration.parameters().iterator();
while (parameters.hasNext()) {
SingleVariableDeclaration parameter = parameters.next();
Type parameterType = parameter.getType();
String typeName = normalizeTypeName(parameterType, parameter.getExtraDimensions(), parameter.isVarargs());
sb.append(typeName);
if (parameters.hasNext()) {
sb.append(", ");
}
}
sb.append(')');
String methodSignature = sb.toString();
return methodSignature;
}
private static int getNodeStartPosition(final ASTNode node) {
if (node instanceof MethodDeclaration) {
MethodDeclaration decl = (MethodDeclaration) node;
return decl.isConstructor() ? decl.getName().getStartPosition() : decl.getReturnType2().getStartPosition();
} else if (node instanceof FieldDeclaration) {
return ((FieldDeclaration) node).getType().getStartPosition();
} else if (node instanceof AbstractTypeDeclaration) {
return ((AbstractTypeDeclaration) node).getName().getStartPosition();
} else if (node instanceof AnnotationTypeMemberDeclaration) {
return ((AnnotationTypeMemberDeclaration) node).getName().getStartPosition();
} else if (node instanceof EnumConstantDeclaration) {
return ((EnumConstantDeclaration) node).getName().getStartPosition();
} else if (node instanceof PackageDeclaration) {
return ((PackageDeclaration) node).getName().getStartPosition();
}
/* TODO: Initializer */
return node.getStartPosition();
}
@Override
public boolean visit(MethodDeclaration node) {
if (!node.isConstructor()) {
if (node.getReturnType2() != null
&& !Utils.isAllowedParameterType(node.getReturnType2().resolveBinding(), true)) {
collector.report(INVALID_PARAMETER_TYPE.create(collector.getSourceInfo(), node.getReturnType2()));
}
}
Utils.checkModifiers(collector, node);
for (Object obj : node.parameters()) {
SingleVariableDeclaration param = (SingleVariableDeclaration) obj;
if (!Utils.isAllowedParameterType(param.getType().resolveBinding(), false)) {
collector.report(INVALID_PARAMETER_TYPE.create(collector.getSourceInfo(), param.getType()));
}
}
// TODO: check body
return false;
}
/**
* @param node
* @return
*/
public static String getMethodType(final MethodDeclaration node) {
final StringBuffer typeSb = new StringBuffer();
if (node.getReturnType2() != null) {
typeSb.append(node.getReturnType2().toString()).append("(");
} else if (node.isConstructor()) {
typeSb.append("constructor(");
} else {
typeSb.append("void(");
}
for (final Object svd : node.parameters()) {
final SingleVariableDeclaration decl = (SingleVariableDeclaration) svd;
typeSb.append(decl.getType().toString());
typeSb.append(",");
}
typeSb.append(")");
final String methodType = typeSb.toString();
return methodType;
}
/**
* @param node
* @return
*/
public static String getMethodType(final MethodDeclaration node) {
final StringBuffer typeSb = new StringBuffer();
if (node.getReturnType2() != null) {
typeSb.append(node.getReturnType2().toString()).append("(");
} else if (node.isConstructor()) {
typeSb.append("constructor(");
} else {
typeSb.append("void(");
}
for (final Object svd : node.parameters()) {
final SingleVariableDeclaration decl = (SingleVariableDeclaration) svd;
typeSb.append(decl.getType().toString());
typeSb.append(",");
}
typeSb.append(")");
final String methodType = typeSb.toString();
return methodType;
}
@Override
public boolean visit(final MethodDeclaration node) {
if (node.isConstructor()) {
return super.visit(node);
} else if (!includeOverrides && methodOverrides(node)) {
return super.visit(node);
}
final String name = node.getName().toString();
methodNamePostions.put(name, node.getName());
return super.visit(node);
}
@Override
public boolean visit(MethodDeclaration node) {
if (currentMethodNode == null) {
currentMethodNode = node;
}
if (node.isConstructor())
return super.visit(node);
final String name = node.getName().toString();
final Method mth = new Method(name, ScopeType.SCOPE_CLASS);
if (classNode != null) {
methods.put(classNode, mth);
}
return super.visit(node);
}
@Override
public boolean visit(final MethodDeclaration node) {
if (node.isConstructor()) {
return super.visit(node);
} else if (!includeOverrides && methodOverrides(node)) {
return super.visit(node);
}
final String name = node.getName().toString();
methodNamePostions.put(name, node.getName());
return super.visit(node);
}
@Override
public boolean visit(MethodDeclaration node) {
if (!isAffected(node)) {
return false;
}
doVisitNode(node.getJavadoc());
doVisitChildren(node.modifiers());
doVisitChildren(node.typeParameters());
if (!node.isConstructor()) {
doVisitNode(node.getReturnType2());
}
// name not visited
int apiLevel= node.getAST().apiLevel();
if (apiLevel >= AST.JLS8) {
doVisitNode(node.getReceiverType());
}
// receiverQualifier not visited:
// Enclosing class names cannot be shadowed by an import (qualification is always redundant).
doVisitChildren(node.parameters());
if (apiLevel >= AST.JLS8) {
doVisitChildren(node.extraDimensions());
doVisitChildren(node.thrownExceptionTypes());
} else {
Iterator<Name> iter= getThrownExceptions(node).iterator();
while (iter.hasNext()) {
typeRefFound(iter.next());
}
}
if (!fSkipMethodBodies) {
doVisitNode(node.getBody());
}
return false;
}
private static MethodDeclaration[] getAllConstructors(AbstractTypeDeclaration typeDeclaration) {
if (typeDeclaration instanceof TypeDeclaration) {
MethodDeclaration[] allMethods= ((TypeDeclaration) typeDeclaration).getMethods();
List<MethodDeclaration> result= new ArrayList<MethodDeclaration>(Math.min(allMethods.length, 1));
for (int i= 0; i < allMethods.length; i++) {
MethodDeclaration declaration= allMethods[i];
if (declaration.isConstructor())
result.add(declaration);
}
return result.toArray(new MethodDeclaration[result.size()]);
}
return new MethodDeclaration[] {};
}
@Override
public boolean visit(MethodDeclaration elem) {
if (!elem.isConstructor()) {
collector.report(INVALID_SIGNAL_CONTENT.create(collector.getSourceInfo(), elem));
} else {
checkConstructor(elem);
Utils.checkModifiers(collector, elem);
}
return false;
}
@Override
public boolean visit(final MethodDeclaration node) {
if (node.isConstructor()) {
return super.visit(node);
} else if (!includeOverrides && methodOverrides(node)) {
return super.visit(node);
}
final String name = node.getName().toString();
methodNamePostions.put(name, node.getName());
return super.visit(node);
}
private boolean isRealConstructorReferenceNode(ASTNode node){
String typeName= fConstructors[0].getDeclaringType().getElementName();
if (node.getParent() instanceof AbstractTypeDeclaration
&& ((AbstractTypeDeclaration) node.getParent()).getNameProperty().equals(node.getLocationInParent())) {
//Example:
// class A{
// A(){}
// }
// class B extends A {}
//==> "B" is found as reference to A()
return false;
}
if (node.getParent() instanceof MethodDeclaration
&& MethodDeclaration.NAME_PROPERTY.equals(node.getLocationInParent())) {
MethodDeclaration md= (MethodDeclaration)node.getParent();
if (md.isConstructor() && ! md.getName().getIdentifier().equals(typeName)) {
//Example:
// class A{
// A(){}
// }
// class B extends A{
// B(){}
// }
//==> "B" in "B(){}" is found as reference to A()
return false;
}
}
return true;
}
@Override
public boolean visit(final MethodDeclaration node) {
if (node.isConstructor()) {
return super.visit(node);
} else if (isOverride(node)) {
return super.visit(node);
}
allMethods.add(node);
return super.visit(node);
}
@Override
public boolean visit(MethodDeclaration node) {
if (node.isConstructor()) {
AbstractTypeDeclaration decl= (AbstractTypeDeclaration) ASTNodes.getParent(node, AbstractTypeDeclaration.class);
NameData name= fNames.get(decl.getName().resolveBinding());
if (name != null) {
name.addReference(node.getName());
}
}
return true;
}
public static boolean isInsideConstructorInvocation(MethodDeclaration methodDeclaration, ASTNode node) {
if (methodDeclaration.isConstructor()) {
Statement statement= ASTResolving.findParentStatement(node);
if (statement instanceof ConstructorInvocation || statement instanceof SuperConstructorInvocation) {
return true; // argument in a this or super call
}
}
return false;
}
@Override
protected ASTRewrite getRewrite() throws CoreException {
CompilationUnit astRoot= ASTResolving.findParentCompilationUnit(fNode);
ASTNode boundNode= astRoot.findDeclaringNode(fBinding);
ASTNode declNode= null;
if (boundNode != null) {
declNode= boundNode; // is same CU
} else {
//setSelectionDescription(selectionDescription);
CompilationUnit newRoot= ASTResolving.createQuickFixAST(getCompilationUnit(), null);
declNode= newRoot.findDeclaringNode(fBinding.getKey());
}
if (declNode != null) {
AST ast= declNode.getAST();
ASTRewrite rewrite= ASTRewrite.create(ast);
if (declNode.getNodeType() == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
VariableDeclarationFragment fragment= (VariableDeclarationFragment)declNode;
ASTNode parent= declNode.getParent();
if (parent instanceof FieldDeclaration) {
FieldDeclaration fieldDecl= (FieldDeclaration) parent;
if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) { // split
VariableDeclarationRewrite.rewriteModifiers(fieldDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, null);
return rewrite;
}
} else if (parent instanceof VariableDeclarationStatement) {
VariableDeclarationStatement varDecl= (VariableDeclarationStatement) parent;
if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) { // split
VariableDeclarationRewrite.rewriteModifiers(varDecl, new VariableDeclarationFragment[] {fragment}, fIncludedModifiers, fExcludedModifiers, rewrite, null);
return rewrite;
}
} else if (parent instanceof VariableDeclarationExpression) {
// can't separate
}
declNode= parent;
} else if (declNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
MethodDeclaration methodDecl= (MethodDeclaration) declNode;
if (!methodDecl.isConstructor()) {
IMethodBinding methodBinding= methodDecl.resolveBinding();
if (methodDecl.getBody() == null && methodBinding != null && Modifier.isAbstract(methodBinding.getModifiers()) && Modifier.isStatic(fIncludedModifiers)) {
// add body
ICompilationUnit unit= getCompilationUnit();
String delimiter= unit.findRecommendedLineSeparator();
String bodyStatement= ""; //$NON-NLS-1$
Block body= ast.newBlock();
rewrite.set(methodDecl, MethodDeclaration.BODY_PROPERTY, body, null);
Type returnType= methodDecl.getReturnType2();
if (returnType != null) {
Expression expression= ASTNodeFactory.newDefaultExpression(ast, returnType, methodDecl.getExtraDimensions());
if (expression != null) {
ReturnStatement returnStatement= ast.newReturnStatement();
returnStatement.setExpression(expression);
bodyStatement= ASTNodes.asFormattedString(returnStatement, 0, delimiter, unit.getJavaProject().getOptions(true));
}
}
String placeHolder= CodeGeneration.getMethodBodyContent(unit, methodBinding.getDeclaringClass().getName(), methodBinding.getName(), false, bodyStatement, delimiter);
if (placeHolder != null) {
ReturnStatement todoNode= (ReturnStatement) rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
body.statements().add(todoNode);
}
}
}
}
ModifierRewrite listRewrite= ModifierRewrite.create(rewrite, declNode);
PositionInformation trackedDeclNode= listRewrite.setModifiers(fIncludedModifiers, fExcludedModifiers, null);
LinkedProposalPositionGroup positionGroup= new LinkedProposalPositionGroup("group"); //$NON-NLS-1$
positionGroup.addPosition(trackedDeclNode);
getLinkedProposalModel().addPositionGroup(positionGroup);
if (boundNode != null) {
// only set end position if in same CU
setEndPosition(rewrite.track(fNode));
}
return rewrite;
}
return null;
}