public IfStatement generateCompareFloatOrDouble(Expression thisRef, Expression otherRef, char[] floatOrDouble, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; /* if (Float.compare(fieldName, other.fieldName) != 0) return false */ MessageSend floatCompare = new MessageSend(); floatCompare.sourceStart = pS; floatCompare.sourceEnd = pE; setGeneratedBy(floatCompare, source); floatCompare.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble); floatCompare.selector = "compare".toCharArray(); floatCompare.arguments = new Expression[] {thisRef, otherRef}; IntLiteral int0 = makeIntLiteral("0".toCharArray(), source); EqualExpression ifFloatCompareIsNot0 = new EqualExpression(floatCompare, int0, OperatorIds.NOT_EQUAL); ifFloatCompareIsNot0.sourceStart = pS; ifFloatCompareIsNot0.sourceEnd = pE; setGeneratedBy(ifFloatCompareIsNot0, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); setGeneratedBy(falseLiteral, source); ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); setGeneratedBy(returnFalse, source); IfStatement ifStatement = new IfStatement(ifFloatCompareIsNot0, returnFalse, pS, pE); setGeneratedBy(ifStatement, source); return ifStatement; }
/** Give 2 clones! */ public Expression longToIntForHashCode(Expression ref1, Expression ref2, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; /* (int)(ref >>> 32 ^ ref) */ IntLiteral int32 = makeIntLiteral("32".toCharArray(), source); BinaryExpression higherBits = new BinaryExpression(ref1, int32, OperatorIds.UNSIGNED_RIGHT_SHIFT); setGeneratedBy(higherBits, source); BinaryExpression xorParts = new BinaryExpression(ref2, higherBits, OperatorIds.XOR); setGeneratedBy(xorParts, source); TypeReference intRef = TypeReference.baseTypeReference(TypeIds.T_int, 0); intRef.sourceStart = pS; intRef.sourceEnd = pE; setGeneratedBy(intRef, source); CastExpression expr = makeCastExpression(xorParts, intRef, source); expr.sourceStart = pS; expr.sourceEnd = pE; return expr; }
/** Generates 'this.<em>name</em>.size()' as an expression; if nullGuard is true, it's this.name == null ? 0 : this.name.size(). */ protected Expression getSize(EclipseNode builderType, char[] name, boolean nullGuard) { MessageSend invoke = new MessageSend(); ThisReference thisRef = new ThisReference(0, 0); FieldReference thisDotName = new FieldReference(name, 0L); thisDotName.receiver = thisRef; invoke.receiver = thisDotName; invoke.selector = SIZE_TEXT; if (!nullGuard) return invoke; ThisReference cdnThisRef = new ThisReference(0, 0); FieldReference cdnThisDotName = new FieldReference(name, 0L); cdnThisDotName.receiver = cdnThisRef; NullLiteral nullLiteral = new NullLiteral(0, 0); EqualExpression isNull = new EqualExpression(cdnThisDotName, nullLiteral, OperatorIds.EQUAL_EQUAL); IntLiteral zeroLiteral = makeIntLiteral(new char[] {'0'}, null); ConditionalExpression conditional = new ConditionalExpression(isNull, zeroLiteral, invoke); return conditional; }
public char[] returnVarNameIfNullCheck(Statement stat) { if (!(stat instanceof IfStatement)) return null; /* Check that the if's statement is a throw statement, possibly in a block. */ { Statement then = ((IfStatement) stat).thenStatement; if (then instanceof Block) { Statement[] blockStatements = ((Block) then).statements; if (blockStatements == null || blockStatements.length == 0) return null; then = blockStatements[0]; } if (!(then instanceof ThrowStatement)) return null; } /* Check that the if's conditional is like 'x == null'. Return from this method (don't generate a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ { Expression cond = ((IfStatement) stat).condition; if (!(cond instanceof EqualExpression)) return null; EqualExpression bin = (EqualExpression) cond; int operatorId = ((bin.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT); if (operatorId != OperatorIds.EQUAL_EQUAL) return null; if (!(bin.left instanceof SingleNameReference)) return null; if (!(bin.right instanceof NullLiteral)) return null; return ((SingleNameReference) bin.left).token; } }
private void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) { MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult); md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; md.modifiers = ClassFileConstants.AccPublic; FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L); thisDotField.receiver = new ThisReference(0, 0); FieldReference thisDotField2 = new FieldReference(data.getPluralName(), 0L); thisDotField2.receiver = new ThisReference(0, 0); md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray(); MessageSend clearMsg = new MessageSend(); clearMsg.receiver = thisDotField2; clearMsg.selector = "clear".toCharArray(); Statement clearStatement = new IfStatement(new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL), clearMsg, 0, 0); md.statements = returnStatement != null ? new Statement[] {clearStatement, returnStatement} : new Statement[] {clearStatement}; md.returnType = returnType; injectMethod(builderType, md); }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.PostfixExpression, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit( PostfixExpression postfixExpression, BlockScope scope) { final int numberOfParens = (postfixExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { manageOpeningParenthesizedExpression(postfixExpression, numberOfParens); } postfixExpression.lhs.traverse(this, scope); int operator = postfixExpression.operator == OperatorIds.PLUS ? TerminalTokens.TokenNamePLUS_PLUS : TerminalTokens.TokenNameMINUS_MINUS; this.scribe.printNextToken(operator, this.preferences.insert_space_before_postfix_operator); if (this.preferences.insert_space_after_postfix_operator) { this.scribe.space(); } if (numberOfParens > 0) { manageClosingParenthesizedExpression(postfixExpression, numberOfParens); } return false; }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.PrefixExpression, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit(PrefixExpression prefixExpression, BlockScope scope) { final int numberOfParens = (prefixExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { manageOpeningParenthesizedExpression(prefixExpression, numberOfParens); } int operator = prefixExpression.operator == OperatorIds.PLUS ? TerminalTokens.TokenNamePLUS_PLUS : TerminalTokens.TokenNameMINUS_MINUS; this.scribe.printNextToken(operator, this.preferences.insert_space_before_prefix_operator); if (this.preferences.insert_space_after_prefix_operator) { this.scribe.space(); } prefixExpression.lhs.traverse(this, scope); if (numberOfParens > 0) { manageClosingParenthesizedExpression(prefixExpression, numberOfParens); } return false; }
public Expression convert(org.eclipse.jdt.internal.compiler.ast.EqualExpression expression) { InfixExpression infixExpression = new InfixExpression(this.ast); if (this.resolveBindings) { recordNodes(infixExpression, expression); } Expression leftExpression = convert(expression.left); infixExpression.setLeftOperand(leftExpression); infixExpression.setRightOperand(convert(expression.right)); int startPosition = leftExpression.getStartPosition(); infixExpression.setSourceRange(startPosition, expression.sourceEnd - startPosition + 1); switch ((expression.bits & org.eclipse.jdt.internal.compiler.ast.ASTNode.OperatorMASK) >> org.eclipse.jdt.internal.compiler.ast.ASTNode.OperatorSHIFT) { case org.eclipse.jdt.internal.compiler.ast.OperatorIds.EQUAL_EQUAL : infixExpression.setOperator(InfixExpression.Operator.EQUALS); break; case org.eclipse.jdt.internal.compiler.ast.OperatorIds.NOT_EQUAL : infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS); } return infixExpression; }
public PostfixExpression convert(org.eclipse.jdt.internal.compiler.ast.PostfixExpression expression) { final PostfixExpression postfixExpression = new PostfixExpression(this.ast); if (this.resolveBindings) { recordNodes(postfixExpression, expression); } postfixExpression.setSourceRange(expression.sourceStart, expression.sourceEnd - expression.sourceStart + 1); postfixExpression.setOperand(convert(expression.lhs)); switch (expression.operator) { case org.eclipse.jdt.internal.compiler.ast.OperatorIds.PLUS : postfixExpression.setOperator(PostfixExpression.Operator.INCREMENT); break; case org.eclipse.jdt.internal.compiler.ast.OperatorIds.MINUS : postfixExpression.setOperator(PostfixExpression.Operator.DECREMENT); break; } return postfixExpression; }
public PrefixExpression convert(org.eclipse.jdt.internal.compiler.ast.PrefixExpression expression) { final PrefixExpression prefixExpression = new PrefixExpression(this.ast); if (this.resolveBindings) { recordNodes(prefixExpression, expression); } prefixExpression.setSourceRange(expression.sourceStart, expression.sourceEnd - expression.sourceStart + 1); prefixExpression.setOperand(convert(expression.lhs)); switch (expression.operator) { case org.eclipse.jdt.internal.compiler.ast.OperatorIds.PLUS : prefixExpression.setOperator(PrefixExpression.Operator.INCREMENT); break; case org.eclipse.jdt.internal.compiler.ast.OperatorIds.MINUS : prefixExpression.setOperator(PrefixExpression.Operator.DECREMENT); break; } return prefixExpression; }
public PrefixExpression convert(org.eclipse.jdt.internal.compiler.ast.UnaryExpression expression) { final PrefixExpression prefixExpression = new PrefixExpression(this.ast); if (this.resolveBindings) { this.recordNodes(prefixExpression, expression); } prefixExpression.setSourceRange(expression.sourceStart, expression.sourceEnd - expression.sourceStart + 1); prefixExpression.setOperand(convert(expression.expression)); switch ((expression.bits & org.eclipse.jdt.internal.compiler.ast.ASTNode.OperatorMASK) >> org.eclipse.jdt.internal.compiler.ast.ASTNode.OperatorSHIFT) { case org.eclipse.jdt.internal.compiler.ast.OperatorIds.PLUS : prefixExpression.setOperator(PrefixExpression.Operator.PLUS); break; case org.eclipse.jdt.internal.compiler.ast.OperatorIds.MINUS : prefixExpression.setOperator(PrefixExpression.Operator.MINUS); break; case org.eclipse.jdt.internal.compiler.ast.OperatorIds.NOT : prefixExpression.setOperator(PrefixExpression.Operator.NOT); break; case org.eclipse.jdt.internal.compiler.ast.OperatorIds.TWIDDLE : prefixExpression.setOperator(PrefixExpression.Operator.COMPLEMENT); } return prefixExpression; }
@Override public void endVisit(EqualExpression x, BlockScope scope) { JBinaryOperator op; switch ((x.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) { case OperatorIds.EQUAL_EQUAL: op = JBinaryOperator.EQ; break; case OperatorIds.NOT_EQUAL: op = JBinaryOperator.NEQ; break; default: throw translateException(x, new InternalCompilerException( "Unexpected operator for EqualExpression")); } pushBinaryOp(x, op); }
@Override public void endVisit(PostfixExpression x, BlockScope scope) { try { SourceInfo info = makeSourceInfo(x); JUnaryOperator op; switch (x.operator) { case OperatorIds.MINUS: op = JUnaryOperator.DEC; break; case OperatorIds.PLUS: op = JUnaryOperator.INC; break; default: throw new InternalCompilerException("Unexpected postfix operator"); } JExpression lhs = pop(x.lhs); push(new JPostfixOperation(info, op, lhs)); } catch (Throwable e) { throw translateException(x, e); } }
@Override public void endVisit(PrefixExpression x, BlockScope scope) { try { SourceInfo info = makeSourceInfo(x); JUnaryOperator op; switch (x.operator) { case OperatorIds.MINUS: op = JUnaryOperator.DEC; break; case OperatorIds.PLUS: op = JUnaryOperator.INC; break; default: throw new InternalCompilerException("Unexpected prefix operator"); } JExpression lhs = pop(x.lhs); push(new JPrefixOperation(info, op, lhs)); } catch (Throwable e) { throw translateException(x, e); } }
/** * Generates a new statement that checks if the given variable is null, and if so, throws a {@code NullPointerException} with the * variable name as message. */ public static Statement generateNullCheck(AbstractVariableDeclaration variable, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; if (isPrimitive(variable.type)) return null; AllocationExpression exception = new AllocationExpression(); setGeneratedBy(exception, source); exception.type = new QualifiedTypeReference(fromQualifiedName("java.lang.NullPointerException"), new long[]{p, p, p}); setGeneratedBy(exception.type, source); exception.arguments = new Expression[] { new StringLiteral(variable.name, pS, pE, 0)}; setGeneratedBy(exception.arguments[0], source); ThrowStatement throwStatement = new ThrowStatement(exception, pS, pE); setGeneratedBy(throwStatement, source); SingleNameReference varName = new SingleNameReference(variable.name, p); setGeneratedBy(varName, source); NullLiteral nullLiteral = new NullLiteral(pS, pE); setGeneratedBy(nullLiteral, source); EqualExpression equalExpression = new EqualExpression(varName, nullLiteral, OperatorIds.EQUAL_EQUAL); equalExpression.sourceStart = pS; equalExpression.statementEnd = equalExpression.sourceEnd = pE; setGeneratedBy(equalExpression, source); IfStatement ifStatement = new IfStatement(equalExpression, throwStatement, 0, 0); setGeneratedBy(ifStatement, source); return ifStatement; }
private IfStatement generateCompareFloatOrDouble(Expression thisRef, Expression otherRef, char[] floatOrDouble, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; /* if (Float.compare(fieldName, other.fieldName) != 0) return false */ MessageSend floatCompare = new MessageSend(); floatCompare.sourceStart = pS; floatCompare.sourceEnd = pE; setGeneratedBy(floatCompare, source); floatCompare.receiver = generateQualifiedNameRef(source, TypeConstants.JAVA, TypeConstants.LANG, floatOrDouble); floatCompare.selector = "compare".toCharArray(); floatCompare.arguments = new Expression[] {thisRef, otherRef}; IntLiteral int0 = makeIntLiteral("0".toCharArray(), source); EqualExpression ifFloatCompareIsNot0 = new EqualExpression(floatCompare, int0, OperatorIds.NOT_EQUAL); ifFloatCompareIsNot0.sourceStart = pS; ifFloatCompareIsNot0.sourceEnd = pE; setGeneratedBy(ifFloatCompareIsNot0, source); FalseLiteral falseLiteral = new FalseLiteral(pS, pE); setGeneratedBy(falseLiteral, source); ReturnStatement returnFalse = new ReturnStatement(falseLiteral, pS, pE); setGeneratedBy(returnFalse, source); IfStatement ifStatement = new IfStatement(ifFloatCompareIsNot0, returnFalse, pS, pE); setGeneratedBy(ifStatement, source); return ifStatement; }
/** Give 2 clones! */ private Expression longToIntForHashCode(Expression ref1, Expression ref2, ASTNode source) { int pS = source.sourceStart, pE = source.sourceEnd; /* (int)(ref >>> 32 ^ ref) */ IntLiteral int32 = makeIntLiteral("32".toCharArray(), source); BinaryExpression higherBits = new BinaryExpression(ref1, int32, OperatorIds.UNSIGNED_RIGHT_SHIFT); setGeneratedBy(higherBits, source); BinaryExpression xorParts = new BinaryExpression(ref2, higherBits, OperatorIds.XOR); setGeneratedBy(xorParts, source); TypeReference intRef = TypeReference.baseTypeReference(TypeIds.T_int, 0); intRef.sourceStart = pS; intRef.sourceEnd = pE; setGeneratedBy(intRef, source); CastExpression expr = makeCastExpression(xorParts, intRef, source); expr.sourceStart = pS; expr.sourceEnd = pE; return expr; }
protected Statement createConstructBuilderVarIfNeeded(SingularData data, EclipseNode builderType) { FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L); thisDotField.receiver = new ThisReference(0, 0); FieldReference thisDotField2 = new FieldReference(data.getPluralName(), 0L); thisDotField2.receiver = new ThisReference(0, 0); Expression cond = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL); MessageSend createBuilderInvoke = new MessageSend(); char[][] tokenizedName = makeGuavaTypeName(getSimpleTargetTypeName(data), false); createBuilderInvoke.receiver = new QualifiedNameReference(tokenizedName, NULL_POSS, 0, 0); createBuilderInvoke.selector = getBuilderMethodName(data); return new IfStatement(cond, new Assignment(thisDotField2, createBuilderInvoke, 0), 0, 0); }
private void generateClearMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType) { MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult); md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; md.modifiers = ClassFileConstants.AccPublic; String pN = new String(data.getPluralName()); char[] keyFieldName = (pN + "$key").toCharArray(); char[] valueFieldName = (pN + "$value").toCharArray(); FieldReference thisDotField = new FieldReference(keyFieldName, 0L); thisDotField.receiver = new ThisReference(0, 0); FieldReference thisDotField2 = new FieldReference(keyFieldName, 0L); thisDotField2.receiver = new ThisReference(0, 0); FieldReference thisDotField3 = new FieldReference(valueFieldName, 0L); thisDotField3.receiver = new ThisReference(0, 0); md.selector = HandlerUtil.buildAccessorName("clear", new String(data.getPluralName())).toCharArray(); MessageSend clearMsg1 = new MessageSend(); clearMsg1.receiver = thisDotField2; clearMsg1.selector = "clear".toCharArray(); MessageSend clearMsg2 = new MessageSend(); clearMsg2.receiver = thisDotField3; clearMsg2.selector = "clear".toCharArray(); Block clearMsgs = new Block(2); clearMsgs.statements = new Statement[] {clearMsg1, clearMsg2}; Statement clearStatement = new IfStatement(new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.NOT_EQUAL), clearMsgs, 0, 0); md.statements = returnStatement != null ? new Statement[] {clearStatement, returnStatement} : new Statement[] {clearStatement}; md.returnType = returnType; injectMethod(builderType, md); }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.BinaryExpression, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit(BinaryExpression binaryExpression, BlockScope scope) { switch((binaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) { case OperatorIds.AND : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameAND, scope); case OperatorIds.DIVIDE : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameDIVIDE, scope); case OperatorIds.GREATER : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameGREATER, scope); case OperatorIds.GREATER_EQUAL : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameGREATER_EQUAL, scope); case OperatorIds.LEFT_SHIFT : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameLEFT_SHIFT, scope); case OperatorIds.LESS : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameLESS, scope); case OperatorIds.LESS_EQUAL : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameLESS_EQUAL, scope); case OperatorIds.MINUS : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameMINUS, scope); case OperatorIds.MULTIPLY : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameMULTIPLY, scope); case OperatorIds.OR : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameOR, scope); case OperatorIds.PLUS : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNamePLUS, scope); case OperatorIds.REMAINDER : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameREMAINDER, scope); case OperatorIds.RIGHT_SHIFT : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameRIGHT_SHIFT, scope); case OperatorIds.UNSIGNED_RIGHT_SHIFT : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameUNSIGNED_RIGHT_SHIFT, scope); case OperatorIds.XOR : return dumpBinaryExpression(binaryExpression, TerminalTokens.TokenNameXOR, scope); default: throw new IllegalStateException(); } }
/** * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.EqualExpression, org.eclipse.jdt.internal.compiler.lookup.BlockScope) */ public boolean visit(EqualExpression equalExpression, BlockScope scope) { if ((equalExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT == OperatorIds.EQUAL_EQUAL) { return dumpEqualityExpression(equalExpression, TerminalTokens.TokenNameEQUAL_EQUAL, scope); } else { return dumpEqualityExpression(equalExpression, TerminalTokens.TokenNameNOT_EQUAL, scope); } }
@Override public void endVisit(UnaryExpression x, BlockScope scope) { try { SourceInfo info = makeSourceInfo(x); JUnaryOperator op; int operator = ((x.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT); switch (operator) { case OperatorIds.MINUS: op = JUnaryOperator.NEG; break; case OperatorIds.NOT: op = JUnaryOperator.NOT; break; case OperatorIds.PLUS: // Odd case.. useless + operator; just leave the operand on the // stack. return; case OperatorIds.TWIDDLE: op = JUnaryOperator.BIT_NOT; break; default: throw new InternalCompilerException("Unexpected operator for unary expression"); } JExpression expression = pop(x.expression); push(new JPrefixOperation(info, op, expression)); } catch (Throwable e) { throw translateException(x, e); } }
/** * Generates a new statement that checks if the given variable is null, and if so, throws a specified exception with the * variable name as message. * * @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}. */ public static Statement generateNullCheck(AbstractVariableDeclaration variable, EclipseNode sourceNode) { NullCheckExceptionType exceptionType = sourceNode.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE); if (exceptionType == null) exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION; ASTNode source = sourceNode.get(); int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; if (isPrimitive(variable.type)) return null; AllocationExpression exception = new AllocationExpression(); setGeneratedBy(exception, source); int partCount = 1; String exceptionTypeStr = exceptionType.getExceptionType(); for (int i = 0; i < exceptionTypeStr.length(); i++) if (exceptionTypeStr.charAt(i) == '.') partCount++; long[] ps = new long[partCount]; Arrays.fill(ps, 0L); exception.type = new QualifiedTypeReference(fromQualifiedName(exceptionTypeStr), ps); setGeneratedBy(exception.type, source); exception.arguments = new Expression[] { new StringLiteral(exceptionType.toExceptionMessage(new String(variable.name)).toCharArray(), pS, pE, 0) }; setGeneratedBy(exception.arguments[0], source); ThrowStatement throwStatement = new ThrowStatement(exception, pS, pE); setGeneratedBy(throwStatement, source); SingleNameReference varName = new SingleNameReference(variable.name, p); setGeneratedBy(varName, source); NullLiteral nullLiteral = new NullLiteral(pS, pE); setGeneratedBy(nullLiteral, source); EqualExpression equalExpression = new EqualExpression(varName, nullLiteral, OperatorIds.EQUAL_EQUAL); equalExpression.sourceStart = pS; equalExpression.statementEnd = equalExpression.sourceEnd = pE; setGeneratedBy(equalExpression, source); Block throwBlock = new Block(0); throwBlock.statements = new Statement[] {throwStatement}; throwBlock.sourceStart = pS; throwBlock.sourceEnd = pE; setGeneratedBy(throwBlock, source); IfStatement ifStatement = new IfStatement(equalExpression, throwBlock, 0, 0); setGeneratedBy(ifStatement, source); return ifStatement; }
public MethodDeclaration createWither(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam, boolean makeAbstract ) { ASTNode source = sourceNode.get(); if (name == null) return null; FieldDeclaration field = (FieldDeclaration) fieldNode.get(); int pS = source.sourceStart, pE = source.sourceEnd; long p = (long) pS << 32 | pE; MethodDeclaration method = new MethodDeclaration(parent.compilationResult); if (makeAbstract) modifier = modifier | ClassFileConstants.AccAbstract | ExtraCompilerModifiers.AccSemicolonBody; method.modifiers = modifier; method.returnType = cloneSelfType(fieldNode, source); if (method.returnType == null) return null; Annotation[] deprecated = null; if (isFieldDeprecated(fieldNode)) { deprecated = new Annotation[] { generateDeprecatedAnnotation(source) }; } method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), deprecated); Argument param = new Argument(field.name, p, copyType(field.type, source), ClassFileConstants.AccFinal); param.sourceStart = pS; param.sourceEnd = pE; method.arguments = new Argument[] { param }; method.selector = name.toCharArray(); method.binding = null; method.thrownExceptions = null; method.typeParameters = null; method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); if (!makeAbstract) { List<Expression> args = new ArrayList<Expression>(); for (EclipseNode child : fieldNode.up().down()) { if (child.getKind() != Kind.FIELD) continue; FieldDeclaration childDecl = (FieldDeclaration) child.get(); // Skip fields that start with $ if (childDecl.name != null && childDecl.name.length > 0 && childDecl.name[0] == '$') continue; long fieldFlags = childDecl.modifiers; // Skip static fields. if ((fieldFlags & ClassFileConstants.AccStatic) != 0) continue; // Skip initialized final fields. if (((fieldFlags & ClassFileConstants.AccFinal) != 0) && childDecl.initialization != null) continue; if (child.get() == fieldNode.get()) { args.add(new SingleNameReference(field.name, p)); } else { args.add(createFieldAccessor(child, FieldAccess.ALWAYS_FIELD, source)); } } AllocationExpression constructorCall = new AllocationExpression(); constructorCall.arguments = args.toArray(new Expression[0]); constructorCall.type = cloneSelfType(fieldNode, source); Expression identityCheck = new EqualExpression( createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source), new SingleNameReference(field.name, p), OperatorIds.EQUAL_EQUAL); ThisReference thisRef = new ThisReference(pS, pE); Expression conditional = new ConditionalExpression(identityCheck, thisRef, constructorCall); Statement returnStatement = new ReturnStatement(conditional, pS, pE); method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; List<Statement> statements = new ArrayList<Statement>(5); if (nonNulls.length > 0) { Statement nullCheck = generateNullCheck(field, sourceNode); if (nullCheck != null) statements.add(nullCheck); } statements.add(returnStatement); method.statements = statements.toArray(new Statement[0]); } param.annotations = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0])); method.traverse(new SetGeneratedByVisitor(source), parent.scope); return method; }
public MethodDeclaration createWither(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam) { ASTNode source = sourceNode.get(); if (name == null) return null; FieldDeclaration field = (FieldDeclaration) fieldNode.get(); int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; MethodDeclaration method = new MethodDeclaration(parent.compilationResult); method.modifiers = modifier; method.returnType = cloneSelfType(fieldNode, source); if (method.returnType == null) return null; Annotation[] deprecated = null; if (isFieldDeprecated(fieldNode)) { deprecated = new Annotation[] { generateDeprecatedAnnotation(source) }; } method.annotations = copyAnnotations(source, onMethod.toArray(new Annotation[0]), deprecated); Argument param = new Argument(field.name, p, copyType(field.type, source), Modifier.FINAL); param.sourceStart = pS; param.sourceEnd = pE; method.arguments = new Argument[] { param }; method.selector = name.toCharArray(); method.binding = null; method.thrownExceptions = null; method.typeParameters = null; method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; List<Expression> args = new ArrayList<Expression>(); for (EclipseNode child : fieldNode.up().down()) { if (child.getKind() != Kind.FIELD) continue; FieldDeclaration childDecl = (FieldDeclaration) child.get(); // Skip fields that start with $ if (childDecl.name != null && childDecl.name.length > 0 && childDecl.name[0] == '$') continue; long fieldFlags = childDecl.modifiers; // Skip static fields. if ((fieldFlags & ClassFileConstants.AccStatic) != 0) continue; // Skip initialized final fields. if (((fieldFlags & ClassFileConstants.AccFinal) != 0) && childDecl.initialization != null) continue; if (child.get() == fieldNode.get()) { args.add(new SingleNameReference(field.name, p)); } else { args.add(createFieldAccessor(child, FieldAccess.ALWAYS_FIELD, source)); } } AllocationExpression constructorCall = new AllocationExpression(); constructorCall.arguments = args.toArray(new Expression[0]); constructorCall.type = cloneSelfType(fieldNode, source); Expression identityCheck = new EqualExpression( createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source), new SingleNameReference(field.name, p), OperatorIds.EQUAL_EQUAL); ThisReference thisRef = new ThisReference(pS, pE); Expression conditional = new ConditionalExpression(identityCheck, thisRef, constructorCall); Statement returnStatement = new ReturnStatement(conditional, pS, pE); method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart; method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd; Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN); List<Statement> statements = new ArrayList<Statement>(5); if (nonNulls.length > 0) { Statement nullCheck = generateNullCheck(field, sourceNode); if (nullCheck != null) statements.add(nullCheck); } statements.add(returnStatement); method.statements = statements.toArray(new Statement[0]); param.annotations = copyAnnotations(source, nonNulls, nullables, onParam.toArray(new Annotation[0])); method.traverse(new SetGeneratedByVisitor(source), parent.scope); return method; }
public boolean visit(BinaryExpression binaryExpression, BlockScope scope) { if (binaryExpression instanceof CombinedBinaryExpression) { CombinedBinaryExpression expression = (CombinedBinaryExpression) binaryExpression; if (expression.referencesTable != null) { return this.visit(expression, scope); } } final int numberOfParens = (binaryExpression.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT; if (numberOfParens > 0) { addRealFragment(binaryExpression); } else { switch((binaryExpression.bits & ASTNode.OperatorMASK) >> ASTNode.OperatorSHIFT) { case OperatorIds.PLUS : if (buildFragments(binaryExpression)) { binaryExpression.left.traverse(this, scope); this.operatorsList.add(new Integer(TerminalTokens.TokenNamePLUS)); binaryExpression.right.traverse(this, scope); } return false; case OperatorIds.MINUS : if (buildFragments(binaryExpression)) { binaryExpression.left.traverse(this, scope); this.operatorsList.add(new Integer(TerminalTokens.TokenNameMINUS)); binaryExpression.right.traverse(this, scope); } return false; case OperatorIds.MULTIPLY : if (buildFragments(binaryExpression)) { binaryExpression.left.traverse(this, scope); this.operatorsList.add(new Integer(TerminalTokens.TokenNameMULTIPLY)); binaryExpression.right.traverse(this, scope); } return false; case OperatorIds.REMAINDER : if (buildFragments(binaryExpression)) { binaryExpression.left.traverse(this, scope); this.operatorsList.add(new Integer(TerminalTokens.TokenNameREMAINDER)); binaryExpression.right.traverse(this, scope); } return false; case OperatorIds.XOR : if (buildFragments(binaryExpression)) { binaryExpression.left.traverse(this, scope); this.operatorsList.add(new Integer(TerminalTokens.TokenNameXOR)); binaryExpression.right.traverse(this, scope); } return false; case OperatorIds.DIVIDE : if (buildFragments(binaryExpression)) { binaryExpression.left.traverse(this, scope); this.operatorsList.add(new Integer(TerminalTokens.TokenNameDIVIDE)); binaryExpression.right.traverse(this, scope); } return false; case OperatorIds.OR : if (buildFragments(binaryExpression)) { binaryExpression.left.traverse(this, scope); this.operatorsList.add(new Integer(TerminalTokens.TokenNameOR)); binaryExpression.right.traverse(this, scope); } return false; case OperatorIds.AND : if (buildFragments(binaryExpression)) { binaryExpression.left.traverse(this, scope); this.operatorsList.add(new Integer(TerminalTokens.TokenNameAND)); binaryExpression.right.traverse(this, scope); } return false; default: addRealFragment(binaryExpression); } } return false; }