Java Code Examples for org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants#AccStatic

The following examples show how to use org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants#AccStatic . 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: TypeDeclaration.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * A <clinit> will be requested as soon as static fields or assertions are present. It will be eliminated during
 * classfile creation if no bytecode was actually produced based on some optimizations/compiler settings.
 */
public final boolean needClassInitMethod() {
	// always need a <clinit> when assertions are present
	if ((this.bits & ASTNode.ContainsAssertion) != 0)
		return true;

	switch (kind(this.modifiers)) {
		case TypeDeclaration.INTERFACE_DECL:
		case TypeDeclaration.ANNOTATION_TYPE_DECL:
			return this.fields != null; // fields are implicitly statics
		case TypeDeclaration.ENUM_DECL:
			return true; // even if no enum constants, need to set $VALUES array
	}
	if (this.fields != null) {
		for (int i = this.fields.length; --i >= 0;) {
			FieldDeclaration field = this.fields[i];
			//need to test the modifier directly while there is no binding yet
			if ((field.modifiers & ClassFileConstants.AccStatic) != 0)
				return true; // TODO (philippe) shouldn't it check whether field is initializer or has some initial value ?
		}
	}
	return false;
}
 
Example 2
Source File: EclipseHandlerUtil.java    From EasyMPermission with MIT License 6 votes vote down vote up
/**
 * Given a list of field names and a node referring to a type, finds each name in the list that does not match a field within the type.
 */
public static List<Integer> createListOfNonExistentFields(List<String> list, EclipseNode type, boolean excludeStandard, boolean excludeTransient) {
	boolean[] matched = new boolean[list.size()];
	
	for (EclipseNode child : type.down()) {
		if (list.isEmpty()) break;
		if (child.getKind() != Kind.FIELD) continue;
		if (excludeStandard) {
			if ((((FieldDeclaration)child.get()).modifiers & ClassFileConstants.AccStatic) != 0) continue;
			if (child.getName().startsWith("$")) continue;
		}
		if (excludeTransient && (((FieldDeclaration)child.get()).modifiers & ClassFileConstants.AccTransient) != 0) continue;
		int idx = list.indexOf(child.getName());
		if (idx > -1) matched[idx] = true;
	}
	
	List<Integer> problematic = new ArrayList<Integer>();
	for (int i = 0 ; i < list.size() ; i++) {
		if (!matched[i]) problematic.add(i);
	}
	
	return problematic;
}
 
Example 3
Source File: ASTNode.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static StringBuffer printModifiers(int modifiers, StringBuffer output) {

		if ((modifiers & ClassFileConstants.AccPublic) != 0)
			output.append("public "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccPrivate) != 0)
			output.append("private "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccProtected) != 0)
			output.append("protected "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccStatic) != 0)
			output.append("static "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccFinal) != 0)
			output.append("final "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccSynchronized) != 0)
			output.append("synchronized "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccVolatile) != 0)
			output.append("volatile "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccTransient) != 0)
			output.append("transient "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccNative) != 0)
			output.append("native "); //$NON-NLS-1$
		if ((modifiers & ClassFileConstants.AccAbstract) != 0)
			output.append("abstract "); //$NON-NLS-1$
		if ((modifiers & ExtraCompilerModifiers.AccDefaultMethod) != 0)
			output.append("default "); //$NON-NLS-1$
		return output;
	}
 
Example 4
Source File: SyntheticMethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Construct enum special methods: values or valueOf methods
 */
public SyntheticMethodBinding(SourceTypeBinding declaringEnum, char[] selector) {
    this.declaringClass = declaringEnum;
    this.selector = selector;
    this.modifiers = ClassFileConstants.AccPublic | ClassFileConstants.AccStatic;
	this.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
	LookupEnvironment environment = declaringEnum.scope.environment();
    this.thrownExceptions = Binding.NO_EXCEPTIONS;
	if (selector == TypeConstants.VALUES) {
	    this.returnType = environment.createArrayType(environment.convertToParameterizedType(declaringEnum), 1);
	    this.parameters = Binding.NO_PARAMETERS;
	    this.purpose = SyntheticMethodBinding.EnumValues;
	} else if (selector == TypeConstants.VALUEOF) {
	    this.returnType = environment.convertToParameterizedType(declaringEnum);
	    this.parameters = new TypeBinding[]{ declaringEnum.scope.getJavaLangString() };
	    this.purpose = SyntheticMethodBinding.EnumValueOf;
	}
	SyntheticMethodBinding[] knownAccessMethods = ((SourceTypeBinding)this.declaringClass).syntheticMethods();
	int methodId = knownAccessMethods == null ? 0 : knownAccessMethods.length;
	this.index = methodId;
	if (declaringEnum.isStrictfp()) {
		this.modifiers |= ClassFileConstants.AccStrictfp;
	}
}
 
Example 5
Source File: RecoveredType.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public RecoveredElement add(Block nestedBlockDeclaration,int bracketBalanceValue) {
	this.pendingTypeParameters = null;
	resetPendingModifiers();

	int mods = ClassFileConstants.AccDefault;
	if(parser().recoveredStaticInitializerStart != 0) {
		mods = ClassFileConstants.AccStatic;
	}
	return this.add(new Initializer(nestedBlockDeclaration, mods), bracketBalanceValue);
}
 
Example 6
Source File: SourceTypeBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public FieldBinding addSyntheticFieldForClassLiteral(TypeBinding targetType, BlockScope blockScope) {
	
	if (!isPrototype()) throw new IllegalStateException();
	
	if (this.synthetics == null)
		this.synthetics = new HashMap[MAX_SYNTHETICS];
	if (this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL] == null)
		this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL] = new HashMap(5);

	// use a different table than FIELDS, given there might be a collision between emulation of X.this$0 and X.class.
	FieldBinding synthField = (FieldBinding) this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL].get(targetType);
	if (synthField == null) {
		synthField = new SyntheticFieldBinding(
			CharOperation.concat(
				TypeConstants.SYNTHETIC_CLASS,
				String.valueOf(this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL].size()).toCharArray()),
			blockScope.getJavaLangClass(),
			ClassFileConstants.AccDefault | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic,
			this,
			Constant.NotAConstant,
			this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL].size());
		this.synthetics[SourceTypeBinding.CLASS_LITERAL_EMUL].put(targetType, synthField);
	}
	// ensure there is not already such a field defined by the user
	FieldBinding existingField;
	if ((existingField = getField(synthField.name, true /*resolve*/)) != null) {
		TypeDeclaration typeDecl = blockScope.referenceType();
		FieldDeclaration[] typeDeclarationFields = typeDecl.fields;
		int max = typeDeclarationFields == null ? 0 : typeDeclarationFields.length;
		for (int i = 0; i < max; i++) {
			FieldDeclaration fieldDecl = typeDeclarationFields[i];
			if (fieldDecl.binding == existingField) {
				blockScope.problemReporter().duplicateFieldInType(this, fieldDecl);
				break;
			}
		}
	}
	return synthField;
}
 
Example 7
Source File: SyntheticMethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public SyntheticMethodBinding(int purpose, ArrayBinding arrayType, char [] selector, SourceTypeBinding declaringClass) {
    this.declaringClass = declaringClass;
    this.selector = selector;
    this.modifiers = ClassFileConstants.AccSynthetic | ClassFileConstants.AccPrivate | ClassFileConstants.AccStatic;
	this.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
    this.returnType = arrayType;
    this.parameters = new TypeBinding[] { purpose == SyntheticMethodBinding.ArrayConstructor ? TypeBinding.INT : (TypeBinding) arrayType};
    this.thrownExceptions = Binding.NO_EXCEPTIONS;
    this.purpose = purpose;
	SyntheticMethodBinding[] knownAccessMethods = declaringClass.syntheticMethods();
	int methodId = knownAccessMethods == null ? 0 : knownAccessMethods.length;
	this.index = methodId;
}
 
Example 8
Source File: SyntheticMethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Construct $deserializeLambda$ method
 */
public SyntheticMethodBinding(SourceTypeBinding declaringClass) {
	this.declaringClass = declaringClass;
	this.selector = TypeConstants.DESERIALIZE_LAMBDA;
	this.modifiers = ClassFileConstants.AccPrivate | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic;
	this.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
	this.thrownExceptions = Binding.NO_EXCEPTIONS;
	this.returnType = declaringClass.scope.getJavaLangObject();
    this.parameters = new TypeBinding[]{declaringClass.scope.getJavaLangInvokeSerializedLambda()};
    this.purpose = SyntheticMethodBinding.DeserializeLambda;
	SyntheticMethodBinding[] knownAccessMethods = declaringClass.syntheticMethods();
	int methodId = knownAccessMethods == null ? 0 : knownAccessMethods.length;
	this.index = methodId;
}
 
Example 9
Source File: MethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public final boolean isMain() {
	if (this.selector.length == 4 && CharOperation.equals(this.selector, TypeConstants.MAIN)
			&& ((this.modifiers & (ClassFileConstants.AccPublic | ClassFileConstants.AccStatic)) != 0)
			&& TypeBinding.VOID == this.returnType
			&& this.parameters.length == 1) {
		TypeBinding paramType = this.parameters[0];
		if (paramType.dimensions() == 1 && paramType.leafComponentType().id == TypeIds.T_JavaLangString) {
			return true;
		}
	}
	return false;
}
 
Example 10
Source File: TypeElementImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Set<Modifier> getModifiers()
{
	ReferenceBinding refBinding = (ReferenceBinding)_binding;
	int modifiers = refBinding.modifiers;
	if (refBinding.isInterface() && refBinding.isNestedType()) {
		modifiers |= ClassFileConstants.AccStatic;
	}
	return Factory.getModifiers(modifiers, getKind(), refBinding.isBinaryBinding());
}
 
Example 11
Source File: SyntheticMethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * An method accessor is a method with an access$N selector, where N is incremented in case of collisions.
 */
public void initializeMethodAccessor(MethodBinding accessedMethod, boolean isSuperAccess, ReferenceBinding receiverType) {

	this.targetMethod = accessedMethod;
	if (isSuperAccess && receiverType.isInterface() && !accessedMethod.isStatic())
		this.modifiers = ClassFileConstants.AccPrivate | ClassFileConstants.AccSynthetic;
	else
		this.modifiers = ClassFileConstants.AccDefault | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic;
	this.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
	SourceTypeBinding declaringSourceType = (SourceTypeBinding) receiverType;
	SyntheticMethodBinding[] knownAccessMethods = declaringSourceType.syntheticMethods();
	int methodId = knownAccessMethods == null ? 0 : knownAccessMethods.length;
	this.index = methodId;

	this.selector = CharOperation.concat(TypeConstants.SYNTHETIC_ACCESS_METHOD_PREFIX, String.valueOf(methodId).toCharArray());
	this.returnType = accessedMethod.returnType;
	this.purpose = isSuperAccess ? SyntheticMethodBinding.SuperMethodAccess : SyntheticMethodBinding.MethodAccess;

	if (accessedMethod.isStatic() || (isSuperAccess && receiverType.isInterface())) {
		this.parameters = accessedMethod.parameters;
	} else {
		this.parameters = new TypeBinding[accessedMethod.parameters.length + 1];
		this.parameters[0] = declaringSourceType;
		System.arraycopy(accessedMethod.parameters, 0, this.parameters, 1, accessedMethod.parameters.length);
	}
	this.thrownExceptions = accessedMethod.thrownExceptions;
	this.declaringClass = declaringSourceType;

	// check for method collision
	boolean needRename;
	do {
		check : {
			needRename = false;
			// check for collision with known methods
			MethodBinding[] methods = declaringSourceType.methods();
			for (int i = 0, length = methods.length; i < length; i++) {
				if (CharOperation.equals(this.selector, methods[i].selector) && areParameterErasuresEqual(methods[i])) {
					needRename = true;
					break check;
				}
			}
			// check for collision with synthetic accessors
			if (knownAccessMethods != null) {
				for (int i = 0, length = knownAccessMethods.length; i < length; i++) {
					if (knownAccessMethods[i] == null) continue;
					if (CharOperation.equals(this.selector, knownAccessMethods[i].selector) && areParameterErasuresEqual(knownAccessMethods[i])) {
						needRename = true;
						break check;
					}
				}
			}
		}
		if (needRename) { // retry with a selector & a growing methodId
			setSelector(CharOperation.concat(TypeConstants.SYNTHETIC_ACCESS_METHOD_PREFIX, String.valueOf(++methodId).toCharArray()));
		}
	} while (needRename);

	// retrieve sourceStart position for the target method for line number attributes
	AbstractMethodDeclaration[] methodDecls = declaringSourceType.scope.referenceContext.methods;
	if (methodDecls != null) {
		for (int i = 0, length = methodDecls.length; i < length; i++) {
			if (methodDecls[i].binding == accessedMethod) {
				this.sourceStart = methodDecls[i].sourceStart;
				return;
			}
		}
	}
}
 
Example 12
Source File: MethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public final boolean isStatic() {
	return (this.modifiers & ClassFileConstants.AccStatic) != 0;
}
 
Example 13
Source File: ReferenceBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Answer true if the receiver is a static member type (or toplevel)
 */
public final boolean isStatic() {
	return (this.modifiers & (ClassFileConstants.AccStatic | ClassFileConstants.AccInterface)) != 0 || (this.tagBits & TagBits.IsNestedType) == 0;
}
 
Example 14
Source File: Factory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void decodeModifiers(Set<Modifier> result, int modifiers, int[] checkBits) {
	if (checkBits == null) return;
	for (int i = 0, max = checkBits.length; i < max; i++) {
		switch(checkBits[i]) {
			case ClassFileConstants.AccPublic :
				appendModifier(result, modifiers, checkBits[i], Modifier.PUBLIC);
				break;
			case ClassFileConstants.AccProtected:
				appendModifier(result, modifiers, checkBits[i], Modifier.PROTECTED);
				break;
			case ClassFileConstants.AccPrivate :
				appendModifier(result, modifiers, checkBits[i], Modifier.PRIVATE);
				break;
			case ClassFileConstants.AccAbstract :
				appendModifier(result, modifiers, checkBits[i], Modifier.ABSTRACT);
				break;
			case ExtraCompilerModifiers.AccDefaultMethod :
				try {
					appendModifier(result, modifiers, checkBits[i], Modifier.valueOf("DEFAULT")); //$NON-NLS-1$
				} catch(IllegalArgumentException iae) {
					// Don't have JDK 1.8, just ignore and proceed.
				}
				break;
			case ClassFileConstants.AccStatic :
				appendModifier(result, modifiers, checkBits[i], Modifier.STATIC);
				break;
			case ClassFileConstants.AccFinal :
				appendModifier(result, modifiers, checkBits[i], Modifier.FINAL);
				break;
			case ClassFileConstants.AccSynchronized :
				appendModifier(result, modifiers, checkBits[i], Modifier.SYNCHRONIZED);
				break;
			case ClassFileConstants.AccNative :
				appendModifier(result, modifiers, checkBits[i], Modifier.NATIVE);
				break;
			case ClassFileConstants.AccStrictfp :
				appendModifier(result, modifiers, checkBits[i], Modifier.STRICTFP);
				break;
			case ClassFileConstants.AccTransient :
				appendModifier(result, modifiers, checkBits[i], Modifier.TRANSIENT);
				break;
			case ClassFileConstants.AccVolatile :
				appendModifier(result, modifiers, checkBits[i], Modifier.VOLATILE);
				break;
		}
	}
}
 
Example 15
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 16
Source File: HandleWither.java    From EasyMPermission with MIT License 4 votes vote down vote up
public void createWitherForField(
		AccessLevel level, EclipseNode fieldNode, EclipseNode sourceNode,
		boolean whineIfExists, List<Annotation> onMethod,
		List<Annotation> onParam) {
	
	ASTNode source = sourceNode.get();
	if (fieldNode.getKind() != Kind.FIELD) {
		sourceNode.addError("@Wither is only supported on a class or a field.");
		return;
	}
	
	FieldDeclaration field = (FieldDeclaration) fieldNode.get();
	TypeReference fieldType = copyType(field.type, source);
	boolean isBoolean = isBoolean(fieldType);
	String witherName = toWitherName(fieldNode, isBoolean);
	
	if (witherName == null) {
		fieldNode.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list.");
		return;
	}
	
	if ((field.modifiers & ClassFileConstants.AccStatic) != 0) {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for static fields.");
		return;
	}
	
	if ((field.modifiers & ClassFileConstants.AccFinal) != 0 && field.initialization != null) {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields.");
		return;
	}
	
	if (field.name != null && field.name.length > 0 && field.name[0] == '$') {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for fields starting with $.");
		return;
	}
	
	for (String altName : toAllWitherNames(fieldNode, isBoolean)) {
		switch (methodExists(altName, fieldNode, false, 1)) {
		case EXISTS_BY_LOMBOK:
			return;
		case EXISTS_BY_USER:
			if (whineIfExists) {
				String altNameExpl = "";
				if (!altName.equals(witherName)) altNameExpl = String.format(" (%s)", altName);
				fieldNode.addWarning(
					String.format("Not generating %s(): A method with that name already exists%s", witherName, altNameExpl));
			}
			return;
		default:
		case NOT_EXISTS:
			//continue scanning the other alt names.
		}
	}
	
	int modifier = toEclipseModifier(level);
	
	MethodDeclaration method = createWither((TypeDeclaration) fieldNode.up().get(), fieldNode, witherName, modifier, sourceNode, onMethod, onParam);
	injectMethod(fieldNode.up(), method);
}
 
Example 17
Source File: SourceTypeBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public FieldBinding addSyntheticFieldForEnumValues() {

	if (!isPrototype()) throw new IllegalStateException();
	if (this.synthetics == null)
		this.synthetics = new HashMap[MAX_SYNTHETICS];
	if (this.synthetics[SourceTypeBinding.FIELD_EMUL] == null)
		this.synthetics[SourceTypeBinding.FIELD_EMUL] = new HashMap(5);

	FieldBinding synthField = (FieldBinding) this.synthetics[SourceTypeBinding.FIELD_EMUL].get("enumConstantValues"); //$NON-NLS-1$
	if (synthField == null) {
		synthField = new SyntheticFieldBinding(
			TypeConstants.SYNTHETIC_ENUM_VALUES,
			this.scope.createArrayType(this,1),
			ClassFileConstants.AccPrivate | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic | ClassFileConstants.AccFinal,
			this,
			Constant.NotAConstant,
			this.synthetics[SourceTypeBinding.FIELD_EMUL].size());
		this.synthetics[SourceTypeBinding.FIELD_EMUL].put("enumConstantValues", synthField); //$NON-NLS-1$
	}
	// ensure there is not already such a field defined by the user
	// ensure there is not already such a field defined by the user
	boolean needRecheck;
	int index = 0;
	do {
		needRecheck = false;
		FieldBinding existingField;
		if ((existingField = getField(synthField.name, true /*resolve*/)) != null) {
			TypeDeclaration typeDecl = this.scope.referenceContext;
			FieldDeclaration[] fieldDeclarations = typeDecl.fields;
			int max = fieldDeclarations == null ? 0 : fieldDeclarations.length;
			for (int i = 0; i < max; i++) {
				FieldDeclaration fieldDecl = fieldDeclarations[i];
				if (fieldDecl.binding == existingField) {
					synthField.name = CharOperation.concat(
						TypeConstants.SYNTHETIC_ENUM_VALUES,
						("_" + String.valueOf(index++)).toCharArray()); //$NON-NLS-1$
					needRecheck = true;
					break;
				}
			}
		}
	} while (needRecheck);
	return synthField;
}
 
Example 18
Source File: HandleSetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
public void createSetterForField(
		AccessLevel level, EclipseNode fieldNode, EclipseNode sourceNode,
		boolean whineIfExists, List<Annotation> onMethod,
		List<Annotation> onParam) {
	
	ASTNode source = sourceNode.get();
	if (fieldNode.getKind() != Kind.FIELD) {
		sourceNode.addError("@Setter is only supported on a class or a field.");
		return;
	}
	
	FieldDeclaration field = (FieldDeclaration) fieldNode.get();
	TypeReference fieldType = copyType(field.type, source);
	boolean isBoolean = isBoolean(fieldType);
	String setterName = toSetterName(fieldNode, isBoolean);
	boolean shouldReturnThis = shouldReturnThis(fieldNode);
	
	if (setterName == null) {
		fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
		return;
	}
	
	int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
	
	for (String altName : toAllSetterNames(fieldNode, isBoolean)) {
		switch (methodExists(altName, fieldNode, false, 1)) {
		case EXISTS_BY_LOMBOK:
			return;
		case EXISTS_BY_USER:
			if (whineIfExists) {
				String altNameExpl = "";
				if (!altName.equals(setterName)) altNameExpl = String.format(" (%s)", altName);
				fieldNode.addWarning(
					String.format("Not generating %s(): A method with that name already exists%s", setterName, altNameExpl));
			}
			return;
		default:
		case NOT_EXISTS:
			//continue scanning the other alt names.
		}
	}
	
	MethodDeclaration method = createSetter((TypeDeclaration) fieldNode.up().get(), fieldNode, setterName, shouldReturnThis, modifier, sourceNode, onMethod, onParam);
	injectMethod(fieldNode.up(), method);
}
 
Example 19
Source File: AbstractMethodDeclaration.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean isStatic() {

		if (this.binding != null)
			return this.binding.isStatic();
		return (this.modifiers & ClassFileConstants.AccStatic) != 0;
	}
 
Example 20
Source File: SyntheticMethodBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public SyntheticMethodBinding(FieldBinding targetField, ReferenceBinding declaringClass, TypeBinding enumBinding, char[] selector) {
	this.modifiers = (declaringClass.isInterface() ? ClassFileConstants.AccPublic : ClassFileConstants.AccDefault) | ClassFileConstants.AccStatic | ClassFileConstants.AccSynthetic;
	this.tagBits |= (TagBits.AnnotationResolved | TagBits.DeprecatedAnnotationResolved);
	SourceTypeBinding declaringSourceType = (SourceTypeBinding) declaringClass;
	SyntheticMethodBinding[] knownAccessMethods = declaringSourceType.syntheticMethods();
	int methodId = knownAccessMethods == null ? 0 : knownAccessMethods.length;
	this.index = methodId;
	this.selector = selector;
	this.returnType = declaringSourceType.scope.createArrayType(TypeBinding.INT, 1);
	this.parameters = Binding.NO_PARAMETERS;
	this.targetReadField = targetField;
	this.targetEnumType = enumBinding;
	this.purpose = SyntheticMethodBinding.SwitchTable;
	this.thrownExceptions = Binding.NO_EXCEPTIONS;
	this.declaringClass = declaringSourceType;

	if (declaringSourceType.isStrictfp()) {
		this.modifiers |= ClassFileConstants.AccStrictfp;
	}
	// check for method collision
	boolean needRename;
	do {
		check : {
			needRename = false;
			// check for collision with known methods
			long range;
			MethodBinding[] methods = declaringSourceType.methods();
			if ((range = ReferenceBinding.binarySearch(this.selector, methods)) >= 0) {
				int paramCount = this.parameters.length;
				nextMethod: for (int imethod = (int)range, end = (int)(range >> 32); imethod <= end; imethod++) {
					MethodBinding method = methods[imethod];
					if (method.parameters.length == paramCount) {
						TypeBinding[] toMatch = method.parameters;
						for (int i = 0; i < paramCount; i++) {
							if (TypeBinding.notEquals(toMatch[i], this.parameters[i])) {
								continue nextMethod;
							}
						}
						needRename = true;
						break check;
					}
				}
			}
			// check for collision with synthetic accessors
			if (knownAccessMethods != null) {
				for (int i = 0, length = knownAccessMethods.length; i < length; i++) {
					if (knownAccessMethods[i] == null) continue;
					if (CharOperation.equals(this.selector, knownAccessMethods[i].selector) && areParametersEqual(methods[i])) {
						needRename = true;
						break check;
					}
				}
			}
		}
		if (needRename) { // retry with a selector postfixed by a growing methodId
			setSelector(CharOperation.concat(selector, String.valueOf(++methodId).toCharArray()));
		}
	} while (needRename);

	// We now at this point - per construction - it is for sure an enclosing instance, we are going to
	// show the target field type declaration location.
	this.sourceStart = declaringSourceType.scope.referenceContext.sourceStart; // use the target declaring class name position instead
}