List of usage examples for org.eclipse.jdt.internal.compiler.lookup TypeConstants UTIL
null UTIL
To view the source code for org.eclipse.jdt.internal.compiler.lookup TypeConstants UTIL.
Click Source Link
From source file:lombok.eclipse.handlers.HandleEqualsAndHashCode.java
License:Open Source License
public MethodDeclaration createHashCode(EclipseNode type, Collection<EclipseNode> fields, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; MethodDeclaration method = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); setGeneratedBy(method, source);//from ww w. j av a 2 s . c om method.modifiers = toEclipseModifier(AccessLevel.PUBLIC); method.returnType = TypeReference.baseTypeReference(TypeIds.T_int, 0); setGeneratedBy(method.returnType, source); method.annotations = new Annotation[] { makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source) }; method.selector = "hashCode".toCharArray(); method.thrownExceptions = null; method.typeParameters = null; method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; method.arguments = null; List<Statement> statements = new ArrayList<Statement>(); final boolean isEmpty = fields.isEmpty(); /* final int PRIME = X; */ { /* Without fields, PRIME isn't used, and that would trigger a 'local variable not used' warning. */ if (!isEmpty || callSuper) { LocalDeclaration primeDecl = new LocalDeclaration(PRIME, pS, pE); setGeneratedBy(primeDecl, source); primeDecl.modifiers |= Modifier.FINAL; primeDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); primeDecl.type.sourceStart = pS; primeDecl.type.sourceEnd = pE; setGeneratedBy(primeDecl.type, source); primeDecl.initialization = makeIntLiteral( String.valueOf(HandlerUtil.primeForHashcode()).toCharArray(), source); statements.add(primeDecl); } } /* int result = 1; */ { LocalDeclaration resultDecl = new LocalDeclaration(RESULT, pS, pE); setGeneratedBy(resultDecl, source); resultDecl.initialization = makeIntLiteral("1".toCharArray(), source); resultDecl.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); resultDecl.type.sourceStart = pS; resultDecl.type.sourceEnd = pE; setGeneratedBy(resultDecl.type, source); statements.add(resultDecl); } if (callSuper) { MessageSend callToSuper = new MessageSend(); setGeneratedBy(callToSuper, source); callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; callToSuper.receiver = new SuperReference(pS, pE); setGeneratedBy(callToSuper.receiver, source); callToSuper.selector = "hashCode".toCharArray(); statements.add(createResultCalculation(source, callToSuper)); } for (EclipseNode field : fields) { TypeReference fType = getFieldType(field, fieldAccess); char[] dollarFieldName = ("$" + field.getName()).toCharArray(); char[] token = fType.getLastToken(); Expression fieldAccessor = createFieldAccessor(field, fieldAccess, source); if (fType.dimensions() == 0 && token != null) { if (Arrays.equals(TypeConstants.BOOLEAN, token)) { /* booleanField ? X : Y */ IntLiteral intTrue = makeIntLiteral(String.valueOf(HandlerUtil.primeForTrue()).toCharArray(), source); IntLiteral intFalse = makeIntLiteral(String.valueOf(HandlerUtil.primeForFalse()).toCharArray(), source); ConditionalExpression intForBool = new ConditionalExpression(fieldAccessor, intTrue, intFalse); setGeneratedBy(intForBool, source); statements.add(createResultCalculation(source, intForBool)); } else if (Arrays.equals(TypeConstants.LONG, token)) { statements.add(createLocalDeclaration(source, dollarFieldName, TypeReference.baseTypeReference(TypeIds.T_long, 0), fieldAccessor)); SingleNameReference copy1 = new SingleNameReference(dollarFieldName, p); setGeneratedBy(copy1, source); SingleNameReference copy2 = new SingleNameReference(dollarFieldName, p); setGeneratedBy(copy2, source); statements.add(createResultCalculation(source, longToIntForHashCode(copy1, copy2, source))); } else if (Arrays.equals(TypeConstants.FLOAT, token)) { /* Float.floatToIntBits(fieldName) */ MessageSend floatToIntBits = new MessageSend(); floatToIntBits.sourceStart = pS; floatToIntBits.sourceEnd = pE; setGeneratedBy(floatToIntBits, source); floatToIntBits.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_FLOAT); floatToIntBits.selector = "floatToIntBits".toCharArray(); floatToIntBits.arguments = new Expression[] { fieldAccessor }; statements.add(createResultCalculation(source, floatToIntBits)); } else if (Arrays.equals(TypeConstants.DOUBLE, token)) { /* longToIntForHashCode(Double.doubleToLongBits(fieldName)) */ MessageSend doubleToLongBits = new MessageSend(); doubleToLongBits.sourceStart = pS; doubleToLongBits.sourceEnd = pE; setGeneratedBy(doubleToLongBits, source); doubleToLongBits.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA_LANG_DOUBLE); doubleToLongBits.selector = "doubleToLongBits".toCharArray(); doubleToLongBits.arguments = new Expression[] { fieldAccessor }; statements.add(createLocalDeclaration(source, dollarFieldName, TypeReference.baseTypeReference(TypeIds.T_long, 0), doubleToLongBits)); SingleNameReference copy1 = new SingleNameReference(dollarFieldName, p); setGeneratedBy(copy1, source); SingleNameReference copy2 = new SingleNameReference(dollarFieldName, p); setGeneratedBy(copy2, source); statements.add(createResultCalculation(source, longToIntForHashCode(copy1, copy2, source))); } else if (BUILT_IN_TYPES.contains(new String(token))) { statements.add(createResultCalculation(source, fieldAccessor)); } else /* objects */ { /* final java.lang.Object $fieldName = this.fieldName; */ /* $fieldName == null ? NULL_PRIME : $fieldName.hashCode() */ statements.add(createLocalDeclaration(source, dollarFieldName, generateQualifiedTypeRef(source, TypeConstants.JAVA_LANG_OBJECT), fieldAccessor)); SingleNameReference copy1 = new SingleNameReference(dollarFieldName, p); setGeneratedBy(copy1, source); SingleNameReference copy2 = new SingleNameReference(dollarFieldName, p); setGeneratedBy(copy2, source); MessageSend hashCodeCall = new MessageSend(); hashCodeCall.sourceStart = pS; hashCodeCall.sourceEnd = pE; setGeneratedBy(hashCodeCall, source); hashCodeCall.receiver = copy1; hashCodeCall.selector = "hashCode".toCharArray(); NullLiteral nullLiteral = new NullLiteral(pS, pE); setGeneratedBy(nullLiteral, source); EqualExpression objIsNull = new EqualExpression(copy2, nullLiteral, OperatorIds.EQUAL_EQUAL); setGeneratedBy(objIsNull, source); IntLiteral intMagic = makeIntLiteral(String.valueOf(HandlerUtil.primeForNull()).toCharArray(), source); ConditionalExpression nullOrHashCode = new ConditionalExpression(objIsNull, intMagic, hashCodeCall); nullOrHashCode.sourceStart = pS; nullOrHashCode.sourceEnd = pE; setGeneratedBy(nullOrHashCode, source); statements.add(createResultCalculation(source, nullOrHashCode)); } } else if (fType.dimensions() > 0 && token != null) { /* Arrays.deepHashCode(array) //just hashCode for simple arrays */ MessageSend arraysHashCodeCall = new MessageSend(); arraysHashCodeCall.sourceStart = pS; arraysHashCodeCall.sourceEnd = pE; setGeneratedBy(arraysHashCodeCall, source); arraysHashCodeCall.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray()); if (fType.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token))) { arraysHashCodeCall.selector = "deepHashCode".toCharArray(); } else { arraysHashCodeCall.selector = "hashCode".toCharArray(); } arraysHashCodeCall.arguments = new Expression[] { fieldAccessor }; statements.add(createResultCalculation(source, arraysHashCodeCall)); } } /* return result; */ { SingleNameReference resultRef = new SingleNameReference(RESULT, p); setGeneratedBy(resultRef, source); ReturnStatement returnStatement = new ReturnStatement(resultRef, pS, pE); setGeneratedBy(returnStatement, source); statements.add(returnStatement); } method.statements = statements.toArray(new Statement[statements.size()]); return method; }
From source file:lombok.eclipse.handlers.HandleEqualsAndHashCode.java
License:Open Source License
public MethodDeclaration createEquals(EclipseNode type, Collection<EclipseNode> fields, boolean callSuper, ASTNode source, FieldAccess fieldAccess, boolean needsCanEqual, List<Annotation> onParam) { int pS = source.sourceStart; int pE = source.sourceEnd; long p = (long) pS << 32 | pE; TypeDeclaration typeDecl = (TypeDeclaration) type.get(); MethodDeclaration method = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); setGeneratedBy(method, source);/*from ww w . j a v a 2 s .com*/ method.modifiers = toEclipseModifier(AccessLevel.PUBLIC); method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0); method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE; setGeneratedBy(method.returnType, source); method.annotations = new Annotation[] { makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source) }; method.selector = "equals".toCharArray(); method.thrownExceptions = null; method.typeParameters = null; method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; TypeReference objectRef = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { p, p, p }); setGeneratedBy(objectRef, source); method.arguments = new Argument[] { new Argument(new char[] { 'o' }, 0, objectRef, Modifier.FINAL) }; method.arguments[0].sourceStart = pS; method.arguments[0].sourceEnd = pE; if (!onParam.isEmpty()) method.arguments[0].annotations = onParam.toArray(new Annotation[0]); setGeneratedBy(method.arguments[0], source); List<Statement> statements = new ArrayList<Statement>(); /* if (o == this) return true; */ { SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p); setGeneratedBy(oRef, source); ThisReference thisRef = new ThisReference(pS, pE); setGeneratedBy(thisRef, source); EqualExpression otherEqualsThis = new EqualExpression(oRef, thisRef, OperatorIds.EQUAL_EQUAL); setGeneratedBy(otherEqualsThis, source); TrueLiteral trueLiteral = new TrueLiteral(pS, pE); setGeneratedBy(trueLiteral, source); ReturnStatement returnTrue = new ReturnStatement(trueLiteral, pS, pE); setGeneratedBy(returnTrue, source); IfStatement ifOtherEqualsThis = new IfStatement(otherEqualsThis, returnTrue, pS, pE); setGeneratedBy(ifOtherEqualsThis, source); statements.add(ifOtherEqualsThis); } /* if (!(o instanceof Outer.Inner.MyType) return false; */ { SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p); setGeneratedBy(oRef, source); TypeReference typeReference = createTypeReference(type, p); setGeneratedBy(typeReference, source); InstanceOfExpression instanceOf = new InstanceOfExpression(oRef, typeReference); instanceOf.sourceStart = pS; instanceOf.sourceEnd = pE; setGeneratedBy(instanceOf, source); Expression notInstanceOf = new UnaryExpression(instanceOf, OperatorIds.NOT); setGeneratedBy(notInstanceOf, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); setGeneratedBy(falseLiteral, source); ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); setGeneratedBy(returnFalse, source); IfStatement ifNotInstanceOf = new IfStatement(notInstanceOf, returnFalse, pS, pE); setGeneratedBy(ifNotInstanceOf, source); statements.add(ifNotInstanceOf); } char[] otherName = "other".toCharArray(); /* MyType<?> other = (MyType<?>) o; */ { if (!fields.isEmpty() || needsCanEqual) { LocalDeclaration other = new LocalDeclaration(otherName, pS, pE); other.modifiers |= ClassFileConstants.AccFinal; setGeneratedBy(other, source); char[] typeName = typeDecl.name; TypeReference targetType; if (typeDecl.typeParameters == null || typeDecl.typeParameters.length == 0) { targetType = new SingleTypeReference(typeName, p); setGeneratedBy(targetType, source); other.type = new SingleTypeReference(typeName, p); setGeneratedBy(other.type, source); } else { TypeReference[] typeArgs = new TypeReference[typeDecl.typeParameters.length]; for (int i = 0; i < typeArgs.length; i++) { typeArgs[i] = new Wildcard(Wildcard.UNBOUND); typeArgs[i].sourceStart = pS; typeArgs[i].sourceEnd = pE; setGeneratedBy(typeArgs[i], source); } targetType = new ParameterizedSingleTypeReference(typeName, typeArgs, 0, p); setGeneratedBy(targetType, source); other.type = new ParameterizedSingleTypeReference(typeName, copyTypes(typeArgs, source), 0, p); setGeneratedBy(other.type, source); } NameReference oRef = new SingleNameReference(new char[] { 'o' }, p); setGeneratedBy(oRef, source); other.initialization = makeCastExpression(oRef, targetType, source); statements.add(other); } } /* if (!other.canEqual((java.lang.Object) this)) return false; */ { if (needsCanEqual) { MessageSend otherCanEqual = new MessageSend(); otherCanEqual.sourceStart = pS; otherCanEqual.sourceEnd = pE; setGeneratedBy(otherCanEqual, source); otherCanEqual.receiver = new SingleNameReference(otherName, p); setGeneratedBy(otherCanEqual.receiver, source); otherCanEqual.selector = "canEqual".toCharArray(); ThisReference thisReference = new ThisReference(pS, pE); setGeneratedBy(thisReference, source); CastExpression castThisRef = makeCastExpression(thisReference, generateQualifiedTypeRef(source, TypeConstants.JAVA_LANG_OBJECT), source); castThisRef.sourceStart = pS; castThisRef.sourceEnd = pE; otherCanEqual.arguments = new Expression[] { castThisRef }; Expression notOtherCanEqual = new UnaryExpression(otherCanEqual, OperatorIds.NOT); setGeneratedBy(notOtherCanEqual, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); setGeneratedBy(falseLiteral, source); ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); setGeneratedBy(returnFalse, source); IfStatement ifNotCanEqual = new IfStatement(notOtherCanEqual, returnFalse, pS, pE); setGeneratedBy(ifNotCanEqual, source); statements.add(ifNotCanEqual); } } /* if (!super.equals(o)) return false; */ if (callSuper) { MessageSend callToSuper = new MessageSend(); callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; setGeneratedBy(callToSuper, source); callToSuper.receiver = new SuperReference(pS, pE); setGeneratedBy(callToSuper.receiver, source); callToSuper.selector = "equals".toCharArray(); SingleNameReference oRef = new SingleNameReference(new char[] { 'o' }, p); setGeneratedBy(oRef, source); callToSuper.arguments = new Expression[] { oRef }; Expression superNotEqual = new UnaryExpression(callToSuper, OperatorIds.NOT); setGeneratedBy(superNotEqual, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); setGeneratedBy(falseLiteral, source); ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); setGeneratedBy(returnFalse, source); IfStatement ifSuperEquals = new IfStatement(superNotEqual, returnFalse, pS, pE); setGeneratedBy(ifSuperEquals, source); statements.add(ifSuperEquals); } for (EclipseNode field : fields) { TypeReference fType = getFieldType(field, fieldAccess); char[] token = fType.getLastToken(); Expression thisFieldAccessor = createFieldAccessor(field, fieldAccess, source); Expression otherFieldAccessor = createFieldAccessor(field, fieldAccess, source, otherName); if (fType.dimensions() == 0 && token != null) { if (Arrays.equals(TypeConstants.FLOAT, token)) { statements.add(generateCompareFloatOrDouble(thisFieldAccessor, otherFieldAccessor, "Float".toCharArray(), source)); } else if (Arrays.equals(TypeConstants.DOUBLE, token)) { statements.add(generateCompareFloatOrDouble(thisFieldAccessor, otherFieldAccessor, "Double".toCharArray(), source)); } else if (BUILT_IN_TYPES.contains(new String(token))) { EqualExpression fieldsNotEqual = new EqualExpression(thisFieldAccessor, otherFieldAccessor, OperatorIds.NOT_EQUAL); setGeneratedBy(fieldsNotEqual, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); setGeneratedBy(falseLiteral, source); ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE); setGeneratedBy(returnStatement, source); IfStatement ifStatement = new IfStatement(fieldsNotEqual, returnStatement, pS, pE); setGeneratedBy(ifStatement, source); statements.add(ifStatement); } else /* objects */ { /* final java.lang.Object this$fieldName = this.fieldName; */ /* final java.lang.Object other$fieldName = other.fieldName; */ /* if (this$fieldName == null ? other$fieldName != null : !this$fieldName.equals(other$fieldName)) return false;; */ char[] thisDollarFieldName = ("this$" + field.getName()).toCharArray(); char[] otherDollarFieldName = ("other$" + field.getName()).toCharArray(); statements.add(createLocalDeclaration(source, thisDollarFieldName, generateQualifiedTypeRef(source, TypeConstants.JAVA_LANG_OBJECT), thisFieldAccessor)); statements.add(createLocalDeclaration(source, otherDollarFieldName, generateQualifiedTypeRef(source, TypeConstants.JAVA_LANG_OBJECT), otherFieldAccessor)); SingleNameReference this1 = new SingleNameReference(thisDollarFieldName, p); setGeneratedBy(this1, source); SingleNameReference this2 = new SingleNameReference(thisDollarFieldName, p); setGeneratedBy(this2, source); SingleNameReference other1 = new SingleNameReference(otherDollarFieldName, p); setGeneratedBy(other1, source); SingleNameReference other2 = new SingleNameReference(otherDollarFieldName, p); setGeneratedBy(other2, source); NullLiteral nullLiteral = new NullLiteral(pS, pE); setGeneratedBy(nullLiteral, source); EqualExpression fieldIsNull = new EqualExpression(this1, nullLiteral, OperatorIds.EQUAL_EQUAL); nullLiteral = new NullLiteral(pS, pE); setGeneratedBy(nullLiteral, source); EqualExpression otherFieldIsntNull = new EqualExpression(other1, nullLiteral, OperatorIds.NOT_EQUAL); MessageSend equalsCall = new MessageSend(); equalsCall.sourceStart = pS; equalsCall.sourceEnd = pE; setGeneratedBy(equalsCall, source); equalsCall.receiver = this2; equalsCall.selector = "equals".toCharArray(); equalsCall.arguments = new Expression[] { other2 }; UnaryExpression fieldsNotEqual = new UnaryExpression(equalsCall, OperatorIds.NOT); fieldsNotEqual.sourceStart = pS; fieldsNotEqual.sourceEnd = pE; setGeneratedBy(fieldsNotEqual, source); ConditionalExpression fullEquals = new ConditionalExpression(fieldIsNull, otherFieldIsntNull, fieldsNotEqual); fullEquals.sourceStart = pS; fullEquals.sourceEnd = pE; setGeneratedBy(fullEquals, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); setGeneratedBy(falseLiteral, source); ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE); setGeneratedBy(returnStatement, source); IfStatement ifStatement = new IfStatement(fullEquals, returnStatement, pS, pE); setGeneratedBy(ifStatement, source); statements.add(ifStatement); } } else if (fType.dimensions() > 0 && token != null) { MessageSend arraysEqualCall = new MessageSend(); arraysEqualCall.sourceStart = pS; arraysEqualCall.sourceEnd = pE; setGeneratedBy(arraysEqualCall, source); arraysEqualCall.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray()); if (fType.dimensions() > 1 || !BUILT_IN_TYPES.contains(new String(token))) { arraysEqualCall.selector = "deepEquals".toCharArray(); } else { arraysEqualCall.selector = "equals".toCharArray(); } arraysEqualCall.arguments = new Expression[] { thisFieldAccessor, otherFieldAccessor }; UnaryExpression arraysNotEqual = new UnaryExpression(arraysEqualCall, OperatorIds.NOT); arraysNotEqual.sourceStart = pS; arraysNotEqual.sourceEnd = pE; setGeneratedBy(arraysNotEqual, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); setGeneratedBy(falseLiteral, source); ReturnStatement returnStatement = new ReturnStatement(falseLiteral, pS, pE); setGeneratedBy(returnStatement, source); IfStatement ifStatement = new IfStatement(arraysNotEqual, returnStatement, pS, pE); setGeneratedBy(ifStatement, source); statements.add(ifStatement); } } /* return true; */ { TrueLiteral trueLiteral = new TrueLiteral(pS, pE); setGeneratedBy(trueLiteral, source); ReturnStatement returnStatement = new ReturnStatement(trueLiteral, pS, pE); setGeneratedBy(returnStatement, source); statements.add(returnStatement); } method.statements = statements.toArray(new Statement[statements.size()]); return method; }
From source file:lombok.eclipse.handlers.HandleSuperBuilder.java
License:Open Source License
private MessageSend createSetterCallWithInstanceValue(BuilderFieldData bfd, EclipseNode type, ASTNode source) { char[] setterName = bfd.name; MessageSend ms = new MessageSend(); Expression[] tgt = new Expression[bfd.singularData == null ? 1 : 2]; if (bfd.obtainVia == null || !bfd.obtainVia.field().isEmpty()) { char[] fieldName = bfd.obtainVia == null ? bfd.rawName : bfd.obtainVia.field().toCharArray(); for (int i = 0; i < tgt.length; i++) { FieldReference fr = new FieldReference(fieldName, 0); fr.receiver = new SingleNameReference(INSTANCE_VARIABLE_NAME, 0); tgt[i] = fr;/*from w w w . ja va2s .co m*/ } } else { String obtainName = bfd.obtainVia.method(); boolean obtainIsStatic = bfd.obtainVia.isStatic(); for (int i = 0; i < tgt.length; i++) { MessageSend obtainExpr = new MessageSend(); obtainExpr.receiver = obtainIsStatic ? new SingleNameReference(type.getName().toCharArray(), 0) : new SingleNameReference(INSTANCE_VARIABLE_NAME, 0); obtainExpr.selector = obtainName.toCharArray(); if (obtainIsStatic) obtainExpr.arguments = new Expression[] { new SingleNameReference(INSTANCE_VARIABLE_NAME, 0) }; tgt[i] = obtainExpr; } } if (bfd.singularData == null) { ms.arguments = tgt; } else { Expression ifNull = new EqualExpression(tgt[0], new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL); MessageSend emptyList = new MessageSend(); emptyList.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Collections".toCharArray()); emptyList.selector = EMPTY_LIST; ms.arguments = new Expression[] { new ConditionalExpression(ifNull, emptyList, tgt[1]) }; } ms.receiver = new SingleNameReference(BUILDER_VARIABLE_NAME, 0); ms.selector = setterName; return ms; }
From source file:lombok.eclipse.handlers.HandleToString.java
License:Open Source License
public static MethodDeclaration createToString(EclipseNode type, Collection<EclipseNode> fields, boolean includeFieldNames, boolean callSuper, ASTNode source, FieldAccess fieldAccess) { String typeName = getTypeName(type); char[] suffix = ")".toCharArray(); String infixS = ", "; char[] infix = infixS.toCharArray(); int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; final int PLUS = OperatorIds.PLUS; char[] prefix; if (callSuper) { prefix = (typeName + "(super=").toCharArray(); } else if (fields.isEmpty()) { prefix = (typeName + "()").toCharArray(); } else if (includeFieldNames) { prefix = (typeName + "(" + new String(((FieldDeclaration) fields.iterator().next().get()).name) + "=") .toCharArray();/*from w w w . j a v a 2 s . c o m*/ } else { prefix = (typeName + "(").toCharArray(); } boolean first = true; Expression current = new StringLiteral(prefix, pS, pE, 0); setGeneratedBy(current, source); if (callSuper) { MessageSend callToSuper = new MessageSend(); callToSuper.sourceStart = pS; callToSuper.sourceEnd = pE; setGeneratedBy(callToSuper, source); callToSuper.receiver = new SuperReference(pS, pE); setGeneratedBy(callToSuper, source); callToSuper.selector = "toString".toCharArray(); current = new BinaryExpression(current, callToSuper, PLUS); setGeneratedBy(current, source); first = false; } for (EclipseNode field : fields) { TypeReference fieldType = getFieldType(field, fieldAccess); Expression fieldAccessor = createFieldAccessor(field, fieldAccess, source); // The distinction between primitive and object will be useful if we ever add a 'hideNulls' option. boolean fieldBaseTypeIsPrimitive = BUILT_IN_TYPES.contains(new String(fieldType.getLastToken())); boolean fieldIsPrimitive = fieldType.dimensions() == 0 && fieldBaseTypeIsPrimitive; boolean fieldIsPrimitiveArray = fieldType.dimensions() == 1 && fieldBaseTypeIsPrimitive; boolean fieldIsObjectArray = fieldType.dimensions() > 0 && !fieldIsPrimitiveArray; @SuppressWarnings("unused") boolean fieldIsObject = !fieldIsPrimitive && !fieldIsPrimitiveArray && !fieldIsObjectArray; Expression ex; if (fieldIsPrimitiveArray || fieldIsObjectArray) { MessageSend arrayToString = new MessageSend(); arrayToString.sourceStart = pS; arrayToString.sourceEnd = pE; arrayToString.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.UTIL, "Arrays".toCharArray()); arrayToString.arguments = new Expression[] { fieldAccessor }; setGeneratedBy(arrayToString.arguments[0], source); arrayToString.selector = (fieldIsObjectArray ? "deepToString" : "toString").toCharArray(); ex = arrayToString; } else { ex = fieldAccessor; } setGeneratedBy(ex, source); if (first) { current = new BinaryExpression(current, ex, PLUS); current.sourceStart = pS; current.sourceEnd = pE; setGeneratedBy(current, source); first = false; continue; } StringLiteral fieldNameLiteral; if (includeFieldNames) { char[] namePlusEqualsSign = (infixS + field.getName() + "=").toCharArray(); fieldNameLiteral = new StringLiteral(namePlusEqualsSign, pS, pE, 0); } else { fieldNameLiteral = new StringLiteral(infix, pS, pE, 0); } setGeneratedBy(fieldNameLiteral, source); current = new BinaryExpression(current, fieldNameLiteral, PLUS); setGeneratedBy(current, source); current = new BinaryExpression(current, ex, PLUS); setGeneratedBy(current, source); } if (!first) { StringLiteral suffixLiteral = new StringLiteral(suffix, pS, pE, 0); setGeneratedBy(suffixLiteral, source); current = new BinaryExpression(current, suffixLiteral, PLUS); setGeneratedBy(current, source); } ReturnStatement returnStatement = new ReturnStatement(current, pS, pE); setGeneratedBy(returnStatement, source); MethodDeclaration method = new MethodDeclaration( ((CompilationUnitDeclaration) type.top().get()).compilationResult); setGeneratedBy(method, source); method.modifiers = toEclipseModifier(AccessLevel.PUBLIC); method.returnType = new QualifiedTypeReference(TypeConstants.JAVA_LANG_STRING, new long[] { p, p, p }); setGeneratedBy(method.returnType, source); method.annotations = new Annotation[] { makeMarkerAnnotation(TypeConstants.JAVA_LANG_OVERRIDE, source) }; method.arguments = null; method.selector = "toString".toCharArray(); method.thrownExceptions = null; method.typeParameters = null; method.bits |= Eclipse.ECLIPSE_DO_NOT_TOUCH_FLAG; method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; method.statements = new Statement[] { returnStatement }; return method; }
From source file:lombok.eclipse.handlers.singulars.EclipseJavaUtilSingularizer.java
License:Open Source License
protected List<Statement> createJavaUtilSimpleCreationAndFillStatements(SingularData data, EclipseNode builderType, boolean mapMode, boolean defineVar, boolean addInitialCapacityArg, boolean nullGuard, String targetType) { char[] varName = mapMode ? (new String(data.getPluralName()) + "$key").toCharArray() : data.getPluralName(); Statement createStat;//w w w . j ava 2 s . c o m { // pluralName = new java.util.TargetType(initialCap); Expression[] constructorArgs = null; if (addInitialCapacityArg) { // this.varName.size() < MAX_POWER_OF_2 ? 1 + this.varName.size() + (this.varName.size() - 3) / 3 : Integer.MAX_VALUE; // lessThanCutOff = this.varName.size() < MAX_POWER_OF_2 Expression lessThanCutoff = new BinaryExpression(getSize(builderType, varName, nullGuard), makeIntLiteral("0x40000000".toCharArray(), null), OperatorIds.LESS); FieldReference integerMaxValue = new FieldReference("MAX_VALUE".toCharArray(), 0L); integerMaxValue.receiver = new QualifiedNameReference(TypeConstants.JAVA_LANG_INTEGER, NULL_POSS, 0, 0); Expression sizeFormulaLeft = new BinaryExpression(makeIntLiteral(new char[] { '1' }, null), getSize(builderType, varName, nullGuard), OperatorIds.PLUS); Expression sizeFormulaRightLeft = new BinaryExpression(getSize(builderType, varName, nullGuard), makeIntLiteral(new char[] { '3' }, null), OperatorIds.MINUS); Expression sizeFormulaRight = new BinaryExpression(sizeFormulaRightLeft, makeIntLiteral(new char[] { '3' }, null), OperatorIds.DIVIDE); Expression sizeFormula = new BinaryExpression(sizeFormulaLeft, sizeFormulaRight, OperatorIds.PLUS); Expression cond = new ConditionalExpression(lessThanCutoff, sizeFormula, integerMaxValue); constructorArgs = new Expression[] { cond }; } TypeReference targetTypeRef = new QualifiedTypeReference( new char[][] { TypeConstants.JAVA, TypeConstants.UTIL, targetType.toCharArray() }, NULL_POSS); targetTypeRef = addTypeArgs(mapMode ? 2 : 1, false, builderType, targetTypeRef, data.getTypeArgs()); AllocationExpression constructorCall = new AllocationExpression(); constructorCall.type = targetTypeRef; constructorCall.arguments = constructorArgs; if (defineVar) { TypeReference localShadowerType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()), NULL_POSS); localShadowerType = addTypeArgs(mapMode ? 2 : 1, false, builderType, localShadowerType, data.getTypeArgs()); LocalDeclaration localShadowerDecl = new LocalDeclaration(data.getPluralName(), 0, 0); localShadowerDecl.type = localShadowerType; localShadowerDecl.initialization = constructorCall; createStat = localShadowerDecl; } else { createStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), constructorCall, 0); } } Statement fillStat; { if (mapMode) { // for (int $i = 0; $i < this.pluralname$key.size(); i++) pluralname.put(this.pluralname$key.get($i), this.pluralname$value.get($i)); char[] iVar = new char[] { '$', 'i' }; MessageSend pluralnameDotPut = new MessageSend(); pluralnameDotPut.selector = new char[] { 'p', 'u', 't' }; pluralnameDotPut.receiver = new SingleNameReference(data.getPluralName(), 0L); FieldReference thisDotKey = new FieldReference(varName, 0L); thisDotKey.receiver = new ThisReference(0, 0); FieldReference thisDotValue = new FieldReference( (new String(data.getPluralName()) + "$value").toCharArray(), 0L); thisDotValue.receiver = new ThisReference(0, 0); MessageSend keyArg = new MessageSend(); keyArg.receiver = thisDotKey; keyArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) }; keyArg.selector = new char[] { 'g', 'e', 't' }; MessageSend valueArg = new MessageSend(); valueArg.receiver = thisDotValue; valueArg.arguments = new Expression[] { new SingleNameReference(iVar, 0L) }; valueArg.selector = new char[] { 'g', 'e', 't' }; pluralnameDotPut.arguments = new Expression[] { keyArg, valueArg }; LocalDeclaration forInit = new LocalDeclaration(iVar, 0, 0); forInit.type = TypeReference.baseTypeReference(TypeIds.T_int, 0); forInit.initialization = makeIntLiteral(new char[] { '0' }, null); Expression checkExpr = new BinaryExpression(new SingleNameReference(iVar, 0L), getSize(builderType, varName, nullGuard), OperatorIds.LESS); Expression incrementExpr = new PostfixExpression(new SingleNameReference(iVar, 0L), IntLiteral.One, OperatorIds.PLUS, 0); fillStat = new ForStatement(new Statement[] { forInit }, checkExpr, new Statement[] { incrementExpr }, pluralnameDotPut, true, 0, 0); } else { // pluralname.addAll(this.pluralname); MessageSend pluralnameDotAddAll = new MessageSend(); pluralnameDotAddAll.selector = new char[] { 'a', 'd', 'd', 'A', 'l', 'l' }; pluralnameDotAddAll.receiver = new SingleNameReference(data.getPluralName(), 0L); FieldReference thisDotPluralname = new FieldReference(varName, 0L); thisDotPluralname.receiver = new ThisReference(0, 0); pluralnameDotAddAll.arguments = new Expression[] { thisDotPluralname }; fillStat = pluralnameDotAddAll; } if (nullGuard) { FieldReference thisDotField = new FieldReference(varName, 0L); thisDotField.receiver = new ThisReference(0, 0); Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL); fillStat = new IfStatement(cond, fillStat, 0, 0); } } Statement unmodifiableStat; { // pluralname = Collections.unmodifiableInterfaceType(pluralname); Expression arg = new SingleNameReference(data.getPluralName(), 0L); MessageSend invoke = new MessageSend(); invoke.arguments = new Expression[] { arg }; invoke.selector = ("unmodifiable" + data.getTargetSimpleType()).toCharArray(); invoke.receiver = new QualifiedNameReference(JAVA_UTIL_COLLECTIONS, NULL_POSS, 0, 0); unmodifiableStat = new Assignment(new SingleNameReference(data.getPluralName(), 0L), invoke, 0); } return Arrays.asList(createStat, fillStat, unmodifiableStat); }
From source file:org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.java
License:Open Source License
public void computeId() { // try to avoid multiple checks against a package/type name switch (this.compoundName.length) { case 3://from w ww . ja v a 2 s. c o m if (!CharOperation.equals(TypeConstants.JAVA, this.compoundName[0])) return; char[] packageName = this.compoundName[1]; if (packageName.length == 0) return; // just to be safe char[] typeName = this.compoundName[2]; if (typeName.length == 0) return; // just to be safe // remaining types MUST be in java.*.* if (!CharOperation.equals(TypeConstants.LANG, this.compoundName[1])) { switch (packageName[0]) { case 'i': if (CharOperation.equals(packageName, TypeConstants.IO)) { switch (typeName[0]) { case 'E': if (CharOperation.equals(typeName, TypeConstants.JAVA_IO_EXTERNALIZABLE[2])) this.id = TypeIds.T_JavaIoExternalizable; return; case 'I': if (CharOperation.equals(typeName, TypeConstants.JAVA_IO_IOEXCEPTION[2])) this.id = TypeIds.T_JavaIoException; return; case 'O': if (CharOperation.equals(typeName, TypeConstants.JAVA_IO_OBJECTSTREAMEXCEPTION[2])) this.id = TypeIds.T_JavaIoObjectStreamException; return; case 'P': if (CharOperation.equals(typeName, TypeConstants.JAVA_IO_PRINTSTREAM[2])) this.id = TypeIds.T_JavaIoPrintStream; return; case 'S': if (CharOperation.equals(typeName, TypeConstants.JAVA_IO_SERIALIZABLE[2])) this.id = TypeIds.T_JavaIoSerializable; return; } } return; case 'u': if (CharOperation.equals(packageName, TypeConstants.UTIL)) { switch (typeName[0]) { case 'C': if (CharOperation.equals(typeName, TypeConstants.JAVA_UTIL_COLLECTION[2])) this.id = TypeIds.T_JavaUtilCollection; return; case 'I': if (CharOperation.equals(typeName, TypeConstants.JAVA_UTIL_ITERATOR[2])) this.id = TypeIds.T_JavaUtilIterator; return; } } return; } return; } // remaining types MUST be in java.lang.* switch (typeName[0]) { case 'A': switch (typeName.length) { case 13: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_AUTOCLOSEABLE[2])) this.id = TypeIds.T_JavaLangAutoCloseable; return; case 14: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ASSERTIONERROR[2])) this.id = TypeIds.T_JavaLangAssertionError; return; } return; case 'B': switch (typeName.length) { case 4: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_BYTE[2])) this.id = TypeIds.T_JavaLangByte; return; case 7: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_BOOLEAN[2])) this.id = TypeIds.T_JavaLangBoolean; return; } return; case 'C': switch (typeName.length) { case 5: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_CLASS[2])) this.id = TypeIds.T_JavaLangClass; return; case 9: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_CHARACTER[2])) this.id = TypeIds.T_JavaLangCharacter; else if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_CLONEABLE[2])) this.id = TypeIds.T_JavaLangCloneable; return; case 22: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_CLASSNOTFOUNDEXCEPTION[2])) this.id = TypeIds.T_JavaLangClassNotFoundException; return; } return; case 'D': switch (typeName.length) { case 6: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_DOUBLE[2])) this.id = TypeIds.T_JavaLangDouble; return; case 10: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_DEPRECATED[2])) this.id = TypeIds.T_JavaLangDeprecated; return; } return; case 'E': switch (typeName.length) { case 4: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ENUM[2])) this.id = TypeIds.T_JavaLangEnum; return; case 5: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ERROR[2])) this.id = TypeIds.T_JavaLangError; return; case 9: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_EXCEPTION[2])) this.id = TypeIds.T_JavaLangException; return; } return; case 'F': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_FLOAT[2])) this.id = TypeIds.T_JavaLangFloat; return; case 'I': switch (typeName.length) { case 7: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_INTEGER[2])) this.id = TypeIds.T_JavaLangInteger; return; case 8: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ITERABLE[2])) this.id = TypeIds.T_JavaLangIterable; return; case 24: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ILLEGALARGUMENTEXCEPTION[2])) this.id = TypeIds.T_JavaLangIllegalArgumentException; return; } return; case 'L': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_LONG[2])) this.id = TypeIds.T_JavaLangLong; return; case 'N': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_NOCLASSDEFERROR[2])) this.id = TypeIds.T_JavaLangNoClassDefError; return; case 'O': switch (typeName.length) { case 6: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_OBJECT[2])) this.id = TypeIds.T_JavaLangObject; return; case 8: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_OVERRIDE[2])) this.id = TypeIds.T_JavaLangOverride; return; } return; case 'R': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_RUNTIMEEXCEPTION[2])) this.id = TypeIds.T_JavaLangRuntimeException; break; case 'S': switch (typeName.length) { case 5: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_SHORT[2])) this.id = TypeIds.T_JavaLangShort; return; case 6: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_STRING[2])) this.id = TypeIds.T_JavaLangString; else if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_SYSTEM[2])) this.id = TypeIds.T_JavaLangSystem; return; case 11: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_SAFEVARARGS[2])) this.id = TypeIds.T_JavaLangSafeVarargs; return; case 12: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_STRINGBUFFER[2])) this.id = TypeIds.T_JavaLangStringBuffer; return; case 13: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_STRINGBUILDER[2])) this.id = TypeIds.T_JavaLangStringBuilder; return; case 16: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_SUPPRESSWARNINGS[2])) this.id = TypeIds.T_JavaLangSuppressWarnings; return; } return; case 'T': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_THROWABLE[2])) this.id = TypeIds.T_JavaLangThrowable; return; case 'V': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_VOID[2])) this.id = TypeIds.T_JavaLangVoid; return; } break; case 4: if (!CharOperation.equals(TypeConstants.JAVA, this.compoundName[0])) return; packageName = this.compoundName[1]; if (packageName.length == 0) return; // just to be safe packageName = this.compoundName[2]; if (packageName.length == 0) return; // just to be safe typeName = this.compoundName[3]; if (typeName.length == 0) return; // just to be safe switch (packageName[0]) { case 'a': if (CharOperation.equals(packageName, TypeConstants.ANNOTATION)) { switch (typeName[0]) { case 'A': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ANNOTATION_ANNOTATION[3])) this.id = TypeIds.T_JavaLangAnnotationAnnotation; return; case 'D': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ANNOTATION_DOCUMENTED[3])) this.id = TypeIds.T_JavaLangAnnotationDocumented; return; case 'E': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ANNOTATION_ELEMENTTYPE[3])) this.id = TypeIds.T_JavaLangAnnotationElementType; return; case 'I': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ANNOTATION_INHERITED[3])) this.id = TypeIds.T_JavaLangAnnotationInherited; return; case 'R': switch (typeName.length) { case 9: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ANNOTATION_RETENTION[3])) this.id = TypeIds.T_JavaLangAnnotationRetention; return; case 15: if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ANNOTATION_RETENTIONPOLICY[3])) this.id = TypeIds.T_JavaLangAnnotationRetentionPolicy; return; } return; case 'T': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ANNOTATION_TARGET[3])) this.id = TypeIds.T_JavaLangAnnotationTarget; return; } } return; case 'i': if (CharOperation.equals(packageName, TypeConstants.INVOKE)) { if (typeName.length == 0) return; // just to be safe switch (typeName[0]) { case 'M': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_INVOKE_METHODHANDLE_$_POLYMORPHICSIGNATURE[3])) this.id = TypeIds.T_JavaLangInvokeMethodHandlePolymorphicSignature; return; } } return; case 'r': if (CharOperation.equals(packageName, TypeConstants.REFLECT)) { switch (typeName[0]) { case 'C': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_REFLECT_CONSTRUCTOR[2])) this.id = TypeIds.T_JavaLangReflectConstructor; return; case 'F': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_REFLECT_FIELD[2])) this.id = TypeIds.T_JavaLangReflectField; return; case 'M': if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_REFLECT_METHOD[2])) this.id = TypeIds.T_JavaLangReflectMethod; return; } } return; } break; case 5: if (!CharOperation.equals(TypeConstants.JAVA, this.compoundName[0])) return; packageName = this.compoundName[1]; if (packageName.length == 0) return; // just to be safe if (CharOperation.equals(TypeConstants.LANG, packageName)) { packageName = this.compoundName[2]; if (packageName.length == 0) return; // just to be safe switch (packageName[0]) { case 'i': if (CharOperation.equals(packageName, TypeConstants.INVOKE)) { typeName = this.compoundName[3]; if (typeName.length == 0) return; // just to be safe switch (typeName[0]) { case 'M': char[] memberTypeName = this.compoundName[4]; if (memberTypeName.length == 0) return; // just to be safe if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_INVOKE_METHODHANDLE_POLYMORPHICSIGNATURE[3]) && CharOperation.equals(memberTypeName, TypeConstants.JAVA_LANG_INVOKE_METHODHANDLE_POLYMORPHICSIGNATURE[4])) this.id = TypeIds.T_JavaLangInvokeMethodHandlePolymorphicSignature; return; } } return; } return; } } }