org.eclipse.jdt.internal.compiler.ast.ReturnStatement Java Examples

The following examples show how to use org.eclipse.jdt.internal.compiler.ast.ReturnStatement. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HandleBuilder.java    From EasyMPermission with MIT License 6 votes vote down vote up
public MethodDeclaration generateBuilderMethod(String builderMethodName, String builderClassName, EclipseNode type, TypeParameter[] typeParams, ASTNode source) {
	int pS = source.sourceStart, pE = source.sourceEnd;
	long p = (long) pS << 32 | pE;
	
	MethodDeclaration out = new MethodDeclaration(
			((CompilationUnitDeclaration) type.top().get()).compilationResult);
	out.selector = builderMethodName.toCharArray();
	out.modifiers = ClassFileConstants.AccPublic | ClassFileConstants.AccStatic;
	out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	out.returnType = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p);
	out.typeParameters = copyTypeParams(typeParams, source);
	AllocationExpression invoke = new AllocationExpression();
	invoke.type = namePlusTypeParamsToTypeReference(builderClassName.toCharArray(), typeParams, p);
	out.statements = new Statement[] {new ReturnStatement(invoke, pS, pE)};
	
	out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) type.get()).scope);
	return out;
}
 
Example #2
Source File: HandleEqualsAndHashCode.java    From EasyMPermission with MIT License 6 votes vote down vote up
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;
}
 
Example #3
Source File: SingletonEclipseHandler.java    From tutorials with MIT License 6 votes vote down vote up
private void addFactoryMethod(EclipseNode singletonClass, TypeDeclaration astNode, TypeReference typeReference, TypeDeclaration innerClass, FieldDeclaration field) {
    MethodDeclaration factoryMethod = new MethodDeclaration(astNode.compilationResult);
    factoryMethod.modifiers = AccStatic | ClassFileConstants.AccPublic;
    factoryMethod.returnType = typeReference;
    factoryMethod.sourceStart = astNode.sourceStart;
    factoryMethod.sourceEnd = astNode.sourceEnd;
    factoryMethod.selector = "getInstance".toCharArray();
    factoryMethod.bits = ECLIPSE_DO_NOT_TOUCH_FLAG;

    long pS = factoryMethod.sourceStart;
    long pE = factoryMethod.sourceEnd;
    long p = (long) pS << 32 | pE;

    FieldReference ref = new FieldReference(field.name, p);
    ref.receiver = new SingleNameReference(innerClass.name, p);

    ReturnStatement statement = new ReturnStatement(ref, astNode.sourceStart, astNode.sourceEnd);

    factoryMethod.statements = new Statement[]{statement};

    EclipseHandlerUtil.injectMethod(singletonClass, factoryMethod);
}
 
Example #4
Source File: EclipseGuavaSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void generateMethods(SingularData data, EclipseNode builderType, boolean fluent, boolean chain) {
	TypeReference returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
	Statement returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
	generateSingularMethod(returnType, returnStatement, data, builderType, fluent);
	
	returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
	returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
	generatePluralMethod(returnType, returnStatement, data, builderType, fluent);
}
 
Example #5
Source File: EclipseJavaUtilListSetSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void generateMethods(SingularData data, EclipseNode builderType, boolean fluent, boolean chain) {
	if (useGuavaInstead(builderType)) {
		guavaListSetSingularizer.generateMethods(data, builderType, fluent, chain);
		return;
	}
	
	TypeReference returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
	Statement returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
	generateSingularMethod(returnType, returnStatement, data, builderType, fluent);
	
	returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
	returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
	generatePluralMethod(returnType, returnStatement, data, builderType, fluent);
}
 
Example #6
Source File: EclipseJavaUtilMapSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void generateMethods(SingularData data, EclipseNode builderType, boolean fluent, boolean chain) {
	if (useGuavaInstead(builderType)) {
		guavaMapSingularizer.generateMethods(data, builderType, fluent, chain);
		return;
	}
	
	TypeReference returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
	Statement returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
	generateSingularMethod(returnType, returnStatement, data, builderType, fluent);
	
	returnType = chain ? cloneSelfType(builderType) : TypeReference.baseTypeReference(TypeIds.T_void, 0);
	returnStatement = chain ? new ReturnStatement(new ThisReference(0, 0), 0, 0) : null;
	generatePluralMethod(returnType, returnStatement, data, builderType, fluent);
}
 
Example #7
Source File: HandleConstructor.java    From EasyMPermission with MIT License 4 votes vote down vote up
public MethodDeclaration createStaticConstructor(AccessLevel level, String name, EclipseNode type, Collection<EclipseNode> fields, ASTNode source) {
	int pS = source.sourceStart, pE = source.sourceEnd;
	long p = (long)pS << 32 | pE;
	
	MethodDeclaration constructor = new MethodDeclaration(
			((CompilationUnitDeclaration) type.top().get()).compilationResult);
	
	constructor.modifiers = toEclipseModifier(level) | ClassFileConstants.AccStatic;
	TypeDeclaration typeDecl = (TypeDeclaration) type.get();
	constructor.returnType = EclipseHandlerUtil.namePlusTypeParamsToTypeReference(typeDecl.name, typeDecl.typeParameters, p);
	constructor.annotations = null;
	constructor.selector = name.toCharArray();
	constructor.thrownExceptions = null;
	constructor.typeParameters = copyTypeParams(((TypeDeclaration)type.get()).typeParameters, source);
	constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
	constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
	
	List<Argument> params = new ArrayList<Argument>();
	List<Expression> assigns = new ArrayList<Expression>();
	AllocationExpression statement = new AllocationExpression();
	statement.sourceStart = pS; statement.sourceEnd = pE;
	statement.type = copyType(constructor.returnType, source);
	
	for (EclipseNode fieldNode : fields) {
		FieldDeclaration field = (FieldDeclaration) fieldNode.get();
		long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd;
		SingleNameReference nameRef = new SingleNameReference(field.name, fieldPos);
		assigns.add(nameRef);
		
		Argument parameter = new Argument(field.name, fieldPos, copyType(field.type, source), Modifier.FINAL);
		parameter.annotations = copyAnnotations(source, findAnnotations(field, NON_NULL_PATTERN), findAnnotations(field, NULLABLE_PATTERN));
		params.add(parameter);
	}
	
	statement.arguments = assigns.isEmpty() ? null : assigns.toArray(new Expression[assigns.size()]);
	constructor.arguments = params.isEmpty() ? null : params.toArray(new Argument[params.size()]);
	constructor.statements = new Statement[] { new ReturnStatement(statement, (int)(p >> 32), (int)p) };
	
	constructor.traverse(new SetGeneratedByVisitor(source), typeDecl.scope);
	return constructor;
}
 
Example #8
Source File: HandleGetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
public Statement[] createSimpleGetterBody(ASTNode source, EclipseNode fieldNode) {
	FieldDeclaration field = (FieldDeclaration) fieldNode.get();
	Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
	Statement returnStatement = new ReturnStatement(fieldRef, field.sourceStart, field.sourceEnd);
	return new Statement[] {returnStatement};
}
 
Example #9
Source File: HandleSetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
static MethodDeclaration createSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, boolean shouldReturnThis, int modifier, EclipseNode sourceNode, List<Annotation> onMethod, List<Annotation> onParam) {
	FieldDeclaration field = (FieldDeclaration) fieldNode.get();
	ASTNode source = sourceNode.get();
	int pS = source.sourceStart, pE = source.sourceEnd;
	long p = (long)pS << 32 | pE;
	MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
	method.modifiers = modifier;
	if (shouldReturnThis) {
		method.returnType = cloneSelfType(fieldNode, source);
	}
	
	if (method.returnType == null) {
		method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
		method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE;
		shouldReturnThis = false;
	}
	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;
	Expression fieldRef = createFieldAccessor(fieldNode, FieldAccess.ALWAYS_FIELD, source);
	NameReference fieldNameRef = new SingleNameReference(field.name, p);
	Assignment assignment = new Assignment(fieldRef, fieldNameRef, (int)p);
	assignment.sourceStart = pS; assignment.sourceEnd = assignment.statementEnd = 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) {
		statements.add(assignment);
	} else {
		Statement nullCheck = generateNullCheck(field, sourceNode);
		if (nullCheck != null) statements.add(nullCheck);
		statements.add(assignment);
	}
	
	if (shouldReturnThis) {
		ThisReference thisRef = new ThisReference(pS, pE);
		ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE);
		statements.add(returnThis);
	}
	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;
}
 
Example #10
Source File: HandleWither.java    From EasyMPermission with MIT License 4 votes vote down vote up
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;
}
 
Example #11
Source File: HandleEqualsAndHashCode.java    From EasyMPermission with MIT License 4 votes vote down vote up
public MethodDeclaration createCanEqual(EclipseNode type, ASTNode source, List<Annotation> onParam) {
	/* public boolean canEqual(final java.lang.Object other) {
	 *     return other instanceof Outer.Inner.MyType;
	 * }
	 */
	int pS = source.sourceStart; int pE = source.sourceEnd;
	long p = (long)pS << 32 | pE;
	
	char[] otherName = "other".toCharArray();
	
	MethodDeclaration method = new MethodDeclaration(
			((CompilationUnitDeclaration) type.top().get()).compilationResult);
	setGeneratedBy(method, source);
	method.modifiers = toEclipseModifier(AccessLevel.PROTECTED);
	method.returnType = TypeReference.baseTypeReference(TypeIds.T_boolean, 0);
	method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE;
	setGeneratedBy(method.returnType, source);
	method.selector = "canEqual".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(otherName, 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);
	
	SingleNameReference otherRef = new SingleNameReference(otherName, p);
	setGeneratedBy(otherRef, source);
	
	TypeReference typeReference = createTypeReference(type, p);
	setGeneratedBy(typeReference, source);
	
	InstanceOfExpression instanceOf = new InstanceOfExpression(otherRef, typeReference);
	instanceOf.sourceStart = pS; instanceOf.sourceEnd = pE;
	setGeneratedBy(instanceOf, source);
	
	ReturnStatement returnStatement = new ReturnStatement(instanceOf, pS, pE);
	setGeneratedBy(returnStatement, source);
	
	method.statements = new Statement[] {returnStatement};
	return method;
}
 
Example #12
Source File: SetGeneratedByVisitor.java    From EasyMPermission with MIT License 4 votes vote down vote up
@Override public boolean visit(ReturnStatement node, BlockScope scope) {
	fixPositions(setGeneratedBy(node, source));
	return super.visit(node, scope);
}
 
Example #13
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void buildMoreCompletionContext(Expression expression) {
	ASTNode parentNode = null;

	int kind = topKnownElementKind(SELECTION_OR_ASSIST_PARSER);
	if(kind != 0) {
		int info = topKnownElementInfo(SELECTION_OR_ASSIST_PARSER);
		nextElement : switch (kind) {
			case K_BETWEEN_CASE_AND_COLON :
				if(this.expressionPtr > 0) {
					SwitchStatement switchStatement = new SwitchStatement();
					switchStatement.expression = this.expressionStack[this.expressionPtr - 1];
					if(this.astLengthPtr > -1 && this.astPtr > -1) {
						int length = this.astLengthStack[this.astLengthPtr];
						int newAstPtr = this.astPtr - length;
						ASTNode firstNode = this.astStack[newAstPtr + 1];
						if(length != 0 && firstNode.sourceStart > switchStatement.expression.sourceEnd) {
							switchStatement.statements = new Statement[length + 1];
							System.arraycopy(
								this.astStack,
								newAstPtr + 1,
								switchStatement.statements,
								0,
								length);
						}
					}
					CaseStatement caseStatement = new CaseStatement(expression, expression.sourceStart, expression.sourceEnd);
					if(switchStatement.statements == null) {
						switchStatement.statements = new Statement[]{caseStatement};
					} else {
						switchStatement.statements[switchStatement.statements.length - 1] = caseStatement;
					}
					parentNode = switchStatement;
					this.assistNodeParent = parentNode;
				}
				break nextElement;
			case K_INSIDE_RETURN_STATEMENT :
				if(info == this.bracketDepth) {
					ReturnStatement returnStatement = new ReturnStatement(expression, expression.sourceStart, expression.sourceEnd);
					parentNode = returnStatement;
					this.assistNodeParent = parentNode;
				}
				break nextElement;
			case K_CAST_STATEMENT :
				Expression castType;
				if(this.expressionPtr > 0
					&& ((castType = this.expressionStack[this.expressionPtr-1]) instanceof TypeReference)) {
					CastExpression cast = new CastExpression(expression, (TypeReference) castType);
					cast.sourceStart = castType.sourceStart;
					cast.sourceEnd= expression.sourceEnd;
					parentNode = cast;
					this.assistNodeParent = parentNode;
				}
				break nextElement;
		}
	}
	// Do not add assist node/parent into the recovery system if we are inside a lambda. The lambda will be fully recovered including the containing statement and added.
	if (lastIndexOfElement(K_LAMBDA_EXPRESSION_DELIMITER) < 0) {
		if(parentNode != null) {
			this.currentElement = this.currentElement.add((Statement)parentNode, 0);
		} else {
			this.currentElement = this.currentElement.add((Statement)wrapWithExplicitConstructorCallIfNeeded(expression), 0);
			if(this.lastCheckPoint < expression.sourceEnd) {
				this.lastCheckPoint = expression.sourceEnd + 1;
			}
		}
	}
}