Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#isLocal()

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#isLocal() . 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: JdtUtils.java    From j2cl with Apache License 2.0 6 votes vote down vote up
private static List<String> getClassComponents(ITypeBinding typeBinding) {
  List<String> classComponents = new ArrayList<>();
  ITypeBinding currentType = typeBinding;
  while (currentType != null) {
    checkArgument(currentType.getTypeDeclaration() != null);
    if (currentType.isLocal()) {
      // JDT binary name for local class is like package.components.EnclosingClass$1SimpleName
      // Extract the generated name by taking the part after the binary name of the declaring
      // class.
      String binaryName = getBinaryNameFromTypeBinding(currentType);
      String declaringClassPrefix =
          getBinaryNameFromTypeBinding(currentType.getDeclaringClass()) + "$";
      checkState(binaryName.startsWith(declaringClassPrefix));
      classComponents.add(0, binaryName.substring(declaringClassPrefix.length()));
    } else {
      classComponents.add(0, currentType.getName());
    }
    currentType = currentType.getDeclaringClass();
  }
  return classComponents;
}
 
Example 2
Source File: JdtUtils.java    From j2cl with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the binary name for a type binding.
 *
 * <p>NOTE: This accounts for the cases that JDT does not assign binary names, which are those of
 * unreachable local or anonymous classes.
 */
private static String getBinaryNameFromTypeBinding(ITypeBinding typeBinding) {
  String binaryName = typeBinding.getBinaryName();
  if (binaryName == null && (typeBinding.isLocal() || typeBinding.isAnonymous())) {
    // Local and anonymous classes in unreachable code have null binary name.

    // The code here is a HACK that relies on the way that JDT synthesizes keys. Keys for
    // unreachable classes have the closest enclosing reachable class key as a prefix (minus the
    // ending semicolon)
    ITypeBinding closestReachableExclosingClass = typeBinding.getDeclaringClass();
    while (closestReachableExclosingClass.getBinaryName() == null) {
      closestReachableExclosingClass = closestReachableExclosingClass.getDeclaringClass();
    }
    String parentKey = closestReachableExclosingClass.getKey();
    String key = typeBinding.getKey();
    return getBinaryNameFromTypeBinding(typeBinding.getDeclaringClass())
        + "$$Unreachable"
        // remove the parent prefix and the ending semicolon
        + key.substring(parentKey.length() - 1, key.length() - 1);
  }
  return binaryName;
}
 
Example 3
Source File: JdtUtils.java    From j2cl with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if instances of this type capture its outer instances; i.e. if it is an non static
 * member class, or an anonymous or local class defined in an instance context.
 */
public static boolean capturesEnclosingInstance(ITypeBinding typeBinding) {
  if (!typeBinding.isClass() || !typeBinding.isNested()) {
    // Only non-top level classes (excludes Enums, Interfaces etc.) can capture outer instances.
    return false;
  }

  if (typeBinding.isLocal()) {
    // Local types (which include anonymous classes in JDT) are static only if they are declared
    // in a static context; i.e. if the member where they are declared is static.
    return !isStatic(getDeclaringMethodOrFieldBinding(typeBinding));
  } else {
    checkArgument(typeBinding.isMember());
    // Member classes must be marked explicitly static.
    return !isStatic(typeBinding);
  }
}
 
Example 4
Source File: TargetProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static TargetProvider create(MethodDeclaration declaration) {
	IMethodBinding method= declaration.resolveBinding();
	if (method == null)
		return new ErrorTargetProvider(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.TargetProvider_method_declaration_not_unique));
	ITypeBinding type= method.getDeclaringClass();
	if (type.isLocal()) {
		if (((IType) type.getJavaElement()).isBinary()) {
			return new ErrorTargetProvider(RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.TargetProvider_cannot_local_method_in_binary));
		} else {
			IType declaringClassOfLocal= (IType) type.getDeclaringClass().getJavaElement();
			return new LocalTypeTargetProvider(declaringClassOfLocal.getCompilationUnit(), declaration);
		}
	} else {
		return new MemberTypeTargetProvider(declaration.resolveBinding());
	}
}
 
Example 5
Source File: SourceProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void updateTypeReferences(ASTRewrite rewriter, CallContext context) {
	ImportRewrite importer= context.importer;
	for (Iterator<SimpleName> iter= fAnalyzer.getTypesToImport().iterator(); iter.hasNext();) {
		Name element= iter.next();
		ITypeBinding binding= ASTNodes.getTypeBinding(element);
		if (binding != null && !binding.isLocal()) {
			// We have collected names not types. So we have to import
			// the declaration type if we reference a parameterized type
			// since we have an entry for every name node (e.g. one for
			// Vector and one for Integer in Vector<Integer>.
			if (binding.isParameterizedType()) {
				binding= binding.getTypeDeclaration();
			}
			String s= importer.addImport(binding);
			if (!ASTNodes.asString(element).equals(s)) {
				rewriter.replace(element, rewriter.createStringPlaceholder(s, ASTNode.SIMPLE_NAME), null);
			}
		}
	}
}
 
Example 6
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	ITypeBinding typeBinding = node.resolveTypeBinding();
	if (typeBinding != null && typeBinding.isLocal()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(typeBinding);
		} else if (!fLocalDefinitions.contains(typeBinding)) {
			fLocalReferencesToEnclosing.add(node);
		}
	}
	if (typeBinding != null && typeBinding.isTypeVariable()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(typeBinding);
		} else if (!fLocalDefinitions.contains(typeBinding)) {
			if (fMethodTypeVariables.contains(typeBinding)) {
				fLocalReferencesToEnclosing.add(node);
			} else {
				fClassTypeVariablesUsed = true;
			}
		}
	}
	IBinding binding = node.resolveBinding();
	if (binding != null && binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(binding);
		} else if (!fLocalDefinitions.contains(binding)) {
			fLocalReferencesToEnclosing.add(node);
		}
	}
	return super.visit(node);
}
 
Example 7
Source File: PromoteTempToFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
	ITypeBinding typeBinding= node.resolveTypeBinding();
	if (typeBinding != null && typeBinding.isLocal()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(typeBinding);
		} else if (! fLocalDefinitions.contains(typeBinding)) {
			fLocalReferencesToEnclosing.add(node);
		}
	}
	if (typeBinding != null && typeBinding.isTypeVariable()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(typeBinding);
		} else if (! fLocalDefinitions.contains(typeBinding)) {
			if (fMethodTypeVariables.contains(typeBinding)) {
				fLocalReferencesToEnclosing.add(node);
			} else {
				fClassTypeVariablesUsed= true;
			}
		}
	}
	IBinding binding= node.resolveBinding();
	if (binding != null && binding.getKind() == IBinding.VARIABLE && ! ((IVariableBinding)binding).isField()) {
		if (node.isDeclaration()) {
			fLocalDefinitions.add(binding);
		} else if (! fLocalDefinitions.contains(binding)) {
			fLocalReferencesToEnclosing.add(node);
		}
	}
	return super.visit(node);
}
 
Example 8
Source File: TType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Initialized the type from the given binding
 *
 * @param binding the binding to initialize from
 */
protected void initialize(ITypeBinding binding) {
	fBindingKey= binding.getKey();
	Assert.isNotNull(fBindingKey);
	fModifiers= binding.getModifiers();
	if (binding.isClass()) {
		fFlags= F_IS_CLASS;
	// the annotation test has to be done before test for interface
	// since annotations are interfaces as well.
	} else if (binding.isAnnotation()) {
		fFlags= F_IS_ANNOTATION | F_IS_INTERFACE;
	} else if (binding.isInterface()) {
		fFlags= F_IS_INTERFACE;
	} else if (binding.isEnum()) {
		fFlags= F_IS_ENUM;
	}

	if (binding.isTopLevel()) {
		fFlags|= F_IS_TOP_LEVEL;
	} else if (binding.isNested()) {
		fFlags|= F_IS_NESTED;
		if (binding.isMember()) {
			fFlags|= F_IS_MEMBER;
		} else if (binding.isLocal()) {
			fFlags|= F_IS_LOCAL;
		} else if (binding.isAnonymous()) {
			fFlags|= F_IS_ANONYMOUS;
		}
	}
}
 
Example 9
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Collects all elements available in a type: its hierarchy and its outer scopes.
 * @param binding The type binding
 * @param flags Flags defining the elements to report
 * @param requestor the requestor to which all results are reported
 * @return return <code>true</code> if the requestor has reported the binding as found and no further results are required
 */
private boolean addTypeDeclarations(ITypeBinding binding, int flags, IBindingRequestor requestor) {
	if (hasFlag(TYPES, flags) && !binding.isAnonymous()) {
		if (requestor.acceptBinding(binding))
			return true;

		ITypeBinding[] typeParameters= binding.getTypeParameters();
		for (int i= 0; i < typeParameters.length; i++) {
			if (requestor.acceptBinding(typeParameters[i]))
				return true;
		}
	}

	addInherited(binding, flags, requestor); // add inherited

	if (binding.isLocal()) {
		addOuterDeclarationsForLocalType(binding, flags, requestor);
	} else {
		ITypeBinding declaringClass= binding.getDeclaringClass();
		if (declaringClass != null) {
			if (addTypeDeclarations(declaringClass, flags, requestor)) // Recursively add inherited
				return true;
		} else if (hasFlag(TYPES, flags)) {
			if (fRoot.findDeclaringNode(binding) != null) {
				List<AbstractTypeDeclaration> types= fRoot.types();
				for (int i= 0; i < types.size(); i++) {
					if (requestor.acceptBinding(types.get(i).resolveBinding()))
						return true;
				}
			}
		}
	}
	return false;
}
 
Example 10
Source File: NullAnnotationsRewriteOperations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
boolean hasNonNullDefault(IBinding enclosingElement) {
	if (!fRemoveIfNonNullByDefault) return false;
	IAnnotationBinding[] annotations = enclosingElement.getAnnotations();
	for (int i= 0; i < annotations.length; i++) {
		IAnnotationBinding annot = annotations[i];
		if (annot.getAnnotationType().getQualifiedName().equals(fNonNullByDefaultName)) {
			IMemberValuePairBinding[] pairs= annot.getDeclaredMemberValuePairs();
			if (pairs.length > 0) {
				// is default cancelled by "false" or "value=false" ?
				for (int j= 0; j < pairs.length; j++)
					if (pairs[j].getKey() == null || pairs[j].getKey().equals("value")) //$NON-NLS-1$
						return (pairs[j].getValue() != Boolean.FALSE);
			}
			return true;
		}
	}
	if (enclosingElement instanceof IMethodBinding) {
		return hasNonNullDefault(((IMethodBinding)enclosingElement).getDeclaringClass());
	} else if (enclosingElement instanceof ITypeBinding) {
		ITypeBinding typeBinding= (ITypeBinding)enclosingElement;
		if (typeBinding.isLocal())
			return hasNonNullDefault(typeBinding.getDeclaringMethod());
		else if (typeBinding.isMember())
			return hasNonNullDefault(typeBinding.getDeclaringClass());
		else
			return hasNonNullDefault(typeBinding.getPackage());
	}
	return false;
}
 
Example 11
Source File: JdtUtils.java    From j2cl with Apache License 2.0 4 votes vote down vote up
private static boolean isLocal(ITypeBinding typeBinding) {
  return typeBinding.isLocal();
}