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

The following examples show how to use org.eclipse.jdt.core.IMethod#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: GroovyDocumentUtil.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("restriction")
public static List<IMethod> getModuleMethods(GroovyCompilationUnit unit) {
    List<IMethod> methods = new ArrayList<>();

    try {
        IType[] types = unit.getAllTypes();
        for (IType t : types) {
            for (IMethod m : t.getMethods()) {
                if (!m.isBinary() && !m.isMainMethod() && !m.isConstructor() && !m.getElementName().equals("run")) {
                    methods.add(m);
                }
            }
        }

    } catch (JavaModelException e) {
        BonitaStudioLog.error(e);
    }

    return methods;

}
 
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 isPullUpAvailable(IMember member) throws JavaModelException {
	if (!member.exists())
		return false;
	final int type= member.getElementType();
	if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE)
		return false;
	if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE)
		return false;
	if (!Checks.isAvailable(member))
		return false;
	if (member instanceof IType) {
		if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member) && !JdtFlags.isAnnotation(member))
			return false;
	}
	if (member instanceof IMethod) {
		final IMethod method= (IMethod) member;
		if (method.isConstructor())
			return false;
		if (JdtFlags.isNative(method))
			return false;
		final IType declaring= method.getDeclaringType();
		if (declaring != null && declaring.isAnnotation())
			return false;
	}
	return true;
}
 
Example 3
Source File: JavaModelUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Tests if a method equals to the given signature.
 * Parameter types are only compared by the simple name, no resolving for
 * the fully qualified type name is done. Constructors are only compared by
 * parameters, not the name.
 * @param name Name of the method
 * @param paramTypes The type signatures of the parameters e.g. <code>{"QString;","I"}</code>
 * @param isConstructor Specifies if the method is a constructor
 * @param curr the method
 * @return Returns <code>true</code> if the method has the given name and parameter types and constructor state.
 * @throws JavaModelException thrown when the method can not be accessed
 */
public static boolean isSameMethodSignature(String name, String[] paramTypes, boolean isConstructor, IMethod curr) throws JavaModelException {
	if (isConstructor || name.equals(curr.getElementName())) {
		if (isConstructor == curr.isConstructor()) {
			String[] currParamTypes= curr.getParameterTypes();
			if (paramTypes.length == currParamTypes.length) {
				for (int i= 0; i < paramTypes.length; i++) {
					String t1= Signature.getSimpleName(Signature.toString(paramTypes[i]));
					String t2= Signature.getSimpleName(Signature.toString(currParamTypes[i]));
					if (!t1.equals(t2)) {
						return false;
					}
				}
				return true;
			}
		}
	}
	return false;
}
 
Example 4
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 5
Source File: TypeHierarchyContentProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private boolean hasCompatibleMethod(IMethod filterMethod, IType typeToFindIn) throws JavaModelException {
	int flags= filterMethod.getFlags();
	if (Flags.isPrivate(flags) || Flags.isStatic(flags) || filterMethod.isConstructor())
		return false;
	synchronized (fTypeHierarchyLifeCycleListener) {
		boolean filterMethodOverrides= initializeMethodOverrideTester(filterMethod, typeToFindIn);
		IMethod[] methods= typeToFindIn.getMethods();
		for (int i= 0; i < methods.length; i++) {
			IMethod curr= methods[i];
			flags= curr.getFlags();
			if (Flags.isPrivate(flags) || Flags.isStatic(flags) || curr.isConstructor())
				continue;
			if (isCompatibleMethod(filterMethod, curr, filterMethodOverrides)) {
				return true;
			}
		}
		return false;
	}
}
 
Example 6
Source File: MethodOverrideTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Finds an overridden method in a type. With generics it is possible that 2 methods in the same type are overridden at the same time.
 * In that case the first overridden method found is returned.
 * @param overriddenType The type to find methods in
 * @param overriding The overriding method
 * @return The first overridden method or <code>null</code> if no method is overridden
 * @throws JavaModelException if a problem occurs
 */
public IMethod findOverriddenMethodInType(IType overriddenType, IMethod overriding) throws JavaModelException {
	int flags= overriding.getFlags();
	if (Flags.isPrivate(flags) || Flags.isStatic(flags) || overriding.isConstructor())
		return null;
	IMethod[] overriddenMethods= overriddenType.getMethods();
	for (int i= 0; i < overriddenMethods.length; i++) {
		IMethod overridden= overriddenMethods[i];
		flags= overridden.getFlags();
		if (Flags.isPrivate(flags) || Flags.isStatic(flags) || overridden.isConstructor())
			continue;
		if (isSubsignature(overriding, overridden)) {
			return overridden;
		}
	}
	return null;
}
 
Example 7
Source File: AbstractHierarchyViewerSorter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private IType getDefiningType(IMethod method) throws JavaModelException {
	int flags= method.getFlags();
	if (Flags.isPrivate(flags) || Flags.isStatic(flags) || method.isConstructor()) {
		return null;
	}

	IType declaringType= method.getDeclaringType();
	ITypeHierarchy hierarchy= getHierarchy(declaringType);
	if (hierarchy != null) {
		MethodOverrideTester tester= new MethodOverrideTester(declaringType, hierarchy);
		IMethod res= tester.findDeclaringMethod(method, true);
		if (res != null) {
			return res.getDeclaringType();
		}
	}
	return null;
}
 
Example 8
Source File: RefactoringAvailabilityTester.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean isInlineMethodAvailable(IMethod method) throws JavaModelException {
	if (method == null) {
		return false;
	}
	if (!method.exists()) {
		return false;
	}
	if (!method.isStructureKnown()) {
		return false;
	}
	if (!method.isBinary()) {
		return true;
	}
	if (method.isConstructor()) {
		return false;
	}
	return SourceRange.isAvailable(method.getNameRange());
}
 
Example 9
Source File: JavaElementHyperlinkSuperImplementationDetector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Indicates whether a method is overridden.
 * 
 * @param method the method to check
 * @return <code>true</code> if the method is overridden, <code>false</code> otherwise
 */
private boolean isOverriddenMethod(IMethod method) {
	try {
		if (JdtFlags.isPrivate(method) || JdtFlags.isStatic(method) || method.isConstructor())
			return false;
		if (JavaElementSuperImplementationHyperlink.findSuperImplementation(method) != null)
			return true;
	} catch (JavaModelException e) {
		JavaPlugin.log(e);
	}
	return false;
}
 
Example 10
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 11
Source File: MethodProposalInfo.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests if a method equals to the given signature. Parameter types are only
 * compared by the simple name, no resolving for the fully qualified type
 * name is done. Constructors are only compared by parameters, not the name.
 *
 * @param name Name of the method
 * @param paramTypes The type signatures of the parameters e.g.
 *        <code>{"QString;","I"}</code>
 * @param isConstructor Specifies if the method is a constructor
 * @param method the method to be compared with this info's method
 * @param typeVariables a map from type variables to types
 * @param type the given type that declares the method
 * @return Returns <code>true</code> if the method has the given name and
 *         parameter types and constructor state.
 * @throws JavaModelException if the method does not exist or if an exception occurs while accessing its corresponding resource
 */
private boolean isSameMethodSignature(String name, String[] paramTypes, boolean isConstructor, IMethod method, Map<String, char[]> typeVariables, IType type) throws JavaModelException {
	if (isConstructor || name.equals(method.getElementName())) {
		if (isConstructor == method.isConstructor()) {
			String[] otherParams= method.getParameterTypes(); // types may be type variables
			boolean isBinaryConstructorForNonStaticMemberClass=
					method.isBinary()
					&& type.isMember()
					&& !Flags.isStatic(type.getFlags());
			int syntheticParameterCorrection= isBinaryConstructorForNonStaticMemberClass && paramTypes.length == otherParams.length - 1 ? 1 : 0;
			if (paramTypes.length == otherParams.length - syntheticParameterCorrection) {
				fFallbackMatch= method;
				String signature= method.getSignature();
				String[] otherParamsFromSignature= Signature.getParameterTypes(signature); // types are resolved / upper-bounded
				// no need to check method type variables since these are
				// not yet bound when proposing a method
				for (int i= 0; i < paramTypes.length; i++) {
					String ourParamName= computeSimpleTypeName(paramTypes[i], typeVariables);
					String otherParamName1= computeSimpleTypeName(otherParams[i + syntheticParameterCorrection], typeVariables);
					String otherParamName2= computeSimpleTypeName(otherParamsFromSignature[i + syntheticParameterCorrection], typeVariables);

					if (!ourParamName.equals(otherParamName1) && !ourParamName.equals(otherParamName2)) {
						return false;
					}
				}
				return true;
			}
		}
	}
	return false;
}
 
Example 12
Source File: MethodChecks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns <code>true</code> iff the method could be a virtual method,
 * i.e. if it is not a constructor, is private, or is static.
 *
 * @param method a method
 * @return <code>true</code> iff the method could a virtual method
 * @throws JavaModelException
 */
public static boolean isVirtual(IMethod method) throws JavaModelException {
	if (method.isConstructor())
		return false;
	if (JdtFlags.isPrivate(method))
		return false;
	if (JdtFlags.isStatic(method))
		return false;
	return true;
}
 
Example 13
Source File: JavaElementUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IMethod[] getAllConstructors(IType type) throws JavaModelException {
	if (JavaModelUtil.isInterfaceOrAnnotation(type))
		return new IMethod[0];
	List<IMethod> result= new ArrayList<IMethod>();
	IMethod[] methods= type.getMethods();
	for (int i= 0; i < methods.length; i++) {
		IMethod iMethod= methods[i];
		if (iMethod.isConstructor())
			result.add(iMethod);
	}
	return result.toArray(new IMethod[result.size()]);
}
 
Example 14
Source File: RenameSupport.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static RenameSupport create(IJavaElement element, String newName, int flags) throws CoreException {
	switch (element.getElementType()) {
		case IJavaElement.JAVA_PROJECT:
			//return RenameSupport.create((IJavaProject) element, newName, flags);
			return null;
		case IJavaElement.PACKAGE_FRAGMENT_ROOT:
			//return RenameSupport.create((IPackageFragmentRoot) element, newName);
			return null;
		case IJavaElement.PACKAGE_FRAGMENT:
			return RenameSupport.create((IPackageFragment) element, newName, flags);
		case IJavaElement.COMPILATION_UNIT:
			return RenameSupport.create((ICompilationUnit) element, newName, flags);
		case IJavaElement.TYPE:
			return RenameSupport.create((IType) element, newName, flags);
		case IJavaElement.METHOD:
			final IMethod method = (IMethod) element;
			if (method.isConstructor()) {
				return create(method.getDeclaringType(), newName, flags);
			} else {
				return RenameSupport.create((IMethod) element, newName, flags);
			}
		case IJavaElement.FIELD:
			return RenameSupport.create((IField) element, newName, flags);
		case IJavaElement.TYPE_PARAMETER:
			return RenameSupport.create((ITypeParameter) element, newName, flags);
		case IJavaElement.LOCAL_VARIABLE:
			return RenameSupport.create((ILocalVariable) element, newName, flags);
	}
	return null;
}
 
Example 15
Source File: RefactoringAvailabilityTester.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.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 16
Source File: XbaseHyperLinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("restriction")
protected boolean canBeOverridden(IMethod method) {
	try {
		return !(org.eclipse.jdt.internal.corext.util.JdtFlags.isPrivate(method) 
				|| org.eclipse.jdt.internal.corext.util.JdtFlags.isStatic(method) 
				|| org.eclipse.jdt.internal.corext.util.JdtFlags.isFinal(method) 
				|| org.eclipse.jdt.internal.corext.util.JdtFlags.isFinal(method.getDeclaringType()) 
				|| method.isConstructor());
	} catch (JavaModelException e) {
		org.eclipse.jdt.internal.ui.JavaPlugin.log(e);
		return false;
	}
}
 
Example 17
Source File: CallerFinder.java    From lapse-plus with GNU General Public License v3.0 5 votes vote down vote up
public static Collection/*<MethodUnitPair>*/ findCallers(IProgressMonitor progressMonitor, IMethod member, IJavaProject project) {
      boolean isConstructor = false;
      try {
          isConstructor = member.isConstructor(); // interrogate the member itself
      } catch (JavaModelException e) {
          JavaPlugin.log(e);
      }
      String methodName = isConstructor ?
          member.getDeclaringType().getElementName() :
          member.getDeclaringType().getElementName() + "." + member.getElementName();
return findCallers(progressMonitor, methodName, project, isConstructor);
  }
 
Example 18
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected void appendMembers(final IType type, final IProgressMonitor monitor) throws JavaModelException {
	try {
		monitor.beginTask(RefactoringCoreMessages.StubCreationOperation_creating_type_stubs, 1);
		final IJavaElement[] children= type.getChildren();
		for (int index= 0; index < children.length; index++) {
			final IMember child= (IMember) children[index];
			final int flags= child.getFlags();
			final boolean isPrivate= Flags.isPrivate(flags);
			final boolean isDefault= !Flags.isPublic(flags) && !Flags.isProtected(flags) && !isPrivate;
			final boolean stub= fStubInvisible || (!isPrivate && !isDefault);
			if (child instanceof IType) {
				if (stub)
					appendTypeDeclaration((IType) child, new SubProgressMonitor(monitor, 1));
			} else if (child instanceof IField) {
				if (stub && !Flags.isEnum(flags) && !Flags.isSynthetic(flags))
					appendFieldDeclaration((IField) child);
			} else if (child instanceof IMethod) {
				final IMethod method= (IMethod) child;
				final String name= method.getElementName();
				if (method.getDeclaringType().isEnum()) {
					final int count= method.getNumberOfParameters();
					if (count == 0 && "values".equals(name)) //$NON-NLS-1$
						continue;
					if (count == 1 && "valueOf".equals(name) && "Ljava.lang.String;".equals(method.getParameterTypes()[0])) //$NON-NLS-1$ //$NON-NLS-2$
						continue;
					if (method.isConstructor())
						continue;
				}
				boolean skip= !stub || name.equals("<clinit>"); //$NON-NLS-1$
				if (method.isConstructor())
					skip= false;
				skip= skip || Flags.isSynthetic(flags) || Flags.isBridge(flags);
				if (!skip)
					appendMethodDeclaration(method);
			}
			fBuffer.append("\n"); //$NON-NLS-1$
		}
	} finally {
		monitor.done();
	}
}
 
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: JavaModelSearch.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
private static boolean methodSignaturesEqual(IType type2, String methodName,
    String[] paramTypes, boolean isConstructor, IMethod method2) {
  try {
    // Method names must match, unless we're comparing constructors
    if (!isConstructor && !method2.getElementName().equals(methodName)) {
      return false;
    }

    // Only compare ctors to ctors and methods to methods
    if (isConstructor != method2.isConstructor()) {
      return false;
    }

    // Parameter count must match
    String signature2 = method2.getSignature();
    String[] paramTypes2 = Signature.getParameterTypes(signature2);
    if (paramTypes.length != paramTypes2.length) {
      return false;
    }

    // Compare each parameter type
    for (int i = 0; i < paramTypes.length; i++) {
      String paramType = paramTypes[i];
      String paramType2 = paramTypes2[i];

      // Compare array nesting depth ([] = 1, [][] = 2, etc.)
      if (Signature.getArrayCount(paramType) != Signature.getArrayCount(paramType2)) {
        return false;
      }

      // Remove any array nesting and generic type parameters
      paramType = getBaseType(paramType);
      paramType2 = getBaseType(paramType2);

      // Extract the full type names from the signatures
      String paramTypeName = getQualifiedTypeName(paramType);
      String paramTypeName2 = getQualifiedTypeName(paramType2);

      if (isTypeParameter(method2, paramTypeName2)) {
        // Skip parameters whose type is a generic type parameter of the
        // method we're comparing against, or the method's containing class
        continue;

        /*
         * TODO: we're currently not checking the bounds of generic type
         * parameters, so sometimes we may return true here even when the
         * caller's method signature doesn't match the method we're comparing
         * against. We could try to add that logic here, or better still, we
         * could integrate TypeOracle and take advantage of its type searching
         * capabilities.
         */
      }

      // If we run into an unresolved parameter type in the method we're
      // searching, we'll need to resolve that before doing the comparison
      if (paramType2.charAt(0) == Signature.C_UNRESOLVED) {
        paramTypeName2 = resolveTypeName(type2, paramTypeName2);
      }

      // Finally, compare the type names
      if (!paramTypeName.equals(paramTypeName2)) {
        return false;
      }
    }

    // We passed all the checks, so the signatures are equal
    return true;

  } catch (JavaModelException e) {
    CorePluginLog.logError(e,
        "Error comparing method signatures of {0} and {1}", methodName,
        method2.getElementName());
    return false;
  }
}