use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class RefactoringAvailabilityTester method isIntroduceFactoryAvailable.
public static boolean isIntroduceFactoryAvailable(final JavaTextSelection selection) throws JavaModelException {
final IJavaElement[] elements = selection.resolveElementAtOffset();
if (elements.length == 1 && elements[0] instanceof IMethod)
return isIntroduceFactoryAvailable((IMethod) elements[0]);
// there's no IMethod for the default constructor
if (!Checks.isAvailable(selection.resolveEnclosingElement()))
return false;
ASTNode node = selection.resolveCoveringNode();
if (node == null) {
ASTNode[] selectedNodes = selection.resolveSelectedNodes();
if (selectedNodes != null && selectedNodes.length == 1) {
node = selectedNodes[0];
if (node == null)
return false;
} else {
return false;
}
}
if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
return true;
node = ASTNodes.getNormalizedNode(node);
if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY)
return true;
return false;
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class RefactoringAvailabilityTester method isReplaceInvocationsAvailable.
public static boolean isReplaceInvocationsAvailable(final JavaTextSelection selection) throws JavaModelException {
final IJavaElement[] elements = selection.resolveElementAtOffset();
if (elements.length != 1)
return false;
IJavaElement element = elements[0];
return (element instanceof IMethod) && isReplaceInvocationsAvailable(((IMethod) element));
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class MemberVisibilityAdjustor method computeOutgoingVisibilityThreshold.
/**
* Computes the visibility threshold for the referenced element.
*
* @param referencing the referencing element
* @param referenced the referenced element
* @param monitor the progress monitor to use
* @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
* @throws JavaModelException if the java elements could not be accessed
*/
private ModifierKeyword computeOutgoingVisibilityThreshold(final IJavaElement referencing, final IMember referenced, final IProgressMonitor monitor) throws JavaModelException {
Assert.isTrue(referencing instanceof ICompilationUnit || referencing instanceof IType || referencing instanceof IPackageFragment);
Assert.isTrue(referenced instanceof IType || referenced instanceof IField || referenced instanceof IMethod);
ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
try {
//$NON-NLS-1$
monitor.beginTask("", 1);
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
final int referencingType = referencing.getElementType();
final int referencedType = referenced.getElementType();
switch(referencedType) {
case IJavaElement.TYPE:
{
final IType typeReferenced = (IType) referenced;
switch(referencingType) {
case IJavaElement.COMPILATION_UNIT:
{
final ICompilationUnit unit = (ICompilationUnit) referencing;
final ICompilationUnit referencedUnit = typeReferenced.getCompilationUnit();
if (referencedUnit != null && referencedUnit.equals(unit))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
keyword = null;
break;
}
case IJavaElement.TYPE:
{
keyword = thresholdTypeToType((IType) referencing, typeReferenced, monitor);
break;
}
case IJavaElement.PACKAGE_FRAGMENT:
{
final IPackageFragment fragment = (IPackageFragment) referencing;
if (typeReferenced.getPackageFragment().equals(fragment))
keyword = null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
case IJavaElement.FIELD:
{
final IField fieldReferenced = (IField) referenced;
final ICompilationUnit referencedUnit = fieldReferenced.getCompilationUnit();
switch(referencingType) {
case IJavaElement.COMPILATION_UNIT:
{
final ICompilationUnit unit = (ICompilationUnit) referencing;
if (referencedUnit != null && referencedUnit.equals(unit))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
keyword = null;
break;
}
case IJavaElement.TYPE:
{
keyword = thresholdTypeToField((IType) referencing, fieldReferenced, monitor);
break;
}
case IJavaElement.PACKAGE_FRAGMENT:
{
final IPackageFragment fragment = (IPackageFragment) referencing;
if (fieldReferenced.getDeclaringType().getPackageFragment().equals(fragment))
keyword = null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
case IJavaElement.METHOD:
{
final IMethod methodReferenced = (IMethod) referenced;
final ICompilationUnit referencedUnit = methodReferenced.getCompilationUnit();
switch(referencingType) {
case IJavaElement.COMPILATION_UNIT:
{
final ICompilationUnit unit = (ICompilationUnit) referencing;
if (referencedUnit != null && referencedUnit.equals(unit))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.getParent().equals(unit.getParent()))
keyword = null;
break;
}
case IJavaElement.TYPE:
{
keyword = thresholdTypeToMethod((IType) referencing, methodReferenced, monitor);
break;
}
case IJavaElement.PACKAGE_FRAGMENT:
{
final IPackageFragment fragment = (IPackageFragment) referencing;
if (methodReferenced.getDeclaringType().getPackageFragment().equals(fragment))
keyword = null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
default:
Assert.isTrue(false);
}
} finally {
monitor.done();
}
return keyword;
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class MemberVisibilityAdjustor method getVisibilityThreshold.
/**
* Computes the visibility threshold for the referenced element.
*
* @param referencing the referencing element
* @param referenced the referenced element
* @param monitor the progress monitor to use
* @return the visibility keyword corresponding to the threshold, or <code>null</code> for default visibility
* @throws JavaModelException if the java elements could not be accessed
*/
public ModifierKeyword getVisibilityThreshold(final IJavaElement referencing, final IMember referenced, final IProgressMonitor monitor) throws JavaModelException {
Assert.isTrue(!(referencing instanceof IInitializer));
Assert.isTrue(!(referenced instanceof IInitializer));
ModifierKeyword keyword = ModifierKeyword.PUBLIC_KEYWORD;
try {
//$NON-NLS-1$
monitor.beginTask("", 1);
monitor.setTaskName(RefactoringCoreMessages.MemberVisibilityAdjustor_checking);
final int referencingType = referencing.getElementType();
final int referencedType = referenced.getElementType();
switch(referencedType) {
case IJavaElement.TYPE:
{
final IType typeReferenced = (IType) referenced;
final ICompilationUnit referencedUnit = typeReferenced.getCompilationUnit();
switch(referencingType) {
case IJavaElement.TYPE:
{
keyword = thresholdTypeToType((IType) referencing, typeReferenced, monitor);
break;
}
case IJavaElement.FIELD:
case IJavaElement.METHOD:
{
final IMember member = (IMember) referencing;
if (typeReferenced.equals(member.getDeclaringType()))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (typeReferenced.getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
keyword = null;
break;
}
case IJavaElement.PACKAGE_FRAGMENT:
{
final IPackageFragment fragment = (IPackageFragment) referencing;
if (typeReferenced.getPackageFragment().equals(fragment))
keyword = null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
case IJavaElement.FIELD:
{
final IField fieldReferenced = (IField) referenced;
final ICompilationUnit referencedUnit = fieldReferenced.getCompilationUnit();
switch(referencingType) {
case IJavaElement.TYPE:
{
keyword = thresholdTypeToField((IType) referencing, fieldReferenced, monitor);
break;
}
case IJavaElement.FIELD:
case IJavaElement.METHOD:
{
final IMember member = (IMember) referencing;
if (fieldReferenced.getDeclaringType().equals(member.getDeclaringType()))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (fieldReferenced.getDeclaringType().getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
keyword = null;
break;
}
case IJavaElement.PACKAGE_FRAGMENT:
{
final IPackageFragment fragment = (IPackageFragment) referencing;
if (fieldReferenced.getDeclaringType().getPackageFragment().equals(fragment))
keyword = null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
case IJavaElement.METHOD:
{
final IMethod methodReferenced = (IMethod) referenced;
final ICompilationUnit referencedUnit = methodReferenced.getCompilationUnit();
switch(referencingType) {
case IJavaElement.TYPE:
{
keyword = thresholdTypeToMethod((IType) referencing, methodReferenced, monitor);
break;
}
case IJavaElement.FIELD:
case IJavaElement.METHOD:
{
final IMember member = (IMember) referencing;
if (methodReferenced.getDeclaringType().equals(member.getDeclaringType()))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (referencedUnit != null && referencedUnit.equals(member.getCompilationUnit()))
keyword = ModifierKeyword.PRIVATE_KEYWORD;
else if (methodReferenced.getDeclaringType().getPackageFragment().equals(member.getDeclaringType().getPackageFragment()))
keyword = null;
break;
}
case IJavaElement.PACKAGE_FRAGMENT:
{
final IPackageFragment fragment = (IPackageFragment) referencing;
if (methodReferenced.getDeclaringType().getPackageFragment().equals(fragment))
keyword = null;
break;
}
default:
Assert.isTrue(false);
}
break;
}
default:
Assert.isTrue(false);
}
} finally {
monitor.done();
}
return keyword;
}
use of org.eclipse.jdt.core.IMethod in project che by eclipse.
the class GetterSetterCompletionProposal method evaluateProposals.
public static void evaluateProposals(IType type, String prefix, int offset, int length, int relevance, Set<String> suggestedMethods, Collection<IJavaCompletionProposal> result) throws CoreException {
if (prefix.length() == 0) {
relevance--;
}
IField[] fields = type.getFields();
IMethod[] methods = type.getMethods();
for (int i = 0; i < fields.length; i++) {
IField curr = fields[i];
if (!JdtFlags.isEnum(curr)) {
String getterName = GetterSetterUtil.getGetterName(curr, null);
if (Strings.startsWithIgnoreCase(getterName, prefix) && !hasMethod(methods, getterName)) {
suggestedMethods.add(getterName);
int getterRelevance = relevance;
if (JdtFlags.isStatic(curr) && JdtFlags.isFinal(curr))
getterRelevance = relevance - 1;
result.add(new GetterSetterCompletionProposal(curr, offset, length, true, getterRelevance));
}
if (!JdtFlags.isFinal(curr)) {
String setterName = GetterSetterUtil.getSetterName(curr, null);
if (Strings.startsWithIgnoreCase(setterName, prefix) && !hasMethod(methods, setterName)) {
suggestedMethods.add(setterName);
result.add(new GetterSetterCompletionProposal(curr, offset, length, false, relevance));
}
}
}
}
}
Aggregations