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

The following examples show how to use org.eclipse.jdt.internal.compiler.ast.FieldReference. 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: PatchExtensionMethodCompletionProposal.java    From EasyMPermission with MIT License 6 votes vote down vote up
static TypeBinding getFirstParameterType(TypeDeclaration decl, CompletionProposalCollector completionProposalCollector) {
		TypeBinding firstParameterType = null;
		ASTNode node = getAssistNode(completionProposalCollector);
		if (node == null) return null;
		if (!(node instanceof CompletionOnQualifiedNameReference) && !(node instanceof CompletionOnSingleNameReference) && !(node instanceof CompletionOnMemberAccess)) return null;
		
		// Never offer on 'super.<autocomplete>'.
		if (node instanceof FieldReference && ((FieldReference)node).receiver instanceof SuperReference) return null;
		
		if (node instanceof NameReference) {
			Binding binding = ((NameReference) node).binding;
			// Unremark next block to allow a 'blank' autocomplete to list any extensions that apply to the current scope, but make sure we're not in a static context first, which this doesn't do.
			// Lacking good use cases, and having this particular concept be a little tricky on javac, means for now we don't support extension methods like this. this.X() will be fine, though.
			
/*			if ((node instanceof SingleNameReference) && (((SingleNameReference) node).token.length == 0)) {
				firstParameterType = decl.binding;
			} else */if (binding instanceof VariableBinding) {
				firstParameterType = ((VariableBinding) binding).type;
			}
		} else if (node instanceof FieldReference) {
			firstParameterType = ((FieldReference) node).actualReceiverType;
		}
		return firstParameterType;
	}
 
Example #2
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 #3
Source File: EclipseSingularsRecipes.java    From EasyMPermission with MIT License 6 votes vote down vote up
/** 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;
}
 
Example #4
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 #5
Source File: EclipseJavaUtilListSetSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
void generatePluralMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
	MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
	md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	md.modifiers = ClassFileConstants.AccPublic;
	
	List<Statement> statements = new ArrayList<Statement>();
	statements.add(createConstructBuilderVarIfNeeded(data, builderType, false));
	
	FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
	thisDotField.receiver = new ThisReference(0, 0);
	MessageSend thisDotFieldDotAddAll = new MessageSend();
	thisDotFieldDotAddAll.arguments = new Expression[] {new SingleNameReference(data.getPluralName(), 0L)};
	thisDotFieldDotAddAll.receiver = thisDotField;
	thisDotFieldDotAddAll.selector = "addAll".toCharArray();
	statements.add(thisDotFieldDotAddAll);
	if (returnStatement != null) statements.add(returnStatement);
	
	md.statements = statements.toArray(new Statement[statements.size()]);
	
	TypeReference paramType = new QualifiedTypeReference(TypeConstants.JAVA_UTIL_COLLECTION, NULL_POSS);
	paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs());
	Argument param = new Argument(data.getPluralName(), 0, paramType, 0);
	md.arguments = new Argument[] {param};
	md.returnType = returnType;
	md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("addAll", new String(data.getPluralName())).toCharArray();
	
	data.setGeneratedByRecursive(md);
	injectMethod(builderType, md);
}
 
Example #6
Source File: PatchDelegate.java    From EasyMPermission with MIT License 5 votes vote down vote up
public Expression get(final ASTNode source, char[] name) {
	FieldReference fieldRef = new FieldReference(name, pos(source));
	setGeneratedBy(fieldRef, source);
	fieldRef.receiver = new ThisReference(source.sourceStart, source.sourceEnd);
	setGeneratedBy(fieldRef.receiver, source);
	return fieldRef;
}
 
Example #7
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected Binding resolveNodeToBinding(ASTNode node) {
	if (node instanceof NameReference) {
   		NameReference nr = (NameReference) node;
		if (nr.binding instanceof VariableBinding) {
			VariableBinding vb = (VariableBinding) nr.binding;
			return vb.type;
		}
	} else if (node instanceof FieldReference) {
   		FieldReference fr = (FieldReference) node;
		return fr.receiver.resolvedType;
	}
	return null;
}
 
Example #8
Source File: JavaStatementPostfixContext.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Calculates the beginning position of a given {@link ASTNode}
 * @param node
 * @return
 */
protected int getNodeBegin(ASTNode node) {
	if (node instanceof NameReference) {
		return ((NameReference) node).sourceStart;
	} else if (node instanceof FieldReference) {
		return ((FieldReference) node).receiver.sourceStart;
	} else if (node instanceof MessageSend) {
		return ((MessageSend) node).receiver.sourceStart;
	}
	return node.sourceStart;
}
 
Example #9
Source File: DefaultBindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
synchronized IVariableBinding resolveField(SuperFieldAccess fieldAccess) {
	Object oldNode = this.newAstToOldAst.get(fieldAccess);
	if (oldNode instanceof FieldReference) {
		FieldReference fieldReference = (FieldReference) oldNode;
		return this.getVariableBinding(fieldReference.binding);
	}
	return null;
}
 
Example #10
Source File: DefaultBindingResolver.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
synchronized IVariableBinding resolveField(FieldAccess fieldAccess) {
	Object oldNode = this.newAstToOldAst.get(fieldAccess);
	if (oldNode instanceof FieldReference) {
		FieldReference fieldReference = (FieldReference) oldNode;
		return this.getVariableBinding(fieldReference.binding);
	}
	return null;
}
 
Example #11
Source File: SelectionParser.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected void consumeFieldAccess(boolean isSuperAccess) {
	// FieldAccess ::= Primary '.' 'Identifier'
	// FieldAccess ::= 'super' '.' 'Identifier'

	if (this.indexOfAssistIdentifier() < 0) {
		super.consumeFieldAccess(isSuperAccess);
		return;
	}
	FieldReference fieldReference =
		new SelectionOnFieldReference(
			this.identifierStack[this.identifierPtr],
			this.identifierPositionStack[this.identifierPtr--]);
	this.identifierLengthPtr--;
	if (isSuperAccess) { //considerates the fieldReferenceerence beginning at the 'super' ....
		fieldReference.sourceStart = this.intStack[this.intPtr--];
		fieldReference.receiver = new SuperReference(fieldReference.sourceStart, this.endPosition);
		pushOnExpressionStack(fieldReference);
	} else { //optimize push/pop
		if ((fieldReference.receiver = this.expressionStack[this.expressionPtr]).isThis()) { //fieldReferenceerence begins at the this
			fieldReference.sourceStart = fieldReference.receiver.sourceStart;
		}
		this.expressionStack[this.expressionPtr] = fieldReference;
	}
	this.assistNode = fieldReference;
	this.lastCheckPoint = fieldReference.sourceEnd + 1;
	if (!this.diet){
		this.restartRecovery	= true;	// force to restart in recovery mode
		this.lastIgnoredToken = -1;
	}
	this.isOrphanCompletionNode = true;
}
 
Example #12
Source File: EclipseHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
static Expression createFieldAccessor(EclipseNode field, FieldAccess fieldAccess, ASTNode source) {
	int pS = source == null ? 0 : source.sourceStart, pE = source == null ? 0 : source.sourceEnd;
	long p = (long)pS << 32 | pE;
	
	boolean lookForGetter = lookForGetter(field, fieldAccess);
	
	GetterMethod getter = lookForGetter ? findGetter(field) : null;
	
	if (getter == null) {
		FieldDeclaration fieldDecl = (FieldDeclaration)field.get();
		FieldReference ref = new FieldReference(fieldDecl.name, p);
		if ((fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0) {
			EclipseNode containerNode = field.up();
			if (containerNode != null && containerNode.get() instanceof TypeDeclaration) {
				ref.receiver = new SingleNameReference(((TypeDeclaration)containerNode.get()).name, p);
			} else {
				Expression smallRef = new FieldReference(field.getName().toCharArray(), p);
				if (source != null) setGeneratedBy(smallRef, source);
				return smallRef;
			}
		} else {
			ref.receiver = new ThisReference(pS, pE);
		}
		
		if (source != null) {
			setGeneratedBy(ref, source);
			setGeneratedBy(ref.receiver, source);
		}
		return ref;
	}
	
	MessageSend call = new MessageSend();
	setGeneratedBy(call, source);
	call.sourceStart = pS; call.statementEnd = call.sourceEnd = pE;
	call.receiver = new ThisReference(pS, pE);
	setGeneratedBy(call.receiver, source);
	call.selector = getter.name;
	return call;
}
 
Example #13
Source File: EclipseJavaUtilListSetSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
	MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
	md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	md.modifiers = ClassFileConstants.AccPublic;
	
	List<Statement> statements = new ArrayList<Statement>();
	statements.add(createConstructBuilderVarIfNeeded(data, builderType, false));
	
	FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
	thisDotField.receiver = new ThisReference(0, 0);
	MessageSend thisDotFieldDotAdd = new MessageSend();
	thisDotFieldDotAdd.arguments = new Expression[] {new SingleNameReference(data.getSingularName(), 0L)};
	thisDotFieldDotAdd.receiver = thisDotField;
	thisDotFieldDotAdd.selector = "add".toCharArray();
	statements.add(thisDotFieldDotAdd);
	if (returnStatement != null) statements.add(returnStatement);
	
	md.statements = statements.toArray(new Statement[statements.size()]);
	TypeReference paramType = cloneParamType(0, data.getTypeArgs(), builderType);
	Argument param = new Argument(data.getSingularName(), 0, paramType, 0);
	md.arguments = new Argument[] {param};
	md.returnType = returnType;
	md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("add", new String(data.getSingularName())).toCharArray();
	
	data.setGeneratedByRecursive(md);
	injectMethod(builderType, md);
}
 
Example #14
Source File: EclipseGuavaSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
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);
}
 
Example #15
Source File: EclipseGuavaSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
void generatePluralMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
	boolean mapMode = isMap();
	
	MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
	md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	md.modifiers = ClassFileConstants.AccPublic;
	
	List<Statement> statements = new ArrayList<Statement>();
	statements.add(createConstructBuilderVarIfNeeded(data, builderType));
	
	FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
	thisDotField.receiver = new ThisReference(0, 0);
	MessageSend thisDotFieldDotAddAll = new MessageSend();
	thisDotFieldDotAddAll.arguments = new Expression[] {new SingleNameReference(data.getPluralName(), 0L)};
	thisDotFieldDotAddAll.receiver = thisDotField;
	thisDotFieldDotAddAll.selector = (mapMode ? "putAll" : "addAll").toCharArray();
	statements.add(thisDotFieldDotAddAll);
	if (returnStatement != null) statements.add(returnStatement);
	
	md.statements = statements.toArray(new Statement[statements.size()]);
	
	TypeReference paramType;
	if (mapMode) {
		paramType = new QualifiedTypeReference(JAVA_UTIL_MAP, NULL_POSS);
		paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs());
	} else {
		paramType = new QualifiedTypeReference(TypeConstants.JAVA_LANG_ITERABLE, NULL_POSS);
		paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs());
	}
	Argument param = new Argument(data.getPluralName(), 0, paramType, 0);
	md.arguments = new Argument[] {param};
	md.returnType = returnType;
	md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName(mapMode ? "putAll" : "addAll", new String(data.getPluralName())).toCharArray();
	
	data.setGeneratedByRecursive(md);
	injectMethod(builderType, md);
}
 
Example #16
Source File: EclipseJavaUtilMapSingularizer.java    From EasyMPermission with MIT License 4 votes vote down vote up
private void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
	MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
	md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	md.modifiers = ClassFileConstants.AccPublic;
	
	List<Statement> statements = new ArrayList<Statement>();
	statements.add(createConstructBuilderVarIfNeeded(data, builderType, true));
	
	String sN = new String(data.getSingularName());
	String pN = new String(data.getPluralName());
	char[] keyParamName = (sN + "Key").toCharArray();
	char[] valueParamName = (sN + "Value").toCharArray();
	char[] keyFieldName = (pN + "$key").toCharArray();
	char[] valueFieldName = (pN + "$value").toCharArray();
	
	/* this.pluralname$key.add(singularnameKey); */ {
		FieldReference thisDotKeyField = new FieldReference(keyFieldName, 0L);
		thisDotKeyField.receiver = new ThisReference(0, 0);
		MessageSend thisDotKeyFieldDotAdd = new MessageSend();
		thisDotKeyFieldDotAdd.arguments = new Expression[] {new SingleNameReference(keyParamName, 0L)};
		thisDotKeyFieldDotAdd.receiver = thisDotKeyField;
		thisDotKeyFieldDotAdd.selector = "add".toCharArray();
		statements.add(thisDotKeyFieldDotAdd);
	}
	
	/* this.pluralname$value.add(singularnameValue); */ {
		FieldReference thisDotValueField = new FieldReference(valueFieldName, 0L);
		thisDotValueField.receiver = new ThisReference(0, 0);
		MessageSend thisDotValueFieldDotAdd = new MessageSend();
		thisDotValueFieldDotAdd.arguments = new Expression[] {new SingleNameReference(valueParamName, 0L)};
		thisDotValueFieldDotAdd.receiver = thisDotValueField;
		thisDotValueFieldDotAdd.selector = "add".toCharArray();
		statements.add(thisDotValueFieldDotAdd);
	}
	if (returnStatement != null) statements.add(returnStatement);
	
	md.statements = statements.toArray(new Statement[statements.size()]);
	TypeReference keyParamType = cloneParamType(0, data.getTypeArgs(), builderType);
	Argument keyParam = new Argument(keyParamName, 0, keyParamType, 0);
	TypeReference valueParamType = cloneParamType(1, data.getTypeArgs(), builderType);
	Argument valueParam = new Argument(valueParamName, 0, valueParamType, 0);
	md.arguments = new Argument[] {keyParam, valueParam};
	md.returnType = returnType;
	md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName("put", new String(data.getSingularName())).toCharArray();
	
	data.setGeneratedByRecursive(md);
	injectMethod(builderType, md);
}
 
Example #17
Source File: EclipseJavaUtilMapSingularizer.java    From EasyMPermission with MIT License 4 votes vote down vote up
private void generatePluralMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
	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();
	
	List<Statement> statements = new ArrayList<Statement>();
	statements.add(createConstructBuilderVarIfNeeded(data, builderType, true));
	
	char[] entryName = "$lombokEntry".toCharArray();
	
	TypeReference forEachType = new QualifiedTypeReference(JAVA_UTIL_MAP_ENTRY, NULL_POSS);
	forEachType = addTypeArgs(2, true, builderType, forEachType, data.getTypeArgs());
	
	MessageSend keyArg = new MessageSend();
	keyArg.receiver = new SingleNameReference(entryName, 0L);
	keyArg.selector = "getKey".toCharArray();
	MessageSend addKey = new MessageSend();
	FieldReference thisDotKeyField = new FieldReference(keyFieldName, 0L);
	thisDotKeyField.receiver = new ThisReference(0, 0);
	addKey.receiver = thisDotKeyField;
	addKey.selector = new char[] {'a', 'd', 'd'};
	addKey.arguments = new Expression[] {keyArg};
	
	MessageSend valueArg = new MessageSend();
	valueArg.receiver = new SingleNameReference(entryName, 0L);
	valueArg.selector = "getValue".toCharArray();
	MessageSend addValue = new MessageSend();
	FieldReference thisDotValueField = new FieldReference(valueFieldName, 0L);
	thisDotValueField.receiver = new ThisReference(0, 0);
	addValue.receiver = thisDotValueField;
	addValue.selector = new char[] {'a', 'd', 'd'};
	addValue.arguments = new Expression[] {valueArg};
	
	LocalDeclaration elementVariable = new LocalDeclaration(entryName, 0, 0);
	elementVariable.type = forEachType;
	ForeachStatement forEach = new ForeachStatement(elementVariable, 0);
	MessageSend invokeEntrySet = new MessageSend();
	invokeEntrySet.selector = new char[] { 'e', 'n', 't', 'r', 'y', 'S', 'e', 't'};
	invokeEntrySet.receiver = new SingleNameReference(data.getPluralName(), 0L);
	forEach.collection = invokeEntrySet;
	Block forEachContent = new Block(0);
	forEachContent.statements = new Statement[] {addKey, addValue};
	forEach.action = forEachContent;
	statements.add(forEach);
	if (returnStatement != null) statements.add(returnStatement);
	
	md.statements = statements.toArray(new Statement[statements.size()]);
	
	TypeReference paramType = new QualifiedTypeReference(JAVA_UTIL_MAP, NULL_POSS);
	paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs());
	Argument param = new Argument(data.getPluralName(), 0, paramType, 0);
	md.arguments = new Argument[] {param};
	md.returnType = returnType;
	md.selector = fluent ? data.getPluralName() : HandlerUtil.buildAccessorName("putAll", new String(data.getPluralName())).toCharArray();
	
	data.setGeneratedByRecursive(md);
	injectMethod(builderType, md);
}
 
Example #18
Source File: BinaryExpressionFragmentBuilder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean visit(FieldReference fieldReference, BlockScope scope) {
	addRealFragment(fieldReference);
	return false;
}
 
Example #19
Source File: EclipseGuavaSingularizer.java    From EasyMPermission with MIT License 4 votes vote down vote up
void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
	boolean mapMode = isMap();
	char[] keyName = !mapMode ? data.getSingularName() : (new String(data.getSingularName()) + "$key").toCharArray();
	char[] valueName = !mapMode ? null : (new String(data.getSingularName()) + "$value").toCharArray();
	
	MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
	md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	md.modifiers = ClassFileConstants.AccPublic;
	
	List<Statement> statements = new ArrayList<Statement>();
	statements.add(createConstructBuilderVarIfNeeded(data, builderType));
	
	FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
	thisDotField.receiver = new ThisReference(0, 0);
	MessageSend thisDotFieldDotAdd = new MessageSend();
	if (mapMode) {
		thisDotFieldDotAdd.arguments = new Expression[] {
				new SingleNameReference(keyName, 0L),
				new SingleNameReference(valueName, 0L)};
	} else {
		thisDotFieldDotAdd.arguments = new Expression[] {new SingleNameReference(keyName, 0L)};
	}
	thisDotFieldDotAdd.receiver = thisDotField;
	thisDotFieldDotAdd.selector = (mapMode ? "put" : "add").toCharArray();
	statements.add(thisDotFieldDotAdd);
	if (returnStatement != null) statements.add(returnStatement);
	md.statements = statements.toArray(new Statement[statements.size()]);
	
	if (mapMode) {
		TypeReference keyType = cloneParamType(0, data.getTypeArgs(), builderType);
		Argument keyParam = new Argument(keyName, 0, keyType, 0);
		TypeReference valueType = cloneParamType(1, data.getTypeArgs(), builderType);
		Argument valueParam = new Argument(valueName, 0, valueType, 0);
		md.arguments = new Argument[] {keyParam, valueParam};
	} else {
		TypeReference paramType = cloneParamType(0, data.getTypeArgs(), builderType);
		Argument param = new Argument(keyName, 0, paramType, 0);
		md.arguments = new Argument[] {param};
	}
	md.returnType = returnType;
	md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName(mapMode ? "put" : "add", new String(data.getSingularName())).toCharArray();
	
	data.setGeneratedByRecursive(md);
	injectMethod(builderType, md);
}
 
Example #20
Source File: HandleConstructor.java    From EasyMPermission with MIT License 4 votes vote down vote up
public static ConstructorDeclaration createConstructor(
		AccessLevel level, EclipseNode type, Collection<EclipseNode> fields,
		Boolean suppressConstructorProperties, EclipseNode sourceNode, List<Annotation> onConstructor) {
	
	ASTNode source = sourceNode.get();
	TypeDeclaration typeDeclaration = ((TypeDeclaration)type.get());
	long p = (long)source.sourceStart << 32 | source.sourceEnd;
	
	boolean isEnum = (((TypeDeclaration)type.get()).modifiers & ClassFileConstants.AccEnum) != 0;
	
	if (isEnum) level = AccessLevel.PRIVATE;
	
	if (suppressConstructorProperties == null) {
		if (fields.isEmpty()) {
			suppressConstructorProperties = false;
		} else {
			suppressConstructorProperties = Boolean.TRUE.equals(type.getAst().readConfiguration(ConfigurationKeys.ANY_CONSTRUCTOR_SUPPRESS_CONSTRUCTOR_PROPERTIES));
		}
	}
	
	ConstructorDeclaration constructor = new ConstructorDeclaration(
			((CompilationUnitDeclaration) type.top().get()).compilationResult);
	
	constructor.modifiers = toEclipseModifier(level);
	constructor.selector = typeDeclaration.name;
	constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
	constructor.constructorCall.sourceStart = source.sourceStart;
	constructor.constructorCall.sourceEnd = source.sourceEnd;
	constructor.thrownExceptions = null;
	constructor.typeParameters = null;
	constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
	constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
	constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;
	constructor.arguments = null;
	
	List<Argument> params = new ArrayList<Argument>();
	List<Statement> assigns = new ArrayList<Statement>();
	List<Statement> nullChecks = new ArrayList<Statement>();
	
	for (EclipseNode fieldNode : fields) {
		FieldDeclaration field = (FieldDeclaration) fieldNode.get();
		char[] rawName = field.name;
		char[] fieldName = removePrefixFromField(fieldNode);
		FieldReference thisX = new FieldReference(rawName, p);
		thisX.receiver = new ThisReference((int)(p >> 32), (int)p);
		
		SingleNameReference assignmentNameRef = new SingleNameReference(fieldName, p);
		Assignment assignment = new Assignment(thisX, assignmentNameRef, (int)p);
		assignment.sourceStart = (int)(p >> 32); assignment.sourceEnd = assignment.statementEnd = (int)(p >> 32);
		assigns.add(assignment);
		long fieldPos = (((long)field.sourceStart) << 32) | field.sourceEnd;
		Argument parameter = new Argument(fieldName, fieldPos, copyType(field.type, source), Modifier.FINAL);
		Annotation[] nonNulls = findAnnotations(field, NON_NULL_PATTERN);
		Annotation[] nullables = findAnnotations(field, NULLABLE_PATTERN);
		if (nonNulls.length != 0) {
			Statement nullCheck = generateNullCheck(field, sourceNode);
			if (nullCheck != null) nullChecks.add(nullCheck);
		}
		parameter.annotations = copyAnnotations(source, nonNulls, nullables);
		params.add(parameter);
	}
	
	nullChecks.addAll(assigns);
	constructor.statements = nullChecks.isEmpty() ? null : nullChecks.toArray(new Statement[nullChecks.size()]);
	constructor.arguments = params.isEmpty() ? null : params.toArray(new Argument[params.size()]);
	
	/* Generate annotations that must  be put on the generated method, and attach them. */ {
		Annotation[] constructorProperties = null;
		if (!suppressConstructorProperties && level != AccessLevel.PRIVATE && level != AccessLevel.PACKAGE && !isLocalType(type)) {
			constructorProperties = createConstructorProperties(source, fields);
		}
		
		constructor.annotations = copyAnnotations(source,
				onConstructor.toArray(new Annotation[0]),
				constructorProperties);
	}
	
	constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope);
	return constructor;
}
 
Example #21
Source File: SetGeneratedByVisitor.java    From EasyMPermission with MIT License 4 votes vote down vote up
@Override public boolean visit(FieldReference node, ClassScope scope) {
	fixPositions(setGeneratedBy(node, source));
	return super.visit(node, scope);
}
 
Example #22
Source File: SetGeneratedByVisitor.java    From EasyMPermission with MIT License 4 votes vote down vote up
@Override public boolean visit(FieldReference node, BlockScope scope) {
	fixPositions(setGeneratedBy(node, source));
	return super.visit(node, scope);
}
 
Example #23
Source File: SetGeneratedByVisitor.java    From EasyMPermission with MIT License 4 votes vote down vote up
private void fixPositions(FieldReference node) {
	node.sourceEnd = sourceEnd;
	node.sourceStart = sourceStart;
	node.statementEnd = sourceEnd;
	node.nameSourcePosition = sourcePos;
}