Java Code Examples for org.eclipse.jdt.internal.compiler.ast.MethodDeclaration#traverse()

The following examples show how to use org.eclipse.jdt.internal.compiler.ast.MethodDeclaration#traverse() . 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
private MethodDeclaration generateCleanMethod(List<BuilderFieldData> builderFields, EclipseNode builderType, ASTNode source) {
	List<Statement> statements = new ArrayList<Statement>();
	
	for (BuilderFieldData bfd : builderFields) {
		if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
			bfd.singularData.getSingularizer().appendCleaningCode(bfd.singularData, builderType, statements);
		}
	}
	
	FieldReference thisUnclean = new FieldReference(CLEAN_FIELD_NAME, 0);
	thisUnclean.receiver = new ThisReference(0, 0);
	statements.add(new Assignment(thisUnclean, new FalseLiteral(0, 0), 0));
	MethodDeclaration decl = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
	decl.selector = CLEAN_METHOD_NAME;
	decl.modifiers = ClassFileConstants.AccPrivate;
	decl.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	decl.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0);
	decl.statements = statements.toArray(new Statement[0]);
	decl.traverse(new SetGeneratedByVisitor(source), (ClassScope) null);
	return decl;
}
 
Example 2
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 3
Source File: PatchDelegate.java    From EasyMPermission with MIT License 5 votes vote down vote up
private static void generateDelegateMethods(EclipseNode typeNode, List<BindingTuple> methods, DelegateReceiver delegateReceiver) {
	CompilationUnitDeclaration top = (CompilationUnitDeclaration) typeNode.top().get();
	for (BindingTuple pair : methods) {
		EclipseNode annNode = typeNode.getAst().get(pair.responsible);
		MethodDeclaration method = createDelegateMethod(pair.fieldName, typeNode, pair, top.compilationResult, annNode, delegateReceiver);
		if (method != null) { 
			SetGeneratedByVisitor visitor = new SetGeneratedByVisitor(annNode.get());
			method.traverse(visitor, ((TypeDeclaration)typeNode.get()).scope);
			injectMethod(typeNode, method);
		}
	}
}
 
Example 4
Source File: UnresolvedReferenceNameFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void find(
		char[] startWith,
		Initializer initializer,
		ClassScope scope,
		int from,
		char[][] discouragedNames,
		UnresolvedReferenceNameRequestor nameRequestor) {
	MethodDeclaration fakeMethod =
		this.findAfter(startWith, scope, from, initializer.bodyEnd, MAX_LINE_COUNT, false, discouragedNames, nameRequestor);
	if (fakeMethod != null) fakeMethod.traverse(this, scope);
}
 
Example 5
Source File: UnresolvedReferenceNameFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void find(
		char[] startWith,
		AbstractMethodDeclaration methodDeclaration,
		int from,
		char[][] discouragedNames,
		UnresolvedReferenceNameRequestor nameRequestor) {
	MethodDeclaration fakeMethod =
		this.findAfter(startWith, methodDeclaration.scope, from, methodDeclaration.bodyEnd, MAX_LINE_COUNT, false, discouragedNames, nameRequestor);
	if (fakeMethod != null) fakeMethod.traverse(this, methodDeclaration.scope.classScope());
}
 
Example 6
Source File: UnresolvedReferenceNameFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void findAfter(
		char[] startWith,
		Scope scope,
		ClassScope classScope,
		int from,
		int to,
		char[][] discouragedNames,
		UnresolvedReferenceNameRequestor nameRequestor) {
	MethodDeclaration fakeMethod =
		this.findAfter(startWith, scope, from, to, MAX_LINE_COUNT / 2, true, discouragedNames, nameRequestor);
	if (fakeMethod != null) fakeMethod.traverse(this, classScope);
}
 
Example 7
Source File: UnresolvedReferenceNameFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public void findBefore(
		char[] startWith,
		Scope scope,
		ClassScope classScope,
		int from,
		int recordTo,
		int parseTo,
		char[][] discouragedNames,
		UnresolvedReferenceNameRequestor nameRequestor) {
	MethodDeclaration fakeMethod =
		this.findBefore(startWith, scope, from, recordTo, parseTo, MAX_LINE_COUNT / 2, discouragedNames, nameRequestor);
	if (fakeMethod != null) fakeMethod.traverse(this, classScope);
}
 
Example 8
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 9
Source File: HandleGetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
public MethodDeclaration createGetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source, boolean lazy, List<Annotation> onMethod) {
	FieldDeclaration field = (FieldDeclaration) fieldNode.get();
	
	// Remember the type; lazy will change it;
	TypeReference returnType = copyType(((FieldDeclaration) fieldNode.get()).type, source);
	
	Statement[] statements;
	if (lazy) {
		statements = createLazyGetterBody(source, fieldNode);
	} else {
		statements = createSimpleGetterBody(source, fieldNode);
	}
	
	MethodDeclaration method = new MethodDeclaration(parent.compilationResult);
	method.modifiers = modifier;
	method.returnType = returnType;
	method.annotations = null;
	method.arguments = null;
	method.selector = name.toCharArray();
	method.binding = null;
	method.thrownExceptions = null;
	method.typeParameters = null;
	method.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	method.bodyStart = method.declarationSourceStart = method.sourceStart = source.sourceStart;
	method.bodyEnd = method.declarationSourceEnd = method.sourceEnd = source.sourceEnd;
	method.statements = statements;
	
	EclipseHandlerUtil.registerCreatedLazyGetter((FieldDeclaration) fieldNode.get(), method.selector, returnType);
	
	/* Generate annotations that must  be put on the generated method, and attach them. */ {
		Annotation[] deprecated = null;
		if (isFieldDeprecated(fieldNode)) {
			deprecated = new Annotation[] { generateDeprecatedAnnotation(source) };
		}
		
		method.annotations = copyAnnotations(source,
				onMethod.toArray(new Annotation[0]),
				findAnnotations(field, NON_NULL_PATTERN),
				findAnnotations(field, NULLABLE_PATTERN),
				findDelegatesAndMarkAsHandled(fieldNode),
				deprecated);
	}
	
	method.traverse(new SetGeneratedByVisitor(source), parent.scope);
	return method;
}
 
Example 10
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 11
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;
}