下面列出了怎么用org.eclipse.jdt.core.dom.PrimitiveType的API类实例代码及写法,或者点击链接到github查看源代码。
private static Type parsePreExprType(Expr expr, String operator){
AST ast = AST.newAST(AST.JLS8);
switch(operator){
case "++":
case "--":
return ast.newPrimitiveType(PrimitiveType.INT);
case "+":
case "-":
return expr.getType();
case "~":
case "!":
return ast.newPrimitiveType(PrimitiveType.BOOLEAN);
default :
return null;
}
}
private InstanceofExpr visit(InstanceofExpression node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
InstanceofExpr instanceofExpr = new InstanceofExpr(startLine, endLine, node);
Expr expression = (Expr) process(node.getLeftOperand());
expression.setParent(instanceofExpr);
instanceofExpr.setExpression(expression);
instanceofExpr.setInstanceType(node.getRightOperand());
AST ast = AST.newAST(AST.JLS8);
Type exprType = ast.newPrimitiveType(PrimitiveType.BOOLEAN);
instanceofExpr.setType(exprType);
return instanceofExpr;
}
/**
* Creates the corresponding statement for the method invocation, based on
* the return type.
*
* @param declaration the method declaration where the invocation statement
* is inserted
* @param invocation the method invocation being encapsulated by the
* resulting statement
* @return the corresponding statement
*/
protected Statement createMethodInvocation(final MethodDeclaration declaration, final MethodInvocation invocation) {
Assert.isNotNull(declaration);
Assert.isNotNull(invocation);
Statement statement= null;
final Type type= declaration.getReturnType2();
if (type == null) {
statement= createExpressionStatement(invocation);
} else {
if (type instanceof PrimitiveType) {
final PrimitiveType primitive= (PrimitiveType) type;
if (primitive.getPrimitiveTypeCode().equals(PrimitiveType.VOID)) {
statement= createExpressionStatement(invocation);
} else {
statement= createReturnStatement(invocation);
}
} else {
statement= createReturnStatement(invocation);
}
}
return statement;
}
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;
}
@Override
protected MethodDeclaration createMethodDeclaration(AST ast) {
MethodDeclaration asyncMethodDecl = ast.newMethodDeclaration();
// New method has same name as original
String methodName = getSyncMethodDeclaration().getName().getIdentifier();
asyncMethodDecl.setName(ast.newSimpleName(methodName));
// Async method has void return type by default (the user can also use
// Request or RequestBuilder as the return type to get more functionality).
// TODO: investigate whether we can enter linked mode after the fix is
// applied, so the user can choose what return type to use. See
// LinkedCorrectionProposal, which is a subclass of
// ASTRewriteCorrectionProposal.
asyncMethodDecl.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
addAsyncParameters(ast, asyncMethodDecl);
// TODO: generate comments for new method
return asyncMethodDecl;
}
private void addNewConstructorToSubclass(AbstractTypeDeclaration subclass, CompilationUnitRewrite cuRewrite) {
AST ast= subclass.getAST();
MethodDeclaration newConstructor= ast.newMethodDeclaration();
newConstructor.setName(ast.newSimpleName(subclass.getName().getIdentifier()));
newConstructor.setConstructor(true);
newConstructor.setJavadoc(null);
newConstructor.modifiers().addAll(ASTNodeFactory.newModifiers(ast, getAccessModifier(subclass)));
newConstructor.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
Block body= ast.newBlock();
newConstructor.setBody(body);
SuperConstructorInvocation superCall= ast.newSuperConstructorInvocation();
addArgumentsToNewSuperConstructorCall(superCall, cuRewrite);
body.statements().add(superCall);
String msg= RefactoringCoreMessages.ChangeSignatureRefactoring_add_constructor;
TextEditGroup description= cuRewrite.createGroupDescription(msg);
cuRewrite.getASTRewrite().getListRewrite(subclass, subclass.getBodyDeclarationsProperty()).insertFirst(newConstructor, description);
// TODO use AbstractTypeDeclaration
}
/**
* Creates the corresponding statement for the method invocation, based on
* the return type.
*
* @param declaration the method declaration where the invocation statement
* is inserted
* @param invocation the method invocation being encapsulated by the
* resulting statement
* @return the corresponding statement
*/
protected Statement createMethodInvocation(final MethodDeclaration declaration, final MethodInvocation invocation) {
Assert.isNotNull(declaration);
Assert.isNotNull(invocation);
Statement statement= null;
final Type type= declaration.getReturnType2();
if (type == null)
statement= createExpressionStatement(invocation);
else {
if (type instanceof PrimitiveType) {
final PrimitiveType primitive= (PrimitiveType) type;
if (primitive.getPrimitiveTypeCode().equals(PrimitiveType.VOID))
statement= createExpressionStatement(invocation);
else
statement= createReturnStatement(invocation);
} else
statement= createReturnStatement(invocation);
}
return statement;
}
public static boolean isGeneralizeTypeAvailable(final IJavaElement element) throws JavaModelException {
if (element != null && element.exists()) {
String type= null;
if (element instanceof IMethod)
type= ((IMethod) element).getReturnType();
else if (element instanceof IField) {
final IField field= (IField) element;
if (JdtFlags.isEnum(field))
return false;
type= field.getTypeSignature();
} else if (element instanceof ILocalVariable)
return true;
else if (element instanceof IType) {
final IType clazz= (IType) element;
if (JdtFlags.isEnum(clazz))
return false;
return true;
}
if (type == null || PrimitiveType.toCode(Signature.toString(type)) != null)
return false;
return true;
}
return false;
}
public static boolean isGeneralizeTypeAvailable(final IStructuredSelection selection) throws JavaModelException {
if (selection.size() == 1) {
final Object element= selection.getFirstElement();
if (element instanceof IMethod) {
final IMethod method= (IMethod) element;
if (!method.exists())
return false;
final String type= method.getReturnType();
if (PrimitiveType.toCode(Signature.toString(type)) == null)
return Checks.isAvailable(method);
} else if (element instanceof IField) {
final IField field= (IField) element;
if (!field.exists())
return false;
if (!JdtFlags.isEnum(field))
return Checks.isAvailable(field);
}
}
return false;
}
private Expression createShiftAssignment(Expression shift1, Expression shift2) {
// (int)(element ^ (element >>> 32));
// see implementation in Arrays.hashCode(), Double.hashCode() and
// Long.hashCode()
CastExpression ce= fAst.newCastExpression();
ce.setType(fAst.newPrimitiveType(PrimitiveType.INT));
InfixExpression unsignedShiftRight= fAst.newInfixExpression();
unsignedShiftRight.setLeftOperand(shift1);
unsignedShiftRight.setRightOperand(fAst.newNumberLiteral("32")); //$NON-NLS-1$
unsignedShiftRight.setOperator(Operator.RIGHT_SHIFT_UNSIGNED);
InfixExpression xor= fAst.newInfixExpression();
xor.setLeftOperand(shift2);
xor.setRightOperand(parenthesize(unsignedShiftRight));
xor.setOperator(InfixExpression.Operator.XOR);
ce.setExpression(parenthesize(xor));
return ce;
}
private static IMember getMember(IStructuredSelection selection) throws JavaModelException {
if (selection.size() != 1)
return null;
Object element= selection.getFirstElement();
if (!(element instanceof IMember))
return null;
if (element instanceof IMethod) {
IMethod method= (IMethod)element;
String returnType= method.getReturnType();
if (PrimitiveType.toCode(Signature.toString(returnType)) != null)
return null;
return method;
} else if (element instanceof IField && !JdtFlags.isEnum((IMember) element)) {
return (IField)element;
}
return null;
}
public boolean visit(PrimitiveType type) {
/*
* PrimitiveType:
byte
short
char
int
long
float
double
boolean
void
*/
styledString.append(type.getPrimitiveTypeCode().toString(), determineDiffStyle(type, new StyledStringStyler(keywordStyle)));
return false;
}
/**
* 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());
}
private BoolLiteral visit(BooleanLiteral node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
BoolLiteral literal = new BoolLiteral(startLine, endLine, node);
literal.setValue(node.booleanValue());
AST ast = AST.newAST(AST.JLS8);
Type type = ast.newPrimitiveType(PrimitiveType.BOOLEAN);
literal.setType(type);
return literal;
}
private CharLiteral visit(CharacterLiteral node) {
int startLine = _cunit.getLineNumber(node.getStartPosition());
int endLine = _cunit.getLineNumber(node.getStartPosition() + node.getLength());
CharLiteral charLiteral = new CharLiteral(startLine, endLine, node);
charLiteral.setValue(node.charValue());
AST ast = AST.newAST(AST.JLS8);
Type type = ast.newPrimitiveType(PrimitiveType.CHAR);
charLiteral.setType(type);
return charLiteral;
}
private PrimitiveType.Code getPrimitiveTypeCode(String type) {
PrimitiveType.Code code= PrimitiveType.toCode(type);
if (code != null) {
return code;
}
if (fEnclosingElement != null && JavaModelUtil.is50OrHigher(fEnclosingElement.getJavaProject())) {
if (code == PrimitiveType.SHORT) {
if ("java.lang.Short".equals(type)) { //$NON-NLS-1$
return code;
}
} else if (code == PrimitiveType.INT) {
if ("java.lang.Integer".equals(type)) { //$NON-NLS-1$
return code;
}
} else if (code == PrimitiveType.LONG) {
if ("java.lang.Long".equals(type)) { //$NON-NLS-1$
return code;
}
} else if (code == PrimitiveType.FLOAT) {
if ("java.lang.Float".equals(type)) { //$NON-NLS-1$
return code;
}
} else if (code == PrimitiveType.DOUBLE) {
if ("java.lang.Double".equals(type)) { //$NON-NLS-1$
return code;
}
} else if (code == PrimitiveType.CHAR) {
if ("java.lang.Character".equals(type)) { //$NON-NLS-1$
return code;
}
} else if (code == PrimitiveType.BYTE) {
if ("java.lang.Byte".equals(type)) { //$NON-NLS-1$
return code;
}
}
}
return null;
}
public static boolean isGeneralizeTypeAvailable(final IJavaElement element) throws JavaModelException {
if (element != null && element.exists()) {
String type = null;
if (element instanceof IMethod) {
type = ((IMethod) element).getReturnType();
} else if (element instanceof IField) {
final IField field = (IField) element;
if (JdtFlags.isEnum(field)) {
return false;
}
type = field.getTypeSignature();
} else if (element instanceof ILocalVariable) {
return true;
} else if (element instanceof IType) {
final IType clazz = (IType) element;
if (JdtFlags.isEnum(clazz)) {
return false;
}
return true;
}
if (type == null || PrimitiveType.toCode(Signature.toString(type)) != null) {
return false;
}
return true;
}
return false;
}
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 "";
}
}
public boolean needPrimitiveCast(final Type type) {
if ((type instanceof PrimitiveType)) {
return ((Objects.equal(((PrimitiveType)type).getPrimitiveTypeCode(), PrimitiveType.CHAR) || Objects.equal(((PrimitiveType)type).getPrimitiveTypeCode(), PrimitiveType.BYTE)) ||
Objects.equal(((PrimitiveType)type).getPrimitiveTypeCode(), PrimitiveType.SHORT));
}
return false;
}
@Override
protected Type computeReturnType(AST ast, MethodDeclaration srcMethod,
MethodDeclaration dstMethod, ImportRewrite imports) {
// Use the previous async return type if valid
ITypeBinding typeBinding = dstMethod.getReturnType2().resolveBinding();
if (typeBinding != null
&& Util.VALID_ASYNC_RPC_RETURN_TYPES.contains(typeBinding.getQualifiedName())) {
return JavaASTUtils.normalizeTypeAndAddImport(ast,
dstMethod.getReturnType2(), imports);
}
return ast.newPrimitiveType(PrimitiveType.VOID);
}
public boolean visit(PrimitiveType node) {
types.add(node.toString());
if(current.getUserObject() != null) {
AnonymousClassDeclarationObject anonymous = (AnonymousClassDeclarationObject)current.getUserObject();
anonymous.getTypes().add(node.toString());
}
return false;
}
public static char buildLabelForVector(ASTNode node) {
char label = (char) node.getNodeType();
if (node instanceof Expression) {
if (node.getClass().getSimpleName().endsWith("Literal")) {
return (char) (label | (node.toString().hashCode() << 7));
}
int type = node.getNodeType();
switch (type) {
case ASTNode.INFIX_EXPRESSION:
return (char) (label | (((InfixExpression) node).getOperator().toString().hashCode() << 7));
case ASTNode.SIMPLE_NAME:
return (char) (label | (node.toString().hashCode() << 7));
case ASTNode.POSTFIX_EXPRESSION:
return (char) (label | (((PostfixExpression) node).getOperator().toString().hashCode() << 7));
case ASTNode.PREFIX_EXPRESSION:
return (char) (label | (((PrefixExpression) node).getOperator().toString().hashCode() << 7));
default:
break;
}
} else if (node instanceof Modifier) {
return (char) (label | (node.toString().hashCode() << 7));
} else if (node instanceof Type) {
if (node instanceof PrimitiveType)
return (char) (label | (node.toString().hashCode() << 7));
} else if (node instanceof TextElement) {
return (char) (label | (node.toString().hashCode() << 7));
} else if (node instanceof TagElement) {
String tag = ((TagElement) node).getTagName();
if (tag != null)
return (char) (label | (((TagElement) node).getTagName().hashCode() << 7));
}
return label;
}
public static String buildASTLabel(ASTNode node) {
String label = node.getClass().getSimpleName();
if (node instanceof Expression) {
if (node.getClass().getSimpleName().endsWith("Literal")) {
return label + "(" + node.toString() + ")";
}
int type = node.getNodeType();
switch (type) {
case ASTNode.INFIX_EXPRESSION:
return label + "(" + ((InfixExpression) node).getOperator().toString() + ")";
case ASTNode.SIMPLE_NAME:
return label + "(" + node.toString() + ")";
case ASTNode.POSTFIX_EXPRESSION:
return label + "(" + ((PostfixExpression) node).getOperator().toString() + ")";
case ASTNode.PREFIX_EXPRESSION:
return label + "(" + ((PrefixExpression) node).getOperator().toString() + ")";
default:
break;
}
} else if (node instanceof Modifier) {
return label + "(" + node.toString() + ")";
} else if (node instanceof Type) {
if (node instanceof PrimitiveType)
return label + "(" + node.toString() + ")";
} else if (node instanceof TextElement) {
return label + "(" + node.toString() + ")";
} else if (node instanceof TagElement) {
String tag = ((TagElement) node).getTagName();
if (tag == null)
return label;
return label + "(" + tag + ")";
}
return label;
}
public static boolean isVoid(Type type) {
if (type instanceof PrimitiveType) {
return ((PrimitiveType) type).getPrimitiveTypeCode().equals(PrimitiveType.VOID);
} else {
return false;
}
}
public static boolean isBoolean(Type type) {
if (type instanceof PrimitiveType) {
return ((PrimitiveType) type).getPrimitiveTypeCode().equals(PrimitiveType.BOOLEAN);
} else {
return false;
}
}
private Statement encapsulateInvocation(MethodDeclaration declaration, MethodInvocation invocation) {
final Type type= declaration.getReturnType2();
if (type == null || (type instanceof PrimitiveType && PrimitiveType.VOID.equals( ((PrimitiveType) type).getPrimitiveTypeCode())))
return invocation.getAST().newExpressionStatement(invocation);
ReturnStatement statement= invocation.getAST().newReturnStatement();
statement.setExpression(invocation);
return statement;
}
private static boolean isVoidArrayType(Type type){
if (! type.isArrayType())
return false;
ArrayType arrayType= (ArrayType)type;
if (! arrayType.getElementType().isPrimitiveType())
return false;
PrimitiveType primitiveType= (PrimitiveType) arrayType.getElementType();
return (primitiveType.getPrimitiveTypeCode() == PrimitiveType.VOID);
}
private static Type parseSuperType(String superType, boolean isInterface) {
if (! superType.trim().equals(superType)) {
return null;
}
StringBuffer cuBuff= new StringBuffer();
if (isInterface)
cuBuff.append("class __X__ implements "); //$NON-NLS-1$
else
cuBuff.append("class __X__ extends "); //$NON-NLS-1$
int offset= cuBuff.length();
cuBuff.append(superType).append(" {}"); //$NON-NLS-1$
ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
p.setSource(cuBuff.toString().toCharArray());
Map<String, String> options= new HashMap<String, String>();
JavaModelUtil.setComplianceOptions(options, JavaModelUtil.VERSION_LATEST);
p.setCompilerOptions(options);
CompilationUnit cu= (CompilationUnit) p.createAST(null);
ASTNode selected= NodeFinder.perform(cu, offset, superType.length());
if (selected instanceof Name)
selected= selected.getParent();
if (selected.getStartPosition() != offset
|| selected.getLength() != superType.length()
|| ! (selected instanceof Type)
|| selected instanceof PrimitiveType) {
return null;
}
Type type= (Type) selected;
String typeNodeRange= cuBuff.substring(type.getStartPosition(), ASTNodes.getExclusiveEnd(type));
if (! superType.equals(typeNodeRange)){
return null;
}
return type;
}
/**
* 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();
}
/**
* @return a statement in form of <code>final int maxLen = 10;</code>
*/
protected VariableDeclarationStatement createMaxLenDeclaration() {
VariableDeclarationFragment fragment= fAst.newVariableDeclarationFragment();
fragment.setName(fAst.newSimpleName(fMaxLenVariableName));
fragment.setInitializer(fAst.newNumberLiteral(String.valueOf(fContext.getLimitItemsValue())));
VariableDeclarationStatement declExpression= fAst.newVariableDeclarationStatement(fragment);
declExpression.setType(fAst.newPrimitiveType(PrimitiveType.INT));
declExpression.modifiers().add(fAst.newModifier(ModifierKeyword.FINAL_KEYWORD));
return declExpression;
}