Skip to content

Commit

Permalink
Bug 516335 - Rewrite CA with ImportRewrite
Browse files Browse the repository at this point in the history
* Getter/Setter suggestions
* Improved method overrides
* Magic method stubs
* Alternate method calls (defaults)

Signed-off-by: Alex Xu <ibazzi@qq.com>
Also-by: Dawid Pakuła <zulus@w3des.net>
  • Loading branch information
ibazzi authored and zulus committed May 19, 2020
1 parent c3072f7 commit a47630e
Show file tree
Hide file tree
Showing 166 changed files with 4,772 additions and 2,140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public static int hashCode(IBinding binding) {
}

/**
* Note: this method is for debugging and testing purposes only. There are tests
* whose pre-computed test results rely on the returned String's format.
* Note: this method is for debugging and testing purposes only. There are
* tests whose pre-computed test results rely on the returned String's
* format.
*
* @param binding
* the binding
Expand Down Expand Up @@ -107,8 +108,8 @@ public static ITypeBinding getTopLevelType(ITypeBinding type) {
* @param thrownException
* the type binding
*
* @return <code>true</code> if the passed type binding is a runtime exception;
* otherwise <code>false</code> is returned
* @return <code>true</code> if the passed type binding is a runtime
* exception; otherwise <code>false</code> is returned
*/
public static boolean isRuntimeException(ITypeBinding thrownException) {
if (thrownException == null || thrownException.isPrimitive() || thrownException.isArray()) {
Expand Down Expand Up @@ -145,10 +146,10 @@ public static IVariableBinding findFieldInType(ITypeBinding type, String fieldNa

/**
* Finds the field specified by <code>fieldName</code> in the type hierarchy
* denoted by the given type. Returns <code>null</code> if no such field exists.
* If the field is defined in more than one super type only the first match is
* returned. First the super class is examined and than the implemented
* interfaces.
* denoted by the given type. Returns <code>null</code> if no such field
* exists. If the field is defined in more than one super type only the
* first match is returned. First the super class is examined and than the
* implemented interfaces.
*
* @param type
* The type to search the field in
Expand Down Expand Up @@ -180,8 +181,8 @@ public static IVariableBinding findFieldInHierarchy(ITypeBinding type, String fi

/**
* Finds the method specified by <code>methodName<code> and </code>
* parameters</code> in the given <code>type</code>. Returns <code>null</code>
* if no such method exits.
* parameters</code> in the given <code>type</code>. Returns
* <code>null</code> if no such method exits.
*
* @param type
* The type to search the method in
Expand All @@ -194,9 +195,17 @@ public static IMethodBinding findMethodInType(ITypeBinding type, String methodNa
return null;
}
IMethodBinding[] methods = type.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (methodName.equalsIgnoreCase(methods[i].getName())) {
return methods[i];
for (IMethodBinding method : methods) {
if (methodName.equalsIgnoreCase(method.getName())) {
return method;
}

}

methods = type.getImportedMethods();
for (IMethodBinding method : methods) {
if (methodName.equalsIgnoreCase(method.getName())) {
return method;
}

}
Expand All @@ -205,10 +214,10 @@ public static IMethodBinding findMethodInType(ITypeBinding type, String methodNa

/**
* Finds the method specified by <code>methodName</code> and </code>
* parameters</code> in the type hierarchy denoted by the given type. Returns
* <code>null</code> if no such method exists. If the method is defined in more
* than one super type only the first match is returned. First the super class
* is examined and than the implemented interfaces.
* parameters</code> in the type hierarchy denoted by the given type.
* Returns <code>null</code> if no such method exists. If the method is
* defined in more than one super type only the first match is returned.
* First the super class is examined and than the implemented interfaces.
*
* @param type
* The type to search the method in
Expand All @@ -221,6 +230,7 @@ public static IMethodBinding findMethodInHierarchy(ITypeBinding type, String met
if (method != null) {
return method;
}

ITypeBinding superClass = type.getSuperclass();
if (superClass != null) {
method = findMethodInHierarchy(superClass, methodName);
Expand All @@ -240,10 +250,10 @@ public static IMethodBinding findMethodInHierarchy(ITypeBinding type, String met

/**
* Finds the method specified by <code>methodName</code> and </code>
* parameters</code> in the type hierarchy denoted by the given type. Returns
* <code>null</code> if no such method exists. If the method is defined in more
* than one super type only the first match is returned. First the super class
* is examined and than the implemented interfaces.
* parameters</code> in the type hierarchy denoted by the given type.
* Returns <code>null</code> if no such method exists. If the method is
* defined in more than one super type only the first match is returned.
* First the super class is examined and than the implemented interfaces.
*
* @param type
* The type to search the method in
Expand Down Expand Up @@ -320,8 +330,8 @@ else if (PHPFlags.isAbstract(curr.getModifiers())) {
* The type to search the method in
* @param method
* The specified method that would override the result
* @return the method binding of the method that is overridden by the specified
* <code>method<code>, or <code>null</code>
* @return the method binding of the method that is overridden by the
* specified <code>method<code>, or <code>null</code>
*/
public static IMethodBinding findOverriddenMethodInType(ITypeBinding type, IMethodBinding method) {
IMethodBinding[] methods = type.getDeclaredMethods();
Expand All @@ -334,11 +344,11 @@ public static IMethodBinding findOverriddenMethodInType(ITypeBinding type, IMeth
}

/**
* Finds a method in the hierarchy of <code>type</code> that is overridden by
* </code>binding</code>. Returns <code>null</code> if no such method exists. If
* the method is defined in more than one super type only the first match is
* returned. First the super class is examined and than the implemented
* interfaces.
* Finds a method in the hierarchy of <code>type</code> that is overridden
* by </code>binding</code>. Returns <code>null</code> if no such method
* exists. If the method is defined in more than one super type only the
* first match is returned. First the super class is examined and than the
* implemented interfaces.
*
* @param type
* The type to search the method in
Expand Down Expand Up @@ -386,16 +396,17 @@ public static IMethodBinding innerFindOverriddenMethodInHierarchy(ITypeBinding t
* @param overriding
* overriding method
* @param testVisibility
* If true the result is tested on visibility. Null is returned if
* the method is not visible.
* If true the result is tested on visibility. Null is returned
* if the method is not visible.
* @return the method binding representing the method
*/
public static IMethodBinding findOverriddenMethod(IMethodBinding overriding, boolean testVisibility) {
int modifiers = overriding.getModifiers();
if (testVisibility
&& (PHPFlags.isPrivate(modifiers) /*
* || PHPFlags.isStatic(modifiers) || overriding.isConstructor()
*/)) {
if (testVisibility && (PHPFlags
.isPrivate(modifiers) /*
* || PHPFlags.isStatic(modifiers) ||
* overriding.isConstructor()
*/)) {
return null;
}

Expand All @@ -406,7 +417,8 @@ public static IMethodBinding findOverriddenMethod(IMethodBinding overriding, boo
if (type.getSuperclass() != null) {
IMethodBinding res = findOverriddenMethodInHierarchy(type.getSuperclass(), overriding);
if (res != null && !PHPFlags.isPrivate(res.getModifiers())) {
if (!testVisibility || isVisibleInHierarchy(res/* , overriding.getDeclaringClass().getPackage() */)) {
if (!testVisibility || isVisibleInHierarchy(
res/* , overriding.getDeclaringClass().getPackage() */)) {
return res;
}
}
Expand All @@ -422,7 +434,8 @@ public static IMethodBinding findOverriddenMethod(IMethodBinding overriding, boo
return null;
}

public static boolean isVisibleInHierarchy(IMethodBinding member/* , IPackageBinding pack */) {
public static boolean isVisibleInHierarchy(
IMethodBinding member/* , IPackageBinding pack */) {
int otherflags = member.getModifiers();
ITypeBinding declaringType = member.getDeclaringClass();
if (PHPFlags.isPublic(otherflags) || PHPFlags.isProtected(otherflags)
Expand Down Expand Up @@ -466,11 +479,11 @@ private static void collectSuperTypes(ITypeBinding curr, Set<ITypeBinding> colle
* overriding method (m1)
* @param overridden
* overridden method (m2)
* @return <code>true</code> iff the method <code>m1</code> is a subsignature of
* the method <code>m2</code>. This is one of the requirements for m1 to
* override m2. Accessibility and return types are not taken into
* account. Note that subsignature is <em>not</em> symmetric! TODO - PHP
* handling (shalom)
* @return <code>true</code> iff the method <code>m1</code> is a
* subsignature of the method <code>m2</code>. This is one of the
* requirements for m1 to override m2. Accessibility and return
* types are not taken into account. Note that subsignature is
* <em>not</em> symmetric! TODO - PHP handling (shalom)
*/
public static boolean isSubsignature(IMethodBinding overriding, IMethodBinding overridden) {
// TODO: use IMethodBinding#isSubsignature(..) once it is tested and
Expand All @@ -483,8 +496,8 @@ public static boolean isSubsignature(IMethodBinding overriding, IMethodBinding o
}

/**
* Finds a type binding for a given fully qualified type in the hierarchy of a
* type. Returns <code>null</code> if no type binding is found.
* Finds a type binding for a given fully qualified type in the hierarchy of
* a type. Returns <code>null</code> if no type binding is found.
*
* @param hierarchyType
* the binding representing the hierarchy
Expand Down
Loading

0 comments on commit a47630e

Please sign in to comment.