Java Code Examples for org.eclipse.jdt.core.IMethod#exists()

The following examples show how to use org.eclipse.jdt.core.IMethod#exists() . 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: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean isIntroduceIndirectionAvailable(IMethod method) throws JavaModelException {
	if (method == null) {
		return false;
	}
	if (!method.exists()) {
		return false;
	}
	if (!method.isStructureKnown()) {
		return false;
	}
	if (method.isConstructor()) {
		return false;
	}
	if (method.getDeclaringType().isAnnotation()) {
		return false;
	}
	if (JavaModelUtil.isPolymorphicSignature(method)) {
		return false;
	}

	return true;
}
 
Example 2
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isGeneralizeTypeAvailable(final IStructuredSelection selection) throws JavaModelException {
	if (selection.size() == 1) {
		final Object element= selection.getFirstElement();
		if (element instanceof IMethod) {
			final IMethod method= (IMethod) element;
			if (!method.exists())
				return false;
			final String type= method.getReturnType();
			if (PrimitiveType.toCode(Signature.toString(type)) == null)
				return Checks.isAvailable(method);
		} else if (element instanceof IField) {
			final IField field= (IField) element;
			if (!field.exists())
				return false;
			if (!JdtFlags.isEnum(field))
				return Checks.isAvailable(field);
		}
	}
	return false;
}
 
Example 3
Source File: RenameMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private RefactoringStatus checkRelatedMethods() throws CoreException {
	RefactoringStatus result= new RefactoringStatus();
	for (Iterator<IMethod> iter= fMethodsToRename.iterator(); iter.hasNext(); ) {
		IMethod method= iter.next();

		result.merge(Checks.checkIfConstructorName(method, getNewElementName(), method.getDeclaringType().getElementName()));

		String[] msgData= new String[]{BasicElementLabels.getJavaElementName(method.getElementName()), BasicElementLabels.getJavaElementName(method.getDeclaringType().getFullyQualifiedName('.'))};
		if (! method.exists()){
			result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_not_in_model, msgData));
			continue;
		}
		if (method.isBinary())
			result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_binary, msgData));
		if (method.isReadOnly())
			result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_read_only, msgData));
		if (JdtFlags.isNative(method))
			result.addError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_native_1, msgData));
	}
	return result;
}
 
Example 4
Source File: AnnotationAtttributeProposalInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Resolves the member described by the receiver and returns it if found.
 * Returns <code>null</code> if no corresponding member can be found.
 *
 * @return the resolved member or <code>null</code> if none is found
 * @throws JavaModelException if accessing the java model fails
 */
@Override
protected IMember resolveMember() throws JavaModelException {
	char[] declarationSignature= fProposal.getDeclarationSignature();
	// for synthetic fields on arrays, declaration signatures may be null
	// TODO remove when https://bugs.eclipse.org/bugs/show_bug.cgi?id=84690 gets fixed
	if (declarationSignature == null)
		return null;
	String typeName= SignatureUtil.stripSignatureToFQN(String.valueOf(declarationSignature));
	IType type= fJavaProject.findType(typeName);
	if (type != null) {
		String name= String.valueOf(fProposal.getName());
		IMethod method= type.getMethod(name, CharOperation.NO_STRINGS);
		if (method.exists())
			return method;
	}
	return null;
}
 
Example 5
Source File: RenameMethodProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private RefactoringStatus checkRelatedMethods() throws CoreException {
	RefactoringStatus result= new RefactoringStatus();
	for (Iterator<IMethod> iter= fMethodsToRename.iterator(); iter.hasNext(); ) {
		IMethod method= iter.next();

		result.merge(Checks.checkIfConstructorName(method, getNewElementName(), method.getDeclaringType().getElementName()));

		String[] msgData= new String[]{BasicElementLabels.getJavaElementName(method.getElementName()), BasicElementLabels.getJavaElementName(method.getDeclaringType().getFullyQualifiedName('.'))};
		if (! method.exists()){
			result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_not_in_model, msgData));
			continue;
		}
		if (method.isBinary()) {
			result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_binary, msgData));
		}
		if (method.isReadOnly()) {
			result.addFatalError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_read_only, msgData));
		}
		if (JdtFlags.isNative(method)) {
			result.addError(Messages.format(RefactoringCoreMessages.RenameMethodRefactoring_no_native_1, msgData));
		}
	}
	return result;
}
 
Example 6
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isIntroduceIndirectionAvailable(IMethod method) throws JavaModelException {
	if (method == null)
		return false;
	if (!method.exists())
		return false;
	if (!method.isStructureKnown())
		return false;
	if (method.isConstructor())
		return false;
	if (method.getDeclaringType().isAnnotation())
		return false;
	if (JavaModelUtil.isPolymorphicSignature(method))
		return false;

	return true;
}
 
Example 7
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private IMethod findJavaMethod(IType type) {
	// Locate the method from its signature.
	//
	String[] parameterTypes = Signature.getParameterTypes(new BindingKey("Lx;.x(" + signature + ")").toSignature());
	IMethod javaMethod = type.getMethod(name, parameterTypes);

	// If the method doesn't exist and this is a nested type...
	//
	if (!javaMethod.exists() && type.getDeclaringType() != null) {
		// This special case handles what appears to be a JDT bug 
		// that sometimes it knows when there are implicit constructor arguments for nested types and sometimes it doesn't.
		// Infer one more initial parameter type and locate the method based on that.
		//
		String[] augmented = new String[parameterTypes.length + 1];
		System.arraycopy(parameterTypes, 0, augmented, 1, parameterTypes.length);
		String first = Signature.createTypeSignature(type.getDeclaringType().getFullyQualifiedName(), true);
		augmented[0] = first;
		javaMethod = type.getMethod(name, augmented);
	}
	return javaMethod;
}
 
Example 8
Source File: Bug403554Test.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	IJavaProject project = getJavaProject(null);
	IType type = project.findType(ArrayList.class.getName());
	IMethod method = type.getMethod("subList", new String[] { "I", "I" });
	while (!method.exists()) {
		String superclassName = type.getSuperclassName();
		int idx = superclassName.indexOf("<");
		if (idx != -1) {
			superclassName = superclassName.substring(0, idx);
		}
		type = project.findType(superclassName);
		method = type.getMethod("subList", new String[] { "I", "I" });
	}
	declarator = type.getElementName();
}
 
Example 9
Source File: JdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private boolean isParameterNamesAvailable() throws Exception {
	ASTParser parser = ASTParser.newParser(AST.JLS3);
	parser.setIgnoreMethodBodies(true);
	IJavaProject javaProject = projectProvider.getJavaProject(resourceSet);
	parser.setProject(javaProject);
	IType type = javaProject.findType("org.eclipse.xtext.common.types.testSetups.TestEnum");
	IBinding[] bindings = parser.createBindings(new IJavaElement[] { type }, null);
	ITypeBinding typeBinding = (ITypeBinding) bindings[0];
	IMethodBinding[] methods = typeBinding.getDeclaredMethods();
	for(IMethodBinding method: methods) {
		if (method.isConstructor()) {
			IMethod element = (IMethod) method.getJavaElement();
			if (element.exists()) {
				String[] parameterNames = element.getParameterNames();
				if (parameterNames.length == 1 && parameterNames[0].equals("string")) {
					return true;
				}
			} else {
				return false;
			}
		}
	}
	return false;
}
 
Example 10
Source File: RenameFieldProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public String getNewGetterName() throws CoreException {
	IMethod primaryGetterCandidate= JavaModelUtil.findMethod(GetterSetterUtil.getGetterName(fField, new String[0]), new String[0], false, fField.getDeclaringType());
	if (! JavaModelUtil.isBoolean(fField) || (primaryGetterCandidate != null && primaryGetterCandidate.exists())) {
		return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), JavaModelUtil.isBoolean(fField), null);
	}
	//bug 30906 describes why we need to look for other alternatives here
	return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), false, null);
}
 
Example 11
Source File: RenameFieldProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private RefactoringStatus checkNewAccessor(IMethod existingAccessor, String newAccessorName) throws CoreException{
	RefactoringStatus result= new RefactoringStatus();
	IMethod accessor= JavaModelUtil.findMethod(newAccessorName, existingAccessor.getParameterTypes(), false, fField.getDeclaringType());
	if (accessor == null || !accessor.exists()) {
		return null;
	}

	String message= Messages.format(RefactoringCoreMessages.RenameFieldRefactoring_already_exists,
			new String[]{JavaElementUtil.createMethodSignature(accessor), BasicElementLabels.getJavaElementName(fField.getDeclaringType().getFullyQualifiedName('.'))});
	result.addError(message, JavaStatusContext.create(accessor));
	return result;
}
 
Example 12
Source File: JavaDeleteProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IMethod[] getGetterSetter(IField field) throws JavaModelException {
	IMethod getter= GetterSetterUtil.getGetter(field);
	IMethod setter= GetterSetterUtil.getSetter(field);
	if (getter != null && getter.exists() || 	setter != null && setter.exists())
		return new IMethod[]{getter, setter};
	else
		return null;
}
 
Example 13
Source File: LaunchPipelineShortcut.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private void launchNew(LaunchableResource resource, String mode) {
  try {
    ILaunchManager launchManager = getLaunchManager();

    String launchConfigurationName =
        launchManager.generateLaunchConfigurationName(resource.getLaunchName());
    ILaunchConfigurationType configurationType =
        getDataflowLaunchConfigurationType(launchManager);
    ILaunchConfigurationWorkingCopy configuration =
        configurationType.newInstance(null, launchConfigurationName);

    configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
        resource.getProjectName());
    IMethod mainMethod = resource.getMainMethod();
    if (mainMethod != null && mainMethod.exists()) {
      configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME,
          mainMethod.getDeclaringType().getFullyQualifiedName());
    }

    String groupIdentifier =
        mode.equals(ILaunchManager.RUN_MODE) ? IDebugUIConstants.ID_RUN_LAUNCH_GROUP
            : IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP;
    int returnStatus =
        DebugUITools.openLaunchConfigurationDialog(DataflowUiPlugin.getActiveWindowShell(),
            configuration, groupIdentifier, null);
    if (returnStatus == Window.OK) {
      configuration.doSave();
    }
  } catch (CoreException e) {
    // TODO: Handle
    DataflowUiPlugin.logError(e, "Error while launching new Launch Configuration for project %s",
        resource.getProjectName());
  }
}
 
Example 14
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static boolean isReplaceInvocationsAvailable(IMethod method) throws JavaModelException {
	if (method == null) {
		return false;
	}
	if (!method.exists()) {
		return false;
	}
	if (method.isConstructor()) {
		return false;
	}
	return true;
}
 
Example 15
Source File: MethodGeneratorImpl.java    From jenerate with Eclipse Public License 1.0 5 votes vote down vote up
private Set<IMethod> getExcludedMethods(IType objectClass, LinkedHashSet<MethodSkeleton<U>> methodSkeletons)
        throws Exception {
    Set<IMethod> excludedMethods = new HashSet<IMethod>();
    for (MethodSkeleton<U> methodSkeleton : methodSkeletons) {
        IMethod excludedMethod = objectClass.getMethod(methodSkeleton.getMethodName(),
                methodSkeleton.getMethodArguments(objectClass));
        if (excludedMethod.exists()) {
            excludedMethods.add(excludedMethod);
        }
    }
    return excludedMethods;
}
 
Example 16
Source File: RenameTypeWizardSimilarElementsPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IStatus validateSettings() {
	final String name= fNameField.getText();
	if (name.length() == 0) {
		return new StatusInfo(IStatus.ERROR, RefactoringMessages.RenameTypeWizardSimilarElementsPage_name_empty);
	}
	IStatus status= JavaConventionsUtil.validateIdentifier(name, fElementToEdit);
	if (status.matches(IStatus.ERROR))
		return status;
	if (!Checks.startsWithLowerCase(name))
		return new StatusInfo(IStatus.WARNING, RefactoringMessages.RenameTypeWizardSimilarElementsPage_name_should_start_lowercase);

	if (fElementToEdit instanceof IMember && ((IMember) fElementToEdit).getDeclaringType() != null) {
		IType type= ((IMember) fElementToEdit).getDeclaringType();
		if (fElementToEdit instanceof IField) {
			final IField f= type.getField(name);
			if (f.exists())
				return new StatusInfo(IStatus.ERROR, RefactoringMessages.RenameTypeWizardSimilarElementsPage_field_exists);
		}
		if (fElementToEdit instanceof IMethod) {
			final IMethod m= type.getMethod(name, ((IMethod) fElementToEdit).getParameterTypes());
			if (m.exists())
				return new StatusInfo(IStatus.ERROR, RefactoringMessages.RenameTypeWizardSimilarElementsPage_method_exists);
		}
	}

	// cannot check local variables; no .getLocalVariable(String) in IMember

	return StatusInfo.OK_STATUS;
}
 
Example 17
Source File: RenameFieldProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public String getNewGetterName() throws CoreException {
	IMethod primaryGetterCandidate= JavaModelUtil.findMethod(GetterSetterUtil.getGetterName(fField, new String[0]), new String[0], false, fField.getDeclaringType());
	if (! JavaModelUtil.isBoolean(fField) || (primaryGetterCandidate != null && primaryGetterCandidate.exists()))
		return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), JavaModelUtil.isBoolean(fField), null);
	//bug 30906 describes why we need to look for other alternatives here
	return GetterSetterUtil.getGetterName(fField.getJavaProject(), getNewElementName(), fField.getFlags(), false, null);
}
 
Example 18
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IMethod getGetter(IField field) throws JavaModelException{
	String getterName= getGetterName(field, EMPTY, true);
	IMethod primaryCandidate= JavaModelUtil.findMethod(getterName, new String[0], false, field.getDeclaringType());
	if (! JavaModelUtil.isBoolean(field) || (primaryCandidate != null && primaryCandidate.exists()))
		return primaryCandidate;
	//bug 30906 describes why we need to look for other alternatives here (try with get... for booleans)
	String secondCandidateName= getGetterName(field, EMPTY, false);
	return JavaModelUtil.findMethod(secondCandidateName, new String[0], false, field.getDeclaringType());
}
 
Example 19
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isMoveMethodAvailable(final IMethod method) throws JavaModelException {
	return method.exists() && !method.isConstructor() && !method.isBinary() && !method.isReadOnly()
			&& !JdtFlags.isStatic(method) && (JdtFlags.isDefaultMethod(method) || !method.getDeclaringType().isInterface());
}
 
Example 20
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static boolean isMoveMethodAvailable(final IMethod method) throws JavaModelException {
	return method.exists() && !method.isConstructor() && !method.isBinary() && !method.isReadOnly() && !JdtFlags.isStatic(method) && (JdtFlags.isDefaultMethod(method) || !method.getDeclaringType().isInterface());
}