Java Code Examples for org.eclipse.jdt.core.dom.Type#isPrimitiveType()

The following examples show how to use org.eclipse.jdt.core.dom.Type#isPrimitiveType() . 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: NodeUtils.java    From SimFix with GNU General Public License v2.0 6 votes vote down vote up
private static boolean isPrimitiveType(Type type){
	if(type == null){
		return false;
	}
	if(type.isPrimitiveType()){
		return true;
	}
	switch(type.toString()){
	case "Integer":
	case "Long":
	case "Float":
	case "Short":
	case "Double":
		return true;
	}
	return false;
}
 
Example 2
Source File: JavaASTFlattener.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public boolean isBooleanType(final Expression expression) {
  if ((expression instanceof BooleanLiteral)) {
    return true;
  }
  if ((expression instanceof SimpleName)) {
    final Type declType = this._aSTFlattenerUtils.findDeclaredType(((SimpleName)expression));
    if ((declType != null)) {
      boolean _matched = false;
      boolean _isPrimitiveType = declType.isPrimitiveType();
      if (_isPrimitiveType) {
        _matched=true;
        PrimitiveType.Code _primitiveTypeCode = ((PrimitiveType) declType).getPrimitiveTypeCode();
        return Objects.equal(_primitiveTypeCode, PrimitiveType.BOOLEAN);
      }
    }
  }
  return false;
}
 
Example 3
Source File: CPlusPlusASTNodeWriter.java    From juniversal with MIT License 6 votes vote down vote up
/**
 * Write out a type, when it's used (as opposed to defined).
 *
 * @param type type to write
 */
public void writeType(Type type, boolean useRawPointer) {
    boolean referenceType = !type.isPrimitiveType();

    if (!referenceType)
        writeNode(type);
    else {
        if (useRawPointer) {
            writeNode(type);
            write("*");
        } else {
            write("ptr< ");
            writeNode(type);
            write(" >");
        }
    }
}
 
Example 4
Source File: JavaSourceFileParser.java    From BUILD_file_generator with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true iff 'methodDeclaration' represents a void static method named 'main' that takes a
 * single String[] parameter.
 */
private static boolean isMainMethod(MethodDeclaration methodDeclaration) {
  // Is it static?
  if ((methodDeclaration.getModifiers() & Modifier.STATIC) == 0) {
    return false;
  }
  // Does it return void?
  Type returnType = methodDeclaration.getReturnType2();
  if (!returnType.isPrimitiveType()) {
    return false;
  }
  if (((PrimitiveType) returnType).getPrimitiveTypeCode() != PrimitiveType.VOID) {
    return false;
  }
  // Is it called 'main'?
  if (!"main".equals(methodDeclaration.getName().getIdentifier())) {
    return false;
  }
  // Does it have a single parameter?
  if (methodDeclaration.parameters().size() != 1) {
    return false;
  }

  // Is the parameter's type String[]?
  SingleVariableDeclaration pt =
      getOnlyElement((List<SingleVariableDeclaration>) methodDeclaration.parameters());
  IVariableBinding vb = pt.resolveBinding();
  if (vb == null) {
    return false;
  }
  ITypeBinding tb = vb.getType();
  return tb != null && "java.lang.String[]".equals(tb.getQualifiedName());
}
 
Example 5
Source File: NodeUtils.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
public static double typeSimilarity(Type t1, Type t2){
	if(t1 == null || t2 == null){
		return 0.0;
	}
	if(t1.toString().equals(t2.toString()) || (t1.isPrimitiveType() && t2.isPrimitiveType() && isWidenType(t1, t2))){
		return 1.0;
	}
	return 0.0;
}
 
Example 6
Source File: ASTUtil.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static String getPrettyTypeName(Type type) {
    if (type.isArrayType()) {
        return getPrettyTypeName((ArrayType) type);
    } else if (type.isParameterizedType()) {
        return getPrettyTypeName((ParameterizedType) type);
    } else if (type.isPrimitiveType()) {
        return getPrettyTypeName((PrimitiveType) type);
    } else if (type.isQualifiedType()) {
        return getPrettyTypeName((QualifiedType) type);
    } else if (type.isSimpleType()) {
        return getPrettyTypeName((SimpleType) type);
    } else {
        return "";
    }
}
 
Example 7
Source File: TypeResolver.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
/**
 * @param type
 * @return
 */
protected String getNameOfType(final Type type) {
	 String nameOfType = "";
	if (type != null) {
		if (type.isPrimitiveType()) {
			nameOfType = type.toString();
		} else if (type.isParameterizedType()) {
			nameOfType = getParametrizedType((ParameterizedType) type);
		} else if (type.isArrayType()) {
			final ArrayType array = (ArrayType) type;
			nameOfType = getNameOfType(array.getElementType()) /*+ "[]"*/;
		} else if (type.isUnionType()) {
               // TODO: this is used for exceptions till now
               // So we will just capture the first type that we encounter
			final UnionType uType = (UnionType) type;
			final StringBuilder sb = new StringBuilder();
			for (final Object unionedType : uType.types()) {
				sb.append(getNameOfType((Type) unionedType));
                   break;
			}

			nameOfType = sb.toString();
		} else if (type.isWildcardType()) {
			final WildcardType wType = (WildcardType) type;
			nameOfType = (wType.isUpperBound() ? "? extends " : "? super ")
					+ getNameOfType(wType.getBound());
		} else {
			nameOfType = getFullyQualifiedNameFor(type.toString());
		}
	}
	return nameOfType;
}
 
Example 8
Source File: JavaApproximateTypeInferencer.java    From api-mining with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param type
 * @return
 */
protected String getNameOfType(final Type type) {
	final String nameOfType;
	if (type.isPrimitiveType()) {
		nameOfType = type.toString();
	} else if (type.isParameterizedType()) {
		nameOfType = getParametrizedType((ParameterizedType) type);
	} else if (type.isArrayType()) {
		final ArrayType array = (ArrayType) type;
		nameOfType = getNameOfType(array.getElementType()) + "[]";
	} else if (type.isUnionType()) {
		final UnionType uType = (UnionType) type;
		final StringBuffer sb = new StringBuffer();
		for (final Object unionedType : uType.types()) {
			sb.append(getNameOfType(((Type) unionedType)));
			sb.append(" | ");
		}
		sb.delete(sb.length() - 3, sb.length());
		nameOfType = sb.toString();
	} else if (type.isWildcardType()) {
		final WildcardType wType = (WildcardType) type;
		if (wType.getBound() == null)
			return "?";
		nameOfType = (wType.isUpperBound() ? "? extends " : "? super ") + getNameOfType(wType.getBound());
	} else {
		nameOfType = getFullyQualifiedNameFor(type.toString());
	}
	return nameOfType;
}
 
Example 9
Source File: ASTNodeFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns an expression that is assignable to the given type. <code>null</code> is
 * returned if the type is the 'void' type.
 *
 * @param ast The AST to create the expression for
 * @param type The type of the returned expression
 * @param extraDimensions Extra dimensions to the type
 * @return the Null-literal for reference types, a boolean-literal for a boolean type, a number
 * literal for primitive types or <code>null</code> if the type is void.
 */
public static Expression newDefaultExpression(AST ast, Type type, int extraDimensions) {
	if (extraDimensions == 0 && type.isPrimitiveType()) {
		PrimitiveType primitiveType= (PrimitiveType) type;
		if (primitiveType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) {
			return ast.newBooleanLiteral(false);
		} else if (primitiveType.getPrimitiveTypeCode() == PrimitiveType.VOID) {
			return null;
		} else {
			return ast.newNumberLiteral("0"); //$NON-NLS-1$
		}
	}
	return ast.newNullLiteral();
}
 
Example 10
Source File: NodeUtils.java    From SimFix with GNU General Public License v2.0 2 votes vote down vote up
private static Type union(Type ty1, Type ty2){
	if(ty1 == null){
		return ty2;
	} else if(ty2 == null){
		return ty1;
	}
	
	if(!ty1.isPrimitiveType() || !ty2.isPrimitiveType()){
		return null;
	}
	
	String ty1String = ty1.toString().toLowerCase().replace("integer", "int");
	String ty2String = ty2.toString().toLowerCase().replace("integer", "int");
	
	AST ast = AST.newAST(AST.JLS8);
	if(ty1String.equals("double") || ty2String.equals("double")){
		
		return ast.newPrimitiveType(PrimitiveType.DOUBLE);
		
	} else if(ty1String.equals("float") || ty2String.equals("float")){
		
		return ast.newPrimitiveType(PrimitiveType.FLOAT);
		
	} else if(ty1String.equals("long") || ty2String.equals("long")){
		
		return ast.newPrimitiveType(PrimitiveType.LONG);
		
	} else if(ty1String.equals("int") || ty2String.equals("int")){
		
		return ast.newPrimitiveType(PrimitiveType.INT);
		
	} else if(ty1String.equals("short") || ty2String.equals("short")){
		
		return ast.newPrimitiveType(PrimitiveType.SHORT);
		
	} else {
		
		return ast.newPrimitiveType(PrimitiveType.BYTE);
		
	}
	
}