Java Code Examples for org.eclipse.jdt.core.dom.MethodDeclaration#isConstructor()

The following examples show how to use org.eclipse.jdt.core.dom.MethodDeclaration#isConstructor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalleeAnalyzerVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
    * @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;
   }
 
Example 2
Source File: CallInliner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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)));
		}
	}
}
 
Example 3
Source File: MethodUtils.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @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;
}
 
Example 4
Source File: MethodUtils.java    From tassal with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @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;
}
 
Example 5
Source File: AstUtils.java    From RefactoringMiner with MIT License 6 votes vote down vote up
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;
	}
 
Example 6
Source File: UglyMathCommentsExtractor.java    From compiler with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 7
Source File: DataTypeVisitor.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
@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;
}
 
Example 8
Source File: MethodUtils.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @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;
}
 
Example 9
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: SourceAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: MethodExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 12
Source File: ConstructorReferenceFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: JavaMethodDeclarationBindingExtractor.java    From tassal with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 14
Source File: SignalVisitor.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
@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;
}
 
Example 15
Source File: PromoteTempToFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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[] {};
}
 
Example 16
Source File: ImportReferencesCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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;
}
 
Example 17
Source File: JavaMethodDeclarationBindingExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 18
Source File: MethodScopeExtractor.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 19
Source File: JavaMethodDeclarationBindingExtractor.java    From codemining-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 20
Source File: ModifierChangeCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@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;
}