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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#isNested() . 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
/**
 * 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 2
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void initializeDestinations() {
	List<ASTNode> result = new ArrayList<>();
	BodyDeclaration decl = fAnalyzer.getEnclosingBodyDeclaration();
	ASTNode current = ASTResolving.findParentType(decl.getParent());
	if (fAnalyzer.isValidDestination(current)) {
		result.add(current);
	}
	if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
		ITypeBinding binding = ASTNodes.getEnclosingType(current);
		ASTNode next = ASTResolving.findParentType(current.getParent());
		while (next != null && binding != null && binding.isNested()) {
			if (fAnalyzer.isValidDestination(next)) {
				result.add(next);
			}
			current = next;
			binding = ASTNodes.getEnclosingType(current);
			next = ASTResolving.findParentType(next.getParent());
		}
	}
	fDestinations = result.toArray(new ASTNode[result.size()]);
	fDestination = fDestinations[fDestinationIndex];
}
 
Example 3
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initializeDestinations() {
	List<ASTNode> result= new ArrayList<ASTNode>();
	BodyDeclaration decl= fAnalyzer.getEnclosingBodyDeclaration();
	ASTNode current= ASTResolving.findParentType(decl.getParent());
	if (fAnalyzer.isValidDestination(current)) {
		result.add(current);
	}
	if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
		ITypeBinding binding= ASTNodes.getEnclosingType(current);
		ASTNode next= ASTResolving.findParentType(current.getParent());
		while (next != null && binding != null && binding.isNested()) {
			if (fAnalyzer.isValidDestination(next)) {
				result.add(next);
			}
			current= next;
			binding= ASTNodes.getEnclosingType(current);
			next= ASTResolving.findParentType(next.getParent());
		}
	}
	fDestinations= result.toArray(new ASTNode[result.size()]);
	fDestination= fDestinations[fDestinationIndex];
}
 
Example 4
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 6 votes vote down vote up
private void addRequiredImportDeclarationsToContext() {
	ImportRewrite sourceImportRewrite = ImportRewrite.create(sourceCompilationUnit, true);
	for(ITypeBinding typeBinding : requiredImportDeclarationsForContext) {
		if(!typeBinding.isNested())
			sourceImportRewrite.addImport(typeBinding);
	}

	try {
		TextEdit sourceImportEdit = sourceImportRewrite.rewriteImports(null);
		if(sourceImportRewrite.getCreatedImports().length > 0) {
			ICompilationUnit sourceICompilationUnit = (ICompilationUnit)sourceCompilationUnit.getJavaElement();
			CompilationUnitChange change = compilationUnitChanges.get(sourceICompilationUnit);
			change.getEdit().addChild(sourceImportEdit);
			change.addTextEditGroup(new TextEditGroup("Add required import declarations", new TextEdit[] {sourceImportEdit}));
		}
	} catch (CoreException e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: ReferenceResolvingVisitor.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
private String getQualifiedName(ITypeBinding typeBinding)
{
    if (typeBinding == null)
        return null;

    String qualifiedName = typeBinding.getQualifiedName();

    if (StringUtils.isEmpty(qualifiedName))
    {
        if (typeBinding.isAnonymous())
        {
            if (typeBinding.getSuperclass() != null)
                qualifiedName = getQualifiedName(typeBinding.getSuperclass());
            else if (typeBinding instanceof AnonymousClassDeclaration)
            {
                qualifiedName = ((AnonymousClassDeclaration) typeBinding).toString();
            }
        }
        else if (StringUtils.isEmpty(qualifiedName) && typeBinding.isNested())
            qualifiedName = typeBinding.getName();
    }

    return qualifiedName;
}
 
Example 6
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void addNewFieldProposals(ICompilationUnit cu, CompilationUnit astRoot, ITypeBinding binding,
		ITypeBinding declaringTypeBinding, SimpleName simpleName, boolean isWriteAccess,
		Collection<ChangeCorrectionProposal> proposals) throws JavaModelException {
	// new variables
	ICompilationUnit targetCU;
	ITypeBinding senderDeclBinding;
	if (binding != null) {
		senderDeclBinding= binding.getTypeDeclaration();
		targetCU= ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding);
	} else { // binding is null for accesses without qualifier
		senderDeclBinding= declaringTypeBinding;
		targetCU= cu;
	}

	if (!senderDeclBinding.isFromSource() || targetCU == null) {
		return;
	}

	boolean mustBeConst= ASTResolving.isInsideModifiers(simpleName);

	addNewFieldForType(targetCU, binding, senderDeclBinding, simpleName, isWriteAccess, mustBeConst, proposals);

	if (binding == null && senderDeclBinding.isNested()) {
		ASTNode anonymDecl= astRoot.findDeclaringNode(senderDeclBinding);
		if (anonymDecl != null) {
			ITypeBinding bind= Bindings.getBindingOfParentType(anonymDecl.getParent());
			if (!bind.isAnonymous()) {
				addNewFieldForType(targetCU, bind, bind, simpleName, isWriteAccess, mustBeConst, proposals);
			}
		}
	}
}
 
Example 7
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 8
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void addNewFieldProposals(ICompilationUnit cu, CompilationUnit astRoot, ITypeBinding binding, ITypeBinding declaringTypeBinding, SimpleName simpleName, boolean isWriteAccess, Collection<ICommandAccess> proposals) throws JavaModelException {
	// new variables
	ICompilationUnit targetCU;
	ITypeBinding senderDeclBinding;
	if (binding != null) {
		senderDeclBinding= binding.getTypeDeclaration();
		targetCU= ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding);
	} else { // binding is null for accesses without qualifier
		senderDeclBinding= declaringTypeBinding;
		targetCU= cu;
	}

	if (!senderDeclBinding.isFromSource() || targetCU == null) {
		return;
	}

	boolean mustBeConst= ASTResolving.isInsideModifiers(simpleName);

	addNewFieldForType(targetCU, binding, senderDeclBinding, simpleName, isWriteAccess, mustBeConst, proposals);

	if (binding == null && senderDeclBinding.isNested()) {
		ASTNode anonymDecl= astRoot.findDeclaringNode(senderDeclBinding);
		if (anonymDecl != null) {
			ITypeBinding bind= Bindings.getBindingOfParentType(anonymDecl.getParent());
			if (!bind.isAnonymous()) {
				addNewFieldForType(targetCU, bind, bind, simpleName, isWriteAccess, mustBeConst, proposals);
			}
		}
	}
}
 
Example 9
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static void addNewMethodProposals(ICompilationUnit cu, CompilationUnit astRoot, Expression sender,
		List<Expression> arguments, boolean isSuperInvocation, ASTNode invocationNode, String methodName,
		Collection<ChangeCorrectionProposal> proposals) throws JavaModelException {
	ITypeBinding nodeParentType= Bindings.getBindingOfParentType(invocationNode);
	ITypeBinding binding= null;
	if (sender != null) {
		binding= sender.resolveTypeBinding();
	} else {
		binding= nodeParentType;
		if (isSuperInvocation && binding != null) {
			binding= binding.getSuperclass();
		}
	}
	if (binding != null && binding.isFromSource()) {
		ITypeBinding senderDeclBinding= binding.getTypeDeclaration();

		ICompilationUnit targetCU= ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding);
		if (targetCU != null) {
			String label;
			ITypeBinding[] parameterTypes= getParameterTypes(arguments);
			if (parameterTypes != null) {
				String sig = org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(methodName, parameterTypes, false);
				boolean is18OrHigher= JavaModelUtil.is18OrHigher(targetCU.getJavaProject());

				boolean isSenderBindingInterface= senderDeclBinding.isInterface();
				if (nodeParentType == senderDeclBinding) {
					label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_description, sig);
				} else {
					label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, new Object[] { sig, BasicElementLabels.getJavaElementName(senderDeclBinding.getName()) } );
				}
				if (is18OrHigher || !isSenderBindingInterface
						|| (nodeParentType != senderDeclBinding && (!(sender instanceof SimpleName) || !((SimpleName) sender).getIdentifier().equals(senderDeclBinding.getName())))) {
					proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments,
							senderDeclBinding, IProposalRelevance.CREATE_METHOD));
				}

				if (senderDeclBinding.isNested() && cu.equals(targetCU) && sender == null && Bindings.findMethodInHierarchy(senderDeclBinding, methodName, (ITypeBinding[]) null) == null) { // no covering method
					ASTNode anonymDecl= astRoot.findDeclaringNode(senderDeclBinding);
					if (anonymDecl != null) {
						senderDeclBinding= Bindings.getBindingOfParentType(anonymDecl.getParent());
						isSenderBindingInterface= senderDeclBinding.isInterface();
						if (!senderDeclBinding.isAnonymous()) {
							if (is18OrHigher || !isSenderBindingInterface) {
								String[] args = new String[] { sig,
										org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getTypeSignature(senderDeclBinding) };
								label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, args);
								proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode,
										arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD));
							}
						}
					}
				}
			}
		}
	}
}
 
Example 10
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void addNewMethodProposals(ICompilationUnit cu, CompilationUnit astRoot, Expression sender, List<Expression> arguments, boolean isSuperInvocation, ASTNode invocationNode, String methodName, Collection<ICommandAccess> proposals) throws JavaModelException {
	ITypeBinding nodeParentType= Bindings.getBindingOfParentType(invocationNode);
	ITypeBinding binding= null;
	if (sender != null) {
		binding= sender.resolveTypeBinding();
	} else {
		binding= nodeParentType;
		if (isSuperInvocation && binding != null) {
			binding= binding.getSuperclass();
		}
	}
	if (binding != null && binding.isFromSource()) {
		ITypeBinding senderDeclBinding= binding.getTypeDeclaration();

		ICompilationUnit targetCU= ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding);
		if (targetCU != null) {
			String label;
			Image image;
			ITypeBinding[] parameterTypes= getParameterTypes(arguments);
			if (parameterTypes != null) {
				String sig= ASTResolving.getMethodSignature(methodName, parameterTypes, false);

				if (ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
					if (nodeParentType == senderDeclBinding) {
						label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_description, sig);
						image= JavaPluginImages.get(JavaPluginImages.IMG_MISC_PRIVATE);
					} else {
						label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, new Object[] { sig, BasicElementLabels.getJavaElementName(senderDeclBinding.getName()) } );
						image= JavaPluginImages.get(JavaPluginImages.IMG_MISC_PUBLIC);
					}
					proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
				}
				if (senderDeclBinding.isNested() && cu.equals(targetCU) && sender == null && Bindings.findMethodInHierarchy(senderDeclBinding, methodName, (ITypeBinding[]) null) == null) { // no covering method
					ASTNode anonymDecl= astRoot.findDeclaringNode(senderDeclBinding);
					if (anonymDecl != null) {
						senderDeclBinding= Bindings.getBindingOfParentType(anonymDecl.getParent());
						if (!senderDeclBinding.isAnonymous() && ASTResolving.isUseableTypeInContext(parameterTypes, senderDeclBinding, false)) {
							String[] args= new String[] { sig, ASTResolving.getTypeSignature(senderDeclBinding) };
							label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, args);
							image= JavaPluginImages.get(JavaPluginImages.IMG_MISC_PROTECTED);
							proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD, image));
						}
					}
				}
			}
		}
	}
}
 
Example 11
Source File: ReplaceTypeCodeWithStateStrategyInputPage.java    From JDeodorant with MIT License 4 votes vote down vote up
public ReplaceTypeCodeWithStateStrategyInputPage(ReplaceTypeCodeWithStateStrategy refactoring) {
	super("State/Strategy Type Names");
	this.refactoring = refactoring;
	ICompilationUnit sourceCompilationUnit = (ICompilationUnit)refactoring.getSourceCompilationUnit().getJavaElement();
	this.parentPackage = (IPackageFragment)sourceCompilationUnit.getParent();
	this.parentPackageClassNames = new ArrayList<String>();
	try {
		for(ICompilationUnit compilationUnit : parentPackage.getCompilationUnits()) {
			String className = compilationUnit.getElementName();
			parentPackageClassNames.add(className.substring(0, className.indexOf(".java")));
		}
	} catch (JavaModelException e) {
		e.printStackTrace();
	}
	TypeVisitor typeVisitor = new TypeVisitor();
	refactoring.getSourceCompilationUnit().accept(typeVisitor);
	Set<ITypeBinding> typeBindings = typeVisitor.getTypeBindings();
	for(ITypeBinding typeBinding : typeBindings) {
		if(!parentPackageClassNames.contains(typeBinding.getName()) && !typeBinding.isNested()) {
			parentPackageClassNames.add(typeBinding.getName());
		}
	}
	this.javaLangClassNames = new ArrayList<String>();
	this.javaLangClassNames.add("Boolean");
	this.javaLangClassNames.add("Byte");
	this.javaLangClassNames.add("Character");
	this.javaLangClassNames.add("Class");
	this.javaLangClassNames.add("Double");
	this.javaLangClassNames.add("Enum");
	this.javaLangClassNames.add("Error");
	this.javaLangClassNames.add("Exception");
	this.javaLangClassNames.add("Float");
	this.javaLangClassNames.add("Integer");
	this.javaLangClassNames.add("Long");
	this.javaLangClassNames.add("Math");
	this.javaLangClassNames.add("Number");
	this.javaLangClassNames.add("Object");
	this.javaLangClassNames.add("Package");
	this.javaLangClassNames.add("Process");
	this.javaLangClassNames.add("Runtime");
	this.javaLangClassNames.add("Short");
	this.javaLangClassNames.add("String");
	this.javaLangClassNames.add("StringBuffer");
	this.javaLangClassNames.add("StringBuilder");
	this.javaLangClassNames.add("System");
	this.javaLangClassNames.add("Thread");
	this.javaLangClassNames.add("Void");
	this.textMap = new LinkedHashMap<Text, SimpleName>();
	this.defaultNamingMap = new LinkedHashMap<Text, String>();
}