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

The following examples show how to use org.eclipse.jdt.core.dom.ITypeBinding#getQualifiedName() . 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: ASTNodeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the new type node representing the return type of <code>lambdaExpression</code>
 * including the extra dimensions.
 * 
 * @param lambdaExpression the lambda expression
 * @param ast the AST to create the return type with
 * @param importRewrite the import rewrite to use, or <code>null</code>
 * @param context the import rewrite context, or <code>null</code>
 * @return a new type node created with the given AST representing the return type of
 *         <code>lambdaExpression</code>
 * 
 * @since 3.10
 */
public static Type newReturnType(LambdaExpression lambdaExpression, AST ast, ImportRewrite importRewrite, ImportRewriteContext context) {
	IMethodBinding method= lambdaExpression.resolveMethodBinding();
	if (method != null) {
		ITypeBinding returnTypeBinding= method.getReturnType();
		if (importRewrite != null) {
			return importRewrite.addImport(returnTypeBinding, ast);
		} else {
			String qualifiedName= returnTypeBinding.getQualifiedName();
			if (qualifiedName.length() > 0) {
				return newType(ast, qualifiedName);
			}
		}
	}
	// fall-back
	return ast.newSimpleType(ast.newSimpleName("Object")); //$NON-NLS-1$
}
 
Example 2
Source File: AccessAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void endVisit(CompilationUnit node) {
	// If we don't had a static import to the field we don't
	// have to add any, even if we generated a setter or
	// getter access.
	if (!fRemoveStaticImport)
		return;

	ITypeBinding type= fFieldBinding.getDeclaringClass();
	String fieldName= fFieldBinding.getName();
	String typeName= type.getQualifiedName();
	if (fRemoveStaticImport) {
		fImportRewriter.removeStaticImport(typeName + "." + fieldName); //$NON-NLS-1$
	}
	if (fReferencingGetter) {
		fImportRewriter.addStaticImport(typeName, fGetter, false);
	}
	if (fReferencingSetter) {
		fImportRewriter.addStaticImport(typeName, fSetter, false);
	}
}
 
Example 3
Source File: JavaTypeHierarchyExtractor.java    From api-mining with GNU General Public License v3.0 6 votes vote down vote up
private void getTypeBindingParents(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		binding = binding.getErasure();
	}
	final String bindingName = binding.isRecovered() ? binding
			.getName() : binding.getQualifiedName();
	final ITypeBinding superclassBinding = binding.getSuperclass();
	if (superclassBinding != null) {
		addTypes(
				superclassBinding.isRecovered() ? superclassBinding.getName()
						: superclassBinding.getQualifiedName(),
				bindingName);
		getTypeBindingParents(superclassBinding);
	}

	for (ITypeBinding iface : binding.getInterfaces()) {
		if (iface.isParameterizedType()) {
			iface = iface.getErasure();
		}
		addTypes(
				iface.isRecovered() ? iface.getName()
						: iface.getQualifiedName(), bindingName);
		getTypeBindingParents(iface);
	}
}
 
Example 4
Source File: TypeURIHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public String getQualifiedName(ITypeBinding binding) {
	if (binding.isParameterizedType()) {
		return getQualifiedName(binding.getErasure());
	}
	if (binding.isArray()) {
		return getQualifiedName(binding.getComponentType(), new StringBuilder()).append("[]").toString();
	}
	if (binding.isTopLevel() || binding.isTypeVariable() || binding.isPrimitive())
		return binding.getQualifiedName();
	return getQualifiedName(binding.getDeclaringClass(), new StringBuilder()).append('$').append(binding.getName()).toString();
}
 
Example 5
Source File: ASTNodeMatcher.java    From JDeodorant with MIT License 5 votes vote down vote up
private static boolean isNumberPrimitiveType(ITypeBinding typeBinding) {
	if(typeBinding.isPrimitive()) {
		String name = typeBinding.getQualifiedName();
		if(name.equals("byte") || name.equals("double") || name.equals("float") || name.equals("int") || name.equals("long") || name.equals("short"))
			return true;
	}
	return false;
}
 
Example 6
Source File: ConvertIterableLoopOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IStatus checkExpressionCondition() {
	Expression expression= getForStatement().getExpression();
	if (!(expression instanceof MethodInvocation))
		return SEMANTIC_CHANGE_WARNING_STATUS;

	MethodInvocation invoc= (MethodInvocation)expression;
	IMethodBinding methodBinding= invoc.resolveMethodBinding();
	if (methodBinding == null)
		return ERROR_STATUS;

	ITypeBinding declaringClass= methodBinding.getDeclaringClass();
	if (declaringClass == null)
		return ERROR_STATUS;

	String qualifiedName= declaringClass.getQualifiedName();
	String methodName= invoc.getName().getIdentifier();
	if (qualifiedName.startsWith("java.util.Enumeration")) { //$NON-NLS-1$
		if (!methodName.equals("hasMoreElements")) //$NON-NLS-1$
			return SEMANTIC_CHANGE_WARNING_STATUS;
	} else if (qualifiedName.startsWith("java.util.Iterator")) { //$NON-NLS-1$
		if (!methodName.equals("hasNext")) //$NON-NLS-1$
			return SEMANTIC_CHANGE_WARNING_STATUS;
		return checkIteratorCondition();
	} else {
		return SEMANTIC_CHANGE_WARNING_STATUS;
	}

	return StatusInfo.OK_STATUS;
}
 
Example 7
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
public Type ensureTypeFromTypeBinding(ITypeBinding binding) {
	String qualifiedName = binding.getQualifiedName();
	if (types.has(qualifiedName))
		return types.named(qualifiedName);
	Type type = createTypeFromTypeBinding(binding);
	type.setName(binding.getName());
	types.add(qualifiedName, type);
	type.setIsStub(true);
	extractBasicModifiersFromBinding(binding.getModifiers(), type);
	type.setContainer(ensureContainerEntityForTypeBinding(binding));
	if (binding.getSuperclass() != null)
		createInheritanceFromSubtypeToSuperTypeBinding(type, binding.getSuperclass());
	for (ITypeBinding interfaceBinding : binding.getInterfaces()) {
		createInheritanceFromSubtypeToSuperTypeBinding(type, interfaceBinding);
	}
	if (binding.isParameterizedType()) {
		/*
		 * This if duplicates the condition from the create method because we want to
		 * break possible infinite loops induced by the below ensure calls. This is
		 * achieved by having this condition after the addition of the type in the types
		 * map.
		 */
		ParameterizedType parameterizedType = ((ParameterizedType) type);
		if (ensureTypeFromTypeBinding(binding.getErasure()) instanceof ParameterizableClass)
			parameterizedType.setParameterizableClass(
					(ParameterizableClass) ensureTypeFromTypeBinding(binding.getErasure()));
		List<Type> arguments = Stream.of(binding.getTypeArguments()).map(arg -> ensureTypeFromTypeBinding(arg))
				.collect(Collectors.toList());
		parameterizedType.setArguments(arguments);
	}
	if (binding.isGenericType()) {
		ParameterizableClass parameterizableClass = (ParameterizableClass) type;
		Stream.of(binding.getTypeParameters())
				.forEach(p -> createParameterType(p.getName().toString(), parameterizableClass));
	}
	return type;
}
 
Example 8
Source File: ElementTypeTeller.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isSpecificClassifier(TypeDeclaration classifierDeclaration) {
	ITypeBinding superclassBinding = classifierDeclaration.resolveBinding().getSuperclass();
	if (superclassBinding == null) {
		return false;
	}
	String superclassQualifiedName = superclassBinding.getQualifiedName();
	boolean extendsModelClass = superclassQualifiedName.equals(ModelClass.class.getCanonicalName());
	boolean extendsSignal = superclassQualifiedName.equals(Signal.class.getCanonicalName());

	return !extendsModelClass && !extendsSignal;
}
 
Example 9
Source File: TypeRules.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isArrayCompatible(ITypeBinding definedType) {
	if (definedType.isTopLevel()) {
		if (definedType.isClass()) {
			return "Object".equals(definedType.getName()) && "java.lang".equals(definedType.getPackage().getName());  //$NON-NLS-1$//$NON-NLS-2$
		} else {
			String qualifiedName= definedType.getQualifiedName();
			return "java.io.Serializable".equals(qualifiedName) || "java.lang.Cloneable".equals(qualifiedName); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	return false;
}
 
Example 10
Source File: RemoteServiceUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
static boolean isSynchronousInterface(ITypeBinding typeBinding) {
  String qualifiedName = typeBinding.getQualifiedName();
  if (qualifiedName.equals(REMOTE_SERVICE_QUALIFIED_NAME) ||
      qualifiedName.equals(RPC_SERVICE_QUALIFIED_NAME)) {
    // Ignore the RemoteService and RpcService marker interfaces
    return false;
  }

  ITypeBinding remoteServiceBinding = Bindings.findTypeInHierarchy(
      typeBinding, REMOTE_SERVICE_QUALIFIED_NAME);
  return remoteServiceBinding != null;
}
 
Example 11
Source File: TypeBindingInheritanceDetection.java    From JDeodorant with MIT License 5 votes vote down vote up
private void generateInheritanceTrees(List<ITypeBinding> typeBindings) {
	for(ITypeBinding typeBinding : typeBindings) {
		ITypeBinding superType = extendsOrImplements(typeBinding, typeBindings);
		if(superType != null && !typeBinding.getQualifiedName().equals(superType.getQualifiedName())) {
			String childName = typeBinding.getQualifiedName();
			String parentName = superType.getQualifiedName();
			InheritanceTree childTree = getTree(childName);
			InheritanceTree parentTree = getTree(parentName);
			if(childTree == null && parentTree == null) {
				InheritanceTree tree = new InheritanceTree();
				tree.addChildToParent(childName, parentName);
				inheritanceTreeList.add(tree);
			}
			else if(childTree == null) {
				parentTree.addChildToParent(childName, parentName);
			}
			else if(parentTree == null) {
				childTree.addChildToParent(childName, parentName);
			}
			else if( !childTree.equals(parentTree) ) {
				parentTree.addChildRootNodeToParent(childTree.getRootNode(), parentName);
				inheritanceTreeList.remove(childTree);
			}
		}
		else if(getTree(typeBinding.getQualifiedName()) == null) {
			inheritanceTreeList.add(new InheritanceTree(typeBinding.getQualifiedName()));
		}
	}
}
 
Example 12
Source File: DIT.java    From ck with Apache License 2.0 5 votes vote down vote up
private void calculate(ITypeBinding binding) {
	ITypeBinding father = binding.getSuperclass();
	if (father != null) {
		String fatherName = father.getQualifiedName();
		if (fatherName.endsWith("Object")) return;
		dit++;

		calculate(father);
	}

}
 
Example 13
Source File: ReferenceResolvingVisitor.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
private ClassReference processTypeAsEnum(ITypeBinding typeBinding, Expression expression, ResolutionStatus resolutionStatus, int lineNumber, int columnNumber, int length, String line)
{
    if (expression == null)
        return null;

    String fullExpression = null;
    String enumPackage = null;
    String enumClassName = null;
    if (typeBinding != null)
    {
        fullExpression = typeBinding.getQualifiedName();
        enumPackage = typeBinding.getPackage().getName();
        enumClassName = typeBinding.getName();
    }

    if (expression instanceof Name)
    {
        if (StringUtils.isNotBlank(fullExpression))
            fullExpression += ".";

        if (expression instanceof QualifiedName)
            fullExpression += ((QualifiedName) expression).getName();
        else
            fullExpression += ((Name) expression).getFullyQualifiedName();
    }

    ClassReference reference = new ClassReference(fullExpression, enumPackage, enumClassName, null, resolutionStatus,
                TypeReferenceLocation.ENUM_CONSTANT,
                lineNumber,
                columnNumber,
                length,
                line);
    this.classReferences.add(reference);
    return reference;
}
 
Example 14
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static String[] getParameterTypeNamesForSeeTag(IMethodBinding binding) {
	ITypeBinding[] typeBindings= binding.getParameterTypes();
	String[] result= new String[typeBindings.length];
	for (int i= 0; i < result.length; i++) {
		ITypeBinding curr= typeBindings[i];
		curr= curr.getErasure(); // Javadoc references use erased type
		result[i]= curr.getQualifiedName();
	}
	return result;
}
 
Example 15
Source File: ASTReader.java    From JDeodorant with MIT License 5 votes vote down vote up
private void processFieldDeclaration(final ClassObject classObject, FieldDeclaration fieldDeclaration) {
	Type fieldType = fieldDeclaration.getType();
	ITypeBinding binding = fieldType.resolveBinding();
	List<CommentObject> fieldDeclarationComments = new ArrayList<CommentObject>();
	int fieldDeclarationStartPosition = fieldDeclaration.getStartPosition();
	int fieldDeclarationEndPosition = fieldDeclarationStartPosition + fieldDeclaration.getLength();
	for(CommentObject comment : classObject.commentList) {
		int commentStartPosition = comment.getStartPosition();
		int commentEndPosition = commentStartPosition + comment.getLength();
		if(fieldDeclarationStartPosition <= commentStartPosition && fieldDeclarationEndPosition >= commentEndPosition) {
			fieldDeclarationComments.add(comment);
		}
	}
	List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
	for(VariableDeclarationFragment fragment : fragments) {
		String qualifiedName = binding.getQualifiedName();
		TypeObject typeObject = TypeObject.extractTypeObject(qualifiedName);
		typeObject.setArrayDimension(typeObject.getArrayDimension() + fragment.getExtraDimensions());
		FieldObject fieldObject = new FieldObject(typeObject, fragment.getName().getIdentifier());
		fieldObject.setClassName(classObject.getName());
		fieldObject.setVariableDeclarationFragment(fragment);
		fieldObject.addComments(fieldDeclarationComments);
		
		int fieldModifiers = fieldDeclaration.getModifiers();
		if((fieldModifiers & Modifier.PUBLIC) != 0)
			fieldObject.setAccess(Access.PUBLIC);
		else if((fieldModifiers & Modifier.PROTECTED) != 0)
			fieldObject.setAccess(Access.PROTECTED);
		else if((fieldModifiers & Modifier.PRIVATE) != 0)
			fieldObject.setAccess(Access.PRIVATE);
		else
			fieldObject.setAccess(Access.NONE);
		
		if((fieldModifiers & Modifier.STATIC) != 0)
			fieldObject.setStatic(true);
		
		classObject.addField(fieldObject);
	}
}
 
Example 16
Source File: RemoteServiceValidator.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public boolean visit(TypeDeclaration changedType) {
  if (!changedType.isInterface()) {
    return true;
  }

  ITypeBinding typeBinding = changedType.resolveBinding();
  if (typeBinding == null) {
    return true;
  }

  PairedInterfaceValidator validator;
  String typeQualifiedName = typeBinding.getQualifiedName();
  TypeDeclaration dependentType = null;

  String dependentTypeQualifiedName;

  if (RemoteServiceUtilities.isSynchronousInterface(typeBinding)) {
    dependentTypeQualifiedName = RemoteServiceUtilities.computeAsyncTypeName(typeQualifiedName);
    dependentType = JavaASTUtils.findTypeDeclaration(javaProject,
        dependentTypeQualifiedName);
    validator = synchronousInterfaceValidator;
  } else {
    validator = asynchronousInterfaceValidator;
    dependentTypeQualifiedName = RemoteServiceUtilities.computeSyncTypeName(typeQualifiedName);
    if (dependentTypeQualifiedName == null) {
      // Not an async interface...
      return true;
    }

    dependentType = JavaASTUtils.findTypeDeclaration(javaProject,
        dependentTypeQualifiedName);
  }

  // Add the type dependency (even if the type doesn't yet resolve)
  dependentTypes.add(dependentTypeQualifiedName);

  ITypeBinding dependentTypeBinding = null;
  if (dependentType != null) {
    dependentTypeBinding = dependentType.resolveBinding();
  }

  problems.addAll(validator.validate(changedType, dependentTypeBinding));

  return true;
}
 
Example 17
Source File: Utils.java    From txtUML with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean isBasicType(ITypeBinding type, boolean isVoidAllowed) {

		String qualifedName = type.getQualifiedName();
		return AllowdBasicTypes.contains(qualifedName)
				|| (qualifedName.equals(void.class.getCanonicalName()) && isVoidAllowed);
	}
 
Example 18
Source File: ReferenceResolvingVisitor.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
private AnnotationClassReference processAnnotation(ClassReference annotatedReference, Annotation node)
{
    final ITypeBinding typeBinding = node.resolveTypeBinding();
    final AnnotationClassReference reference;
    final String qualifiedName;
    final String packageName;
    final String className;
    final ResolutionStatus status;
    if (typeBinding != null)
    {
        status = ResolutionStatus.RESOLVED;
        qualifiedName = typeBinding.getQualifiedName();
        packageName = typeBinding.getPackage().getName();
        className = typeBinding.getName();
    }
    else
    {
        ResolveClassnameResult result = resolveClassname(node.getTypeName().toString());
        status = result.found ? ResolutionStatus.RECOVERED : ResolutionStatus.UNRESOLVED;
        qualifiedName = result.result;

        PackageAndClassName packageAndClassName = PackageAndClassName.parseFromQualifiedName(result.result);
        packageName = packageAndClassName.packageName;
        className = packageAndClassName.className;
    }

    reference = new AnnotationClassReference(
                annotatedReference,
                qualifiedName,
                packageName,
                className,
                status,
                compilationUnit.getLineNumber(node.getStartPosition()),
                compilationUnit.getColumnNumber(node.getStartPosition()),
                node.getLength(),
                node.toString());

    addAnnotationValues(annotatedReference, reference, node);

    return reference;
}
 
Example 19
Source File: StubUtility2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static MethodDeclaration createDelegationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding delegate, IVariableBinding delegatingField, CodeGenerationSettings settings) throws CoreException {
	Assert.isNotNull(delegate);
	Assert.isNotNull(delegatingField);
	Assert.isNotNull(settings);

	AST ast= rewrite.getAST();

	MethodDeclaration decl= ast.newMethodDeclaration();
	decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, delegate.getModifiers() & ~Modifier.SYNCHRONIZED & ~Modifier.ABSTRACT & ~Modifier.NATIVE));

	decl.setName(ast.newSimpleName(delegate.getName()));
	decl.setConstructor(false);

	createTypeParameters(imports, context, ast, delegate, decl);

	decl.setReturnType2(imports.addImport(delegate.getReturnType(), ast, context));

	List<SingleVariableDeclaration> params= createParameters(unit.getJavaProject(), imports, context, ast, delegate, null, decl);

	createThrownExceptions(decl, delegate, imports, context, ast);

	Block body= ast.newBlock();
	decl.setBody(body);

	String delimiter= StubUtility.getLineDelimiterUsed(unit);

	Statement statement= null;
	MethodInvocation invocation= ast.newMethodInvocation();
	invocation.setName(ast.newSimpleName(delegate.getName()));
	List<Expression> arguments= invocation.arguments();
	for (int i= 0; i < params.size(); i++)
		arguments.add(ast.newSimpleName(params.get(i).getName().getIdentifier()));
	if (settings.useKeywordThis) {
		FieldAccess access= ast.newFieldAccess();
		access.setExpression(ast.newThisExpression());
		access.setName(ast.newSimpleName(delegatingField.getName()));
		invocation.setExpression(access);
	} else
		invocation.setExpression(ast.newSimpleName(delegatingField.getName()));
	if (delegate.getReturnType().isPrimitive() && delegate.getReturnType().getName().equals("void")) {//$NON-NLS-1$
		statement= ast.newExpressionStatement(invocation);
	} else {
		ReturnStatement returnStatement= ast.newReturnStatement();
		returnStatement.setExpression(invocation);
		statement= returnStatement;
	}
	body.statements().add(statement);

	ITypeBinding declaringType= delegatingField.getDeclaringClass();
	if (declaringType == null) { // can be null for
		return decl;
	}

	String qualifiedName= declaringType.getQualifiedName();
	IPackageBinding packageBinding= declaringType.getPackage();
	if (packageBinding != null) {
		if (packageBinding.getName().length() > 0 && qualifiedName.startsWith(packageBinding.getName()))
			qualifiedName= qualifiedName.substring(packageBinding.getName().length());
	}

	if (settings.createComments) {
		/*
		 * TODO: have API for delegate method comments This is an inlined
		 * version of
		 * {@link CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, IMethodBinding, String)}
		 */
		delegate= delegate.getMethodDeclaration();
		String declaringClassQualifiedName= delegate.getDeclaringClass().getQualifiedName();
		String linkToMethodName= delegate.getName();
		String[] parameterTypesQualifiedNames= StubUtility.getParameterTypeNamesForSeeTag(delegate);
		String string= StubUtility.getMethodComment(unit, qualifiedName, decl, delegate.isDeprecated(), linkToMethodName, declaringClassQualifiedName, parameterTypesQualifiedNames, true, delimiter);
		if (string != null) {
			Javadoc javadoc= (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
			decl.setJavadoc(javadoc);
		}
	}
	return decl;
}
 
Example 20
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Returns the fully qualified name of the specified type binding.
 * <p>
 * If the binding resolves to a generic type, the fully qualified name of the raw type is returned.
 *
 * @param type the type binding to get its fully qualified name
 * @return the fully qualified name
 */
public static String getFullyQualifiedName(ITypeBinding type) {
	String name= type.getQualifiedName();
	final int index= name.indexOf('<');
	if (index > 0)
		name= name.substring(0, index);
	return name;
}