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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#isMember() . 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: Binding2JavaModel.java    From lapse-plus with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts the given <code>ITypeBinding</code> into a <code>IType</code>
 * using the classpath defined by the given Java project. Returns <code>null</code>
 * if the conversion isn't possible.
 */
public static IType find(ITypeBinding type, IJavaProject scope) throws JavaModelException {
    if (type.isPrimitive())
        return null;
    String[] typeElements= getNameComponents(type);
    IJavaElement element= scope.findElement(getPathToCompilationUnit(type.getPackage(), typeElements[0]));
    IType candidate= null;
    if (element instanceof ICompilationUnit) {
        candidate= ((ICompilationUnit)element).getType(typeElements[0]);
    } else if (element instanceof IClassFile) {
        candidate= ((IClassFile)element).getType();
    } else if (element == null){
        if (type.isMember())
            candidate= findType(scope, getFullyQualifiedImportName(type.getDeclaringClass()));
        else
            candidate= findType(scope, getFullyQualifiedImportName(type));
    }
    
    if (candidate == null || typeElements.length == 1)
        return candidate;
        
    return findTypeInType(typeElements, candidate);
}
 
Example 2
Source File: NewCUUsingWizardProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Fill-in the "Super Class" and "Super Interfaces" fields.
 * @param page the wizard page.
 */
private void fillInWizardPageSuperTypes(NewTypeWizardPage page) {
	ITypeBinding type= getPossibleSuperTypeBinding(fNode);
	type= Bindings.normalizeTypeBinding(type);
	if (type != null) {
		if (type.isArray()) {
			type= type.getElementType();
		}
		if (type.isTopLevel() || type.isMember()) {
			if (type.isClass() && (fTypeKind == K_CLASS)) {
				page.setSuperClass(type.getQualifiedName(), true);
			} else if (type.isInterface()) {
				List<String> superInterfaces= new ArrayList<String>();
				superInterfaces.add(type.getQualifiedName());
				page.setSuperInterfaces(superInterfaces, true);
			}
		}
	}
}
 
Example 3
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
ParameterNameInitializer createParameterNameInitializer(
		IMethodBinding method,
		WorkingCopyOwner workingCopyOwner,
		JvmExecutable result,
		String handleIdentifier,
		String[] path,
		String name,
		SegmentSequence signaturex) {
	if (method.isConstructor()) {
		ITypeBinding declarator = method.getDeclaringClass();
		if (declarator.isEnum()) {
			return new EnumConstructorParameterNameInitializer(workingCopyOwner, result, handleIdentifier, path, name, signaturex);
		}
		if (declarator.isMember()) {
			return new ParameterNameInitializer(workingCopyOwner, result, handleIdentifier, path, name, signaturex) {
				@Override
				protected void setParameterNames(IMethod javaMethod, java.util.List<JvmFormalParameter> parameters) throws JavaModelException {
					String[] parameterNames = javaMethod.getParameterNames();
					int size = parameters.size();
					if (size == parameterNames.length) {
						super.setParameterNames(javaMethod, parameters);
					} else if (size == parameterNames.length - 1) {
						for (int i = 1; i < parameterNames.length; i++) {
							String string = parameterNames[i];
							parameters.get(i - 1).setName(string);
						}
					} else {
						throw new IllegalStateException("unmatching arity for java method "+javaMethod.toString()+" and "+getExecutable().getIdentifier());
					}
				}
			};
		}
	}
	return new ParameterNameInitializer(workingCopyOwner, result, handleIdentifier, path, name, signaturex);
}
 
Example 4
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns a type reference for the given type binding. If the binding is null, an {@link JvmUnknownTypeReference unknown}
 * type reference is returned.
 */
// @NonNull 
protected JvmTypeReference createTypeReference(/* @Nullable */ ITypeBinding typeBinding) {
	if (typeBinding == null) {
		return TypesFactory.eINSTANCE.createJvmUnknownTypeReference();
	}
	if (typeBinding.isArray()) {
		ITypeBinding componentType = typeBinding.getComponentType();
		JvmTypeReference componentTypeReference = createTypeReference(componentType);
		JvmGenericArrayTypeReference typeReference = TypesFactory.eINSTANCE.createJvmGenericArrayTypeReference();
		typeReference.setComponentType(componentTypeReference);
		return typeReference;
	}
	ITypeBinding outer = null;
	if (typeBinding.isMember() && !Modifier.isStatic(typeBinding.getModifiers())) {
		outer = typeBinding.getDeclaringClass();
	}
	JvmParameterizedTypeReference result;
	if (outer != null) {
		JvmParameterizedTypeReference outerReference = (JvmParameterizedTypeReference) createTypeReference(outer);
		result = TypesFactory.eINSTANCE.createJvmInnerTypeReference();
		((JvmInnerTypeReference) result).setOuter(outerReference);
	} else {
		result = TypesFactory.eINSTANCE.createJvmParameterizedTypeReference();
	}
	ITypeBinding[] typeArguments = typeBinding.getTypeArguments();
	if (typeArguments.length != 0) {
		ITypeBinding erasure = typeBinding.getErasure();
		result.setType(createProxy(erasure));
		InternalEList<JvmTypeReference> arguments = (InternalEList<JvmTypeReference>)result.getArguments();
		for (int i = 0; i < typeArguments.length; i++) {
			JvmTypeReference argument = createTypeArgument(typeArguments[i]);
			arguments.addUnique(argument);
		}
	} else {
		result.setType(createProxy(typeBinding));
	}
	return result;
}
 
Example 5
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 6
Source File: OrganizeImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean needsImport(ITypeBinding typeBinding, SimpleName ref) {
	if (!typeBinding.isTopLevel() && !typeBinding.isMember() || typeBinding.isRecovered()) {
		return false; // no imports for anonymous, local, primitive types or parameters types
	}
	int modifiers= typeBinding.getModifiers();
	if (Modifier.isPrivate(modifiers)) {
		return false; // imports for privates are not required
	}
	ITypeBinding currTypeBinding= Bindings.getBindingOfParentType(ref);
	if (currTypeBinding == null) {
		if (ASTNodes.getParent(ref, ASTNode.PACKAGE_DECLARATION) != null) {
			return true; // reference in package-info.java
		}
		return false; // not in a type
	}
	if (!Modifier.isPublic(modifiers)) {
		if (!currTypeBinding.getPackage().getName().equals(typeBinding.getPackage().getName())) {
			return false; // not visible
		}
	}

	ASTNode parent= ref.getParent();
	while (parent instanceof Type) {
		parent= parent.getParent();
	}
	if (parent instanceof AbstractTypeDeclaration && parent.getParent() instanceof CompilationUnit) {
		return true;
	}

	if (typeBinding.isMember()) {
		if (fAnalyzer.isDeclaredInScope(typeBinding, ref, ScopeAnalyzer.TYPES | ScopeAnalyzer.CHECK_VISIBILITY))
			return false;
	}
	return true;
}
 
Example 7
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;
}