Skip to content

Commit

Permalink
Switch to Flags for common modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Sep 24, 2017
1 parent c465221 commit 670a112
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.runtime.MetaClassHelper;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
import org.eclipse.jdt.internal.compiler.ast.Argument;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.lookup.AnnotationBinding;
import org.eclipse.jdt.internal.compiler.lookup.BinaryTypeBinding;
import org.eclipse.jdt.internal.compiler.lookup.ClassScope;
Expand Down Expand Up @@ -244,7 +244,7 @@ private boolean isNotActuallyAbstract(MethodBinding methodBinding, ReferenceBind
if (methodBinding.declaringClass instanceof SourceTypeBinding) {
AbstractMethodDeclaration methodDeclaration = ((SourceTypeBinding) methodBinding.declaringClass).scope.referenceContext.declarationOf(methodBinding);
if (methodDeclaration != null) {
return (methodDeclaration.modifiers & ClassFileConstants.AccAbstract) == 0;
return !Flags.isAbstract(methodDeclaration.modifiers);
}
}
if (methodBinding.declaringClass instanceof BinaryTypeBinding) {
Expand Down Expand Up @@ -308,12 +308,12 @@ private void createMethod(String name, boolean isStatic, String signature, TypeB
}
}
if (!found) {
int modifiers = ClassFileConstants.AccPublic;
int modifiers = Flags.AccPublic;
if (isStatic) {
modifiers |= ClassFileConstants.AccStatic;
modifiers |= Flags.AccStatic;
}
if (this.referenceContext.binding.isInterface()) {
modifiers |= ClassFileConstants.AccAbstract;
modifiers |= Flags.AccAbstract;
}
char[] methodName = name.toCharArray();
/*
Expand Down Expand Up @@ -357,12 +357,12 @@ private void createGetterMethod(String propertyName, String name, boolean isStat
// FIXASC what about inherited methods - what if the supertype
// provides an implementation, does the subtype get a new method?
if (!found) {
int modifiers = ClassFileConstants.AccPublic;
int modifiers = Flags.AccPublic;
if (isStatic) {
modifiers |= ClassFileConstants.AccStatic;
modifiers |= Flags.AccStatic;
}
if (this.referenceContext.binding.isInterface()) {
modifiers |= ClassFileConstants.AccAbstract;
modifiers |= Flags.AccAbstract;
}
/*
* if (typeDeclaration != null) { // check we are not attempting to override a final method MethodBinding[]
Expand Down Expand Up @@ -414,12 +414,12 @@ private void createSetterMethod(String propertyName, String name, boolean isStat
// FIXASC what about inherited methods - what if the supertype
// provides an implementation, does the subtype get a new method?
if (!found) {
int modifiers = ClassFileConstants.AccPublic;
int modifiers = Flags.AccPublic;
if (isStatic) {
modifiers |= ClassFileConstants.AccStatic;
modifiers |= Flags.AccStatic;
}
if (this.referenceContext.binding.isInterface()) {
modifiers |= ClassFileConstants.AccAbstract;
modifiers |= Flags.AccAbstract;
}
char[] methodName = name.toCharArray();
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.codehaus.jdt.groovy.core.dom.GroovyCompilationUnit;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.compiler.CategorizedProblem;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.groovy.core.Activator;
Expand Down Expand Up @@ -954,7 +955,7 @@ private void createPackageDeclaration(ModuleNode moduleNode) {
PackageNode packageNode = moduleNode.getPackage();
char[][] splits = CharOperation.splitOn('.', packageName.toCharArray());
long[] positions = positionsFor(splits, startOffset(packageNode), endOffset(packageNode));
ImportReference ref = new ImportReference(splits, positions, true, ClassFileConstants.AccDefault);
ImportReference ref = new ImportReference(splits, positions, true, Flags.AccDefault);
ref.annotations = createAnnotations(packageNode.getAnnotations());
ref.declarationEnd = ref.sourceEnd + trailerLength(packageNode);
ref.declarationSourceStart = ref.sourceStart - 8; // "package ".length()
Expand Down Expand Up @@ -986,11 +987,11 @@ private void createImportDeclarations(ModuleNode moduleNode) {
ImportReference ref;
if (importNode.getAlias() == null || importNode.getAlias().length() < 1) {
long[] positions = positionsFor(splits, typeStartOffset, typeEndOffset);
ref = new ImportReference(splits, positions, false, ClassFileConstants.AccDefault);
ref = new ImportReference(splits, positions, false, Flags.AccDefault);
} else {
endOffset = endOffset(importNode);
long[] positions = positionsFor(splits, typeStartOffset, endOffset);
ref = new AliasImportReference(importNode.getAlias().toCharArray(), splits, positions, false, ClassFileConstants.AccDefault);
ref = new AliasImportReference(importNode.getAlias().toCharArray(), splits, positions, false, Flags.AccDefault);
}
ref.annotations = createAnnotations(importNode.getAnnotations());
ref.sourceEnd = Math.max(endOffset - 1, ref.sourceStart); // For error reporting, Eclipse wants -1
Expand All @@ -1017,7 +1018,7 @@ private void createImportDeclarations(ModuleNode moduleNode) {
packageEndOffset = packageStartOffset + importText.length() - "import ".length() - ".*".length(),
endOffset = endOffset(importPackage);
char[][] splits = CharOperation.splitOn('.', importPackage.getPackageName().substring(0, importPackage.getPackageName().length() - 1).toCharArray());
ImportReference ref = new ImportReference(splits, positionsFor(splits, packageStartOffset, packageEndOffset), true, ClassFileConstants.AccDefault);
ImportReference ref = new ImportReference(splits, positionsFor(splits, packageStartOffset, packageEndOffset), true, Flags.AccDefault);
ref.annotations = createAnnotations(importPackage.getAnnotations());
// import * style only have slocs for the entire ImportNode and not for the embedded type
ref.sourceEnd = Math.max(endOffset - 1, ref.sourceStart); // For error reporting, Eclipse wants -1
Expand All @@ -1038,11 +1039,11 @@ private void createImportDeclarations(ModuleNode moduleNode) {
ImportReference ref = null;
if (importNode.getAlias() == null || importNode.getAlias().length() < 1) {
long[] positions = positionsFor(splits, typeStartOffset, typeEndOffset);
ref = new ImportReference(splits, positions, false, ClassFileConstants.AccDefault | ClassFileConstants.AccStatic);
ref = new ImportReference(splits, positions, false, Flags.AccDefault | Flags.AccStatic);
} else {
endOffset = endOffset(importNode);
long[] positions = positionsFor(splits, typeStartOffset, endOffset);
ref = new AliasImportReference(importNode.getAlias().toCharArray(), splits, positions, false, ClassFileConstants.AccDefault | ClassFileConstants.AccStatic);
ref = new AliasImportReference(importNode.getAlias().toCharArray(), splits, positions, false, Flags.AccDefault | Flags.AccStatic);
}
ref.annotations = createAnnotations(importNode.getAnnotations());
ref.sourceEnd = Math.max(endOffset - 1, ref.sourceStart); // For error reporting, Eclipse wants -1
Expand All @@ -1062,7 +1063,7 @@ private void createImportDeclarations(ModuleNode moduleNode) {
endOffset = endOffset(importNode);
char[][] splits = CharOperation.splitOn('.', classname.toCharArray());
long[] positions = positionsFor(splits, typeStartOffset, typeEndOffset);
ImportReference ref = new ImportReference(splits, positions, true, ClassFileConstants.AccDefault | ClassFileConstants.AccStatic);
ImportReference ref = new ImportReference(splits, positions, true, Flags.AccDefault | Flags.AccStatic);
ref.annotations = createAnnotations(importNode.getAnnotations());
ref.sourceEnd = Math.max(endOffset - 1, ref.sourceStart); // For error reporting, Eclipse wants -1
ref.declarationEnd = ref.sourceEnd + trailerLength(importNode);
Expand Down Expand Up @@ -1305,9 +1306,9 @@ private void createConstructorDeclarations(ClassNode classNode, boolean isEnum,
ConstructorDeclaration constructor = new ConstructorDeclaration(unitDeclaration.compilationResult);
constructor.bits |= ASTNode.IsDefaultConstructor;
if (isEnum) {
constructor.modifiers = ClassFileConstants.AccPrivate;
constructor.modifiers = Flags.AccPrivate;
} else {
constructor.modifiers = ClassFileConstants.AccPublic;
constructor.modifiers = Flags.AccPublic;
}
constructor.selector = ctorName;
accumulatedMethodDeclarations.add(constructor);
Expand All @@ -1317,7 +1318,7 @@ private void createConstructorDeclarations(ClassNode classNode, boolean isEnum,
ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(unitDeclaration.compilationResult);
fixupSourceLocationsForConstructorDeclaration(constructorDeclaration, constructorNode);
constructorDeclaration.annotations = createAnnotations(constructorNode.getAnnotations());
constructorDeclaration.modifiers = isEnum ? ClassFileConstants.AccPrivate : getModifiers(constructorNode);
constructorDeclaration.modifiers = isEnum ? Flags.AccPrivate : getModifiers(constructorNode);
constructorDeclaration.selector = ctorName;
constructorDeclaration.arguments = createArguments(constructorNode.getParameters(), false);
constructorDeclaration.thrownExceptions = createTypeReferencesForClassNodes(constructorNode.getExceptions());
Expand Down Expand Up @@ -1415,7 +1416,7 @@ private MethodDeclaration createMethodDeclaration(ClassNode classNode, boolean i
ClassNode returnType = methodNode.getReturnType();

// source of 'static main(args)' would become 'static Object main(Object args)' - so transform here
if ((modifiers & ClassFileConstants.AccStatic) != 0 && params != null && params.length == 1 && methodNode.getName().equals("main")) {
if (Flags.isStatic(modifiers) && params != null && params.length == 1 && methodNode.getName().equals("main")) {
Parameter p = params[0];
if (p.getType() == null || p.getType().getName().equals(ClassHelper.OBJECT)) {
String name = p.getName();
Expand Down Expand Up @@ -1447,7 +1448,7 @@ private void createConstructorVariants(ConstructorNode constructorNode, Construc
for (Argument[] variant : variants) {
ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(compilationResult);
constructorDeclaration.annotations = createAnnotations(constructorNode.getAnnotations());
constructorDeclaration.modifiers = isEnum ? ClassFileConstants.AccPrivate : ClassFileConstants.AccPublic;
constructorDeclaration.modifiers = isEnum ? Flags.AccPrivate : Flags.AccPublic;
constructorDeclaration.selector = constructorDecl.selector;
constructorDeclaration.arguments = variant;
fixupSourceLocationsForConstructorDeclaration(constructorDeclaration, constructorNode);
Expand Down Expand Up @@ -1676,7 +1677,7 @@ private Argument[] createArguments(Parameter[] ps, boolean isMain) {
} else {
pos = toPos(parameter.getStart(), parameter.getEnd() - 1);
}
arguments[i] = new Argument(parameter.getName().toCharArray(), pos, parameterTypeReference, ClassFileConstants.AccPublic);
arguments[i] = new Argument(parameter.getName().toCharArray(), pos, parameterTypeReference, Flags.AccPublic);
arguments[i].declarationSourceStart = pstart;
}
if (isVargs(ps)) {
Expand Down Expand Up @@ -2149,37 +2150,37 @@ private int endOffset(org.codehaus.groovy.ast.ASTNode node) {
private int getModifiers(ClassNode node, boolean isInner) {
int modifiers = node.getModifiers();
if (isTrait(node)) {
modifiers |= ClassFileConstants.AccInterface;
modifiers |= Flags.AccInterface;
}
if (node.isInterface()) {
modifiers &= ~ClassFileConstants.AccAbstract;
modifiers &= ~Flags.AccAbstract;
}
if (node.isEnum()) {
modifiers &= ~(ClassFileConstants.AccAbstract | ClassFileConstants.AccFinal);
modifiers &= ~(Flags.AccAbstract | Flags.AccFinal);
}
if (!isInner) {
// TODO: does this make types visible that shouldn't be?
modifiers &= ~(ClassFileConstants.AccProtected | ClassFileConstants.AccPrivate | ClassFileConstants.AccStatic);
modifiers &= ~(Flags.AccProtected | Flags.AccPrivate | Flags.AccStatic);
}
if (/*node.isSyntheticPublic() &&*/ hasPackageScopeXform(node, PackageScopeTarget.CLASS)) {
modifiers &= ~ClassFileConstants.AccPublic;
modifiers &= ~Flags.AccPublic;
}
return modifiers;
}

private int getModifiers(FieldNode node) {
int modifiers = node.getModifiers();
if (node.getDeclaringClass().getProperty(node.getName()) != null && hasPackageScopeXform(node, PackageScopeTarget.FIELDS)) {
modifiers &= ~ClassFileConstants.AccPrivate;
modifiers &= ~Flags.AccPrivate;
}
return modifiers;
}

private int getModifiers(MethodNode node) {
int modifiers = node.getModifiers();
modifiers &= ~(ClassFileConstants.AccSynthetic | ClassFileConstants.AccTransient);
modifiers &= ~(Flags.AccSynthetic | Flags.AccTransient);
if (node.isSyntheticPublic() && hasPackageScopeXform(node, PackageScopeTarget.METHODS)) {
modifiers &= ~ClassFileConstants.AccPublic;
modifiers &= ~Flags.AccPublic;
}
return modifiers;
}
Expand All @@ -2189,7 +2190,7 @@ private int getModifiers(ConstructorNode node) {
try {
PackageScopeTarget type = PackageScopeTarget.valueOf("CONSTRUCTORS");
if (node.isSyntheticPublic() && hasPackageScopeXform(node, type)) {
modifiers &= ~ClassFileConstants.AccPublic;
modifiers &= ~Flags.AccPublic;
}
} catch (IllegalArgumentException unavailable) {}
return modifiers;
Expand Down Expand Up @@ -2606,7 +2607,7 @@ private void applyEarlyTransforms(char[] ctorName, ClassNode classNode, List<Abs
p.setSourcePosition(classNode); // prevent IllegalArgumentException in ASTConverter

decl.arguments = createArguments(new Parameter[] {p}, false);
decl.modifiers = ClassFileConstants.AccPublic;
decl.modifiers = Flags.AccPublic;
decl.returnType = createTypeReferenceForClassNode(ClassHelper.int_TYPE);
decl.selector = "compareTo".toCharArray();
decl.sourceEnd = classNode.getEnd();
Expand All @@ -2632,7 +2633,7 @@ private void applyEarlyTransforms(char[] ctorName, ClassNode classNode, List<Abs

ConstructorDeclaration decl = new ConstructorDeclaration(unitDeclaration.compilationResult);
decl.arguments = createArguments(params, false);
decl.modifiers = ClassFileConstants.AccPublic;
decl.modifiers = Flags.AccPublic;
decl.selector = ctorName;
decl.sourceEnd = classNode.getNameEnd();
decl.sourceStart = classNode.getNameStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.codehaus.jdt.groovy.internal.compiler.ast;

import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -34,10 +33,10 @@
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.FieldDeclarationWithInitializer;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.BooleanConstant;
import org.eclipse.jdt.internal.compiler.impl.ByteConstant;
import org.eclipse.jdt.internal.compiler.impl.CharConstant;
Expand Down Expand Up @@ -146,7 +145,7 @@ private static int getMods(TypeBinding tb) {
return ((ReferenceBinding) tb).modifiers;
} else {
// FIXASC need to be smarter here? Who is affected?
return ClassFileConstants.AccPublic;
return Flags.AccPublic;
}
}

Expand Down Expand Up @@ -340,8 +339,8 @@ private MethodNode methodBindingToMethodNode(MethodBinding methodBinding) {
// If they need to be correct we need to retrieve the method decl from the binding scope

int modifiers = methodBinding.modifiers;
if (jdtBinding.isInterface() && (modifiers & (0x10000 /*Modifier.DEFAULT*/ | Modifier.STATIC)) == 0) {
modifiers |= Modifier.ABSTRACT;
if (jdtBinding.isInterface() && !Flags.isStatic(modifiers) && !Flags.isSynthetic(modifiers) && /*!Flags.isDefaultMethod(modifiers)*/ (modifiers & 0x10000) == 0) {
modifiers |= Flags.AccAbstract;
}

ClassNode returnType = resolver.convertToClassNode(methodBinding.returnType);
Expand Down

0 comments on commit 670a112

Please sign in to comment.