Java Code Examples for com.sun.tools.javac.code.Flags#STATIC

The following examples show how to use com.sun.tools.javac.code.Flags#STATIC . 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: HandleConstructor.java    From EasyMPermission with MIT License 6 votes vote down vote up
public static List<JavacNode> findAllFields(JavacNode typeNode) {
	ListBuffer<JavacNode> fields = new ListBuffer<JavacNode>();
	for (JavacNode child : typeNode.down()) {
		if (child.getKind() != Kind.FIELD) continue;
		JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
		//Skip fields that start with $
		if (fieldDecl.name.toString().startsWith("$")) continue;
		long fieldFlags = fieldDecl.mods.flags;
		//Skip static fields.
		if ((fieldFlags & Flags.STATIC) != 0) continue;
		//Skip initialized final fields
		boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
		if (!isFinal || fieldDecl.init == null) fields.append(child);
	}
	return fields.toList();
}
 
Example 2
Source File: SerializedForm.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private VarSymbol getDefinedSerializableFields(ClassSymbol def) {
    Names names = def.name.table.names;

    /* SERIALIZABLE_FIELDS can be private,
     * so must lookup by ClassSymbol, not by ClassDocImpl.
     */
    for (Scope.Entry e = def.members().lookup(names.fromString(SERIALIZABLE_FIELDS)); e.scope != null; e = e.next()) {
        if (e.sym.kind == Kinds.VAR) {
            VarSymbol f = (VarSymbol)e.sym;
            if ((f.flags() & Flags.STATIC) != 0 &&
                (f.flags() & Flags.PRIVATE) != 0) {
                return f;
            }
        }
    }
    return null;
}
 
Example 3
Source File: TreeMaker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public ModifiersTree removeModifiersModifier(ModifiersTree modifiers, Modifier modifier) {
    long c = ((JCModifiers) modifiers).flags & ~Flags.GENERATEDCONSTR;
    switch (modifier) {
        case ABSTRACT: c = c & ~Flags.ABSTRACT; break;
        case FINAL: c = c & ~Flags.FINAL; break;
        case NATIVE: c = c & ~Flags.NATIVE; break;
        case PRIVATE: c = c & ~Flags.PRIVATE; break;
        case PROTECTED: c = c & ~Flags.PROTECTED; break;
        case PUBLIC: c = c & ~Flags.PUBLIC; break;
        case STATIC: c = c & ~Flags.STATIC; break;
        case STRICTFP: c = c & ~Flags.STRICTFP; break;
        case SYNCHRONIZED: c = c & ~Flags.SYNCHRONIZED; break;
        case TRANSIENT: c = c & ~Flags.TRANSIENT; break;
        case VOLATILE: c = c & ~Flags.VOLATILE; break;
        case DEFAULT: c = c & ~Flags.DEFAULT; break;
        default:
            break;
    }
    return Modifiers(c, modifiers.getAnnotations());
}
 
Example 4
Source File: SerializedForm.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void addMethodIfExist(DocEnv env, ClassSymbol def, String methodName) {
    Names names = def.name.table.names;

    for (Scope.Entry e = def.members().lookup(names.fromString(methodName)); e.scope != null; e = e.next()) {
        if (e.sym.kind == Kinds.MTH) {
            MethodSymbol md = (MethodSymbol)e.sym;
            if ((md.flags() & Flags.STATIC) == 0) {
                /*
                 * WARNING: not robust if unqualifiedMethodName is overloaded
                 *          method. Signature checking could make more robust.
                 * READOBJECT takes a single parameter, java.io.ObjectInputStream.
                 * WRITEOBJECT takes a single parameter, java.io.ObjectOutputStream.
                 */
                methods.append(env.getMethodDoc(md));
            }
        }
    }
}
 
Example 5
Source File: SerializedForm.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void addMethodIfExist(DocEnv env, ClassSymbol def, String methodName) {
    Names names = def.name.table.names;

    for (Scope.Entry e = def.members().lookup(names.fromString(methodName)); e.scope != null; e = e.next()) {
        if (e.sym.kind == Kinds.MTH) {
            MethodSymbol md = (MethodSymbol)e.sym;
            if ((md.flags() & Flags.STATIC) == 0) {
                /*
                 * WARNING: not robust if unqualifiedMethodName is overloaded
                 *          method. Signature checking could make more robust.
                 * READOBJECT takes a single parameter, java.io.ObjectInputStream.
                 * WRITEOBJECT takes a single parameter, java.io.ObjectOutputStream.
                 */
                methods.append(env.getMethodDoc(md));
            }
        }
    }
}
 
Example 6
Source File: GenStubs.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * methods: remove method bodies, make methods native
 */
@Override
public void visitMethodDef(JCMethodDecl tree) {
    tree.mods = translate(tree.mods);
    tree.restype = translate(tree.restype);
    tree.typarams = translateTypeParams(tree.typarams);
    tree.params = translateVarDefs(tree.params);
    tree.thrown = translate(tree.thrown);
    if (tree.body != null) {
        if ((currClassMods & Flags.INTERFACE) != 0) {
            tree.mods.flags &= ~(Flags.DEFAULT | Flags.STATIC);
        } else {
            tree.mods.flags |= Flags.NATIVE;
        }
        tree.body = null;
    }
    result = tree;
}
 
Example 7
Source File: GenStubs.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * methods: remove method bodies, make methods native
 */
@Override
public void visitMethodDef(JCMethodDecl tree) {
    tree.mods = translate(tree.mods);
    tree.restype = translate(tree.restype);
    tree.typarams = translateTypeParams(tree.typarams);
    tree.params = translateVarDefs(tree.params);
    tree.thrown = translate(tree.thrown);
    if (tree.body != null) {
        if ((currClassMods & Flags.INTERFACE) != 0) {
            tree.mods.flags &= ~(Flags.DEFAULT | Flags.STATIC);
        } else {
            tree.mods.flags |= Flags.NATIVE;
        }
        tree.body = null;
    }
    result = tree;
}
 
Example 8
Source File: HandleConstructor.java    From EasyMPermission with MIT License 6 votes vote down vote up
public static List<JavacNode> findRequiredFields(JavacNode typeNode) {
	ListBuffer<JavacNode> fields = new ListBuffer<JavacNode>();
	for (JavacNode child : typeNode.down()) {
		if (child.getKind() != Kind.FIELD) continue;
		JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
		//Skip fields that start with $
		if (fieldDecl.name.toString().startsWith("$")) continue;
		long fieldFlags = fieldDecl.mods.flags;
		//Skip static fields.
		if ((fieldFlags & Flags.STATIC) != 0) continue;
		boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
		boolean isNonNull = !findAnnotations(child, NON_NULL_PATTERN).isEmpty();
		if ((isFinal || isNonNull) && fieldDecl.init == null) fields.append(child);
	}
	return fields.toList();
}
 
Example 9
Source File: GenStubs.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * methods: remove method bodies, make methods native
 */
@Override
public void visitMethodDef(JCMethodDecl tree) {
    tree.mods = translate(tree.mods);
    tree.restype = translate(tree.restype);
    tree.typarams = translateTypeParams(tree.typarams);
    tree.params = translateVarDefs(tree.params);
    tree.thrown = translate(tree.thrown);
    if (tree.body != null) {
        if ((currClassMods & Flags.INTERFACE) != 0) {
            tree.mods.flags &= ~(Flags.DEFAULT | Flags.STATIC);
        } else {
            tree.mods.flags |= Flags.NATIVE;
        }
        tree.body = null;
    }
    result = tree;
}
 
Example 10
Source File: SerializedForm.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private VarSymbol getDefinedSerializableFields(ClassSymbol def) {
    Names names = def.name.table.names;

    /* SERIALIZABLE_FIELDS can be private,
     * so must lookup by ClassSymbol, not by ClassDocImpl.
     */
    for (Scope.Entry e = def.members().lookup(names.fromString(SERIALIZABLE_FIELDS)); e.scope != null; e = e.next()) {
        if (e.sym.kind == Kinds.VAR) {
            VarSymbol f = (VarSymbol)e.sym;
            if ((f.flags() & Flags.STATIC) != 0 &&
                (f.flags() & Flags.PRIVATE) != 0) {
                return f;
            }
        }
    }
    return null;
}
 
Example 11
Source File: SerializedForm.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private void computeDefaultSerializableFields(DocEnv env,
                                              ClassSymbol def,
                                              ClassDocImpl cd) {
    for (Scope.Entry e = def.members().elems; e != null; e = e.sibling) {
        if (e.sym != null && e.sym.kind == Kinds.VAR) {
            VarSymbol f = (VarSymbol)e.sym;
            if ((f.flags() & Flags.STATIC) == 0 &&
                (f.flags() & Flags.TRANSIENT) == 0) {
                //### No modifier filtering applied here.
                FieldDocImpl fd = env.getFieldDoc(f);
                //### Add to beginning.
                //### Preserve order used by old 'javadoc'.
                fields.prepend(fd);
            }
        }
    }
}
 
Example 12
Source File: SerializedForm.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void addMethodIfExist(DocEnv env, ClassSymbol def, String methodName) {
    Names names = def.name.table.names;

    for (Scope.Entry e = def.members().lookup(names.fromString(methodName)); e.scope != null; e = e.next()) {
        if (e.sym.kind == Kinds.MTH) {
            MethodSymbol md = (MethodSymbol)e.sym;
            if ((md.flags() & Flags.STATIC) == 0) {
                /*
                 * WARNING: not robust if unqualifiedMethodName is overloaded
                 *          method. Signature checking could make more robust.
                 * READOBJECT takes a single parameter, java.io.ObjectInputStream.
                 * WRITEOBJECT takes a single parameter, java.io.ObjectOutputStream.
                 */
                methods.append(env.getMethodDoc(md));
            }
        }
    }
}
 
Example 13
Source File: PackageGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
ListBuffer<JCTree> processConstants(Element constNode, HashMap<String, Integer> scope) {
    String baseName = constNode.getAttribute("basename");
    int count = 1;
    try {
        count = Integer.parseInt(constNode.getAttribute("count"));
    } catch (Exception e) {} // nothing to do, will use count = 1

    long declFlags = Flags.PUBLIC | Flags.STATIC | Flags.FINAL | Flags.ENUM;
    ListBuffer<JCTree> fields = new ListBuffer<>();

    for (int i = 0; i < count; i++) {
        String declName = baseName +
                          ((count == 1) ? "" : getUniqIndex(scope, baseName));

        JCVariableDecl constDecl = make.VarDef(
                                       make.Modifiers(declFlags),
                                       names.fromString(declName),
                                       null,  // no need for type in enum decl
                                       null); // no init

        fields.append(constDecl);
    }
    return fields;
}
 
Example 14
Source File: HandleTable.java    From sqlitemagic with Apache License 2.0 5 votes vote down vote up
private static Set<String> findAllStaticMethodNames(JavacNode typeNode) {
  Set<String> methodNames = new LinkedHashSet<>();
  for (JavacNode child : typeNode.down()) {
    if (child.getKind() != AST.Kind.METHOD) continue;
    JCTree.JCMethodDecl methodDecl = (JCTree.JCMethodDecl) child.get();
    long methodFlags = methodDecl.mods.flags;
    //Take static methods
    if ((methodFlags & Flags.STATIC) != 0) {
      methodNames.add(child.getName());
    }
  }
  return methodNames;
}
 
Example 15
Source File: AbstractSrcMethod.java    From manifold with Apache License 2.0 5 votes vote down vote up
private boolean isNonDefaultNonStaticInterfaceMethod()
{
  return getOwner() instanceof AbstractSrcClass &&
         ((AbstractSrcClass)getOwner()).isInterface() &&
         (getModifiers() & Flags.DEFAULT) == 0 &&
         (getModifiers() & Flags.STATIC) == 0;
}
 
Example 16
Source File: ElementsService.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Element getImplementationOf(ExecutableElement method, TypeElement origin) {
    MethodSymbol msym = (MethodSymbol)method;
    MethodSymbol implmethod = (msym).implementation((TypeSymbol)origin, jctypes, true);
    if ((msym.flags() & Flags.STATIC) != 0) {
        // return null if outside of hierarchy, or the method itself if origin extends method's class
        if (jctypes.isSubtype(((TypeSymbol)origin).type,  ((TypeSymbol)((MethodSymbol)method).owner).type)) {
            return method;
        } else {
            return null;
        }
    }
    if (implmethod == null || implmethod == method) {
        //look for default implementations
        if (allowDefaultMethods) {
            com.sun.tools.javac.util.List<MethodSymbol> candidates = jctypes.interfaceCandidates(((TypeSymbol) origin).type, (MethodSymbol) method);
            X: for (com.sun.tools.javac.util.List<MethodSymbol> ptr = candidates; ptr.head != null; ptr = ptr.tail) {
                MethodSymbol prov = ptr.head;
                if (prov != null && prov.overrides((MethodSymbol) method, (TypeSymbol) origin, jctypes, true) &&
                    hasImplementation(prov)) {
                    // PENDING: even if `prov' overrides the method, there may be a different method, in different interface, that overrides `method'
                    // 'prov' must override all such compatible methods in order to present a valid implementation of `method'.
                    for (com.sun.tools.javac.util.List<MethodSymbol> sibling = candidates; sibling.head != null; sibling = sibling.tail) {
                        MethodSymbol redeclare = sibling.head;

                        // if the default method does not override the alternative candidate from an interface, then the default will be rejected
                        // as specified in JLS #8, par. 8.4.8
                        if (!prov.overrides(redeclare, (TypeSymbol)origin, jctypes, allowDefaultMethods)) {
                            break X;
                        }
                    }
                    implmethod = prov;
                    break;
                }
            }
        }
    }
    return implmethod;
}
 
Example 17
Source File: HandleUtilityClass.java    From EasyMPermission with MIT License 5 votes vote down vote up
private void changeModifiersAndGenerateConstructor(JavacNode typeNode, JavacNode errorNode) {
	JCClassDecl classDecl = (JCClassDecl) typeNode.get();
	
	boolean makeConstructor = true;
	
	classDecl.mods.flags |= Flags.FINAL;
	
	boolean markStatic = true;
	
	if (typeNode.up().getKind() == Kind.COMPILATION_UNIT) markStatic = false;
	if (markStatic && typeNode.up().getKind() == Kind.TYPE) {
		JCClassDecl typeDecl = (JCClassDecl) typeNode.up().get();
		if ((typeDecl.mods.flags & Flags.INTERFACE) != 0) markStatic = false;
	}
	
	if (markStatic) classDecl.mods.flags |= Flags.STATIC;
	
	for (JavacNode element : typeNode.down()) {
		if (element.getKind() == Kind.FIELD) {
			JCVariableDecl fieldDecl = (JCVariableDecl) element.get();
			fieldDecl.mods.flags |= Flags.STATIC;
		} else if (element.getKind() == Kind.METHOD) {
			JCMethodDecl methodDecl = (JCMethodDecl) element.get();
			if (methodDecl.name.contentEquals("<init>")) {
				if (getGeneratedBy(methodDecl) == null && (methodDecl.mods.flags & Flags.GENERATEDCONSTR) == 0) {
					element.addError("@UtilityClasses cannot have declared constructors.");
					makeConstructor = false;
					continue;
				}
			}
			
			methodDecl.mods.flags |= Flags.STATIC;
		} else if (element.getKind() == Kind.TYPE) {
			JCClassDecl innerClassDecl = (JCClassDecl) element.get();
			innerClassDecl.mods.flags |= Flags.STATIC;
		}
	}
	
	if (makeConstructor) createPrivateDefaultConstructor(typeNode);
}
 
Example 18
Source File: HandleWither.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void generateWitherForType(JavacNode typeNode, JavacNode errorNode, AccessLevel level, boolean checkForTypeLevelWither) {
	if (checkForTypeLevelWither) {
		if (hasAnnotation(Wither.class, typeNode)) {
			//The annotation will make it happen, so we can skip it.
			return;
		}
	}
	
	JCClassDecl typeDecl = null;
	if (typeNode.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) typeNode.get();
	long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags;
	boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0;
	
	if (typeDecl == null || notAClass) {
		errorNode.addError("@Wither is only supported on a class or a field.");
		return;
	}
	
	for (JavacNode field : typeNode.down()) {
		if (field.getKind() != Kind.FIELD) continue;
		JCVariableDecl fieldDecl = (JCVariableDecl) field.get();
		//Skip fields that start with $
		if (fieldDecl.name.toString().startsWith("$")) continue;
		//Skip static fields.
		if ((fieldDecl.mods.flags & Flags.STATIC) != 0) continue;
		//Skip final initialized fields.
		if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) continue;
		
		generateWitherForField(field, errorNode.get(), level);
	}
}
 
Example 19
Source File: HandleWither.java    From EasyMPermission with MIT License 4 votes vote down vote up
public void createWitherForField(AccessLevel level, JavacNode fieldNode, JavacNode source, boolean whineIfExists, List<JCAnnotation> onMethod, List<JCAnnotation> onParam) {
	if (fieldNode.getKind() != Kind.FIELD) {
		fieldNode.addError("@Wither is only supported on a class or a field.");
		return;
	}
	
	JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get();
	String methodName = toWitherName(fieldNode);
	
	if (methodName == null) {
		fieldNode.addWarning("Not generating wither for this field: It does not fit your @Accessors prefix list.");
		return;
	}
	
	if ((fieldDecl.mods.flags & Flags.STATIC) != 0) {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for static fields.");
		return;
	}
	
	if ((fieldDecl.mods.flags & Flags.FINAL) != 0 && fieldDecl.init != null) {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for final, initialized fields.");
		return;
	}
	
	if (fieldDecl.name.toString().startsWith("$")) {
		fieldNode.addWarning("Not generating wither for this field: Withers cannot be generated for fields starting with $.");
		return;
	}
	
	for (String altName : toAllWitherNames(fieldNode)) {
		switch (methodExists(altName, fieldNode, false, 1)) {
		case EXISTS_BY_LOMBOK:
			return;
		case EXISTS_BY_USER:
			if (whineIfExists) {
				String altNameExpl = "";
				if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName);
				fieldNode.addWarning(
					String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
			}
			return;
		default:
		case NOT_EXISTS:
			//continue scanning the other alt names.
		}
	}
	
	long access = toJavacModifier(level);
	
	JCMethodDecl createdWither = createWither(access, fieldNode, fieldNode.getTreeMaker(), source, onMethod, onParam);
	injectMethod(fieldNode.up(), createdWither);
}
 
Example 20
Source File: HandleGetter.java    From EasyMPermission with MIT License 4 votes vote down vote up
public void createGetterForField(AccessLevel level,
		JavacNode fieldNode, JavacNode source, boolean whineIfExists, boolean lazy, List<JCAnnotation> onMethod) {
	if (fieldNode.getKind() != Kind.FIELD) {
		source.addError("@Getter is only supported on a class or a field.");
		return;
	}
	
	JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get();
	
	if (lazy) {
		if ((fieldDecl.mods.flags & Flags.PRIVATE) == 0 || (fieldDecl.mods.flags & Flags.FINAL) == 0) {
			source.addError("'lazy' requires the field to be private and final.");
			return;
		}
		if (fieldDecl.init == null) {
			source.addError("'lazy' requires field initialization.");
			return;
		}
	}
	
	String methodName = toGetterName(fieldNode);
	
	if (methodName == null) {
		source.addWarning("Not generating getter for this field: It does not fit your @Accessors prefix list.");
		return;
	}
	
	for (String altName : toAllGetterNames(fieldNode)) {
		switch (methodExists(altName, fieldNode, false, 0)) {
		case EXISTS_BY_LOMBOK:
			return;
		case EXISTS_BY_USER:
			if (whineIfExists) {
				String altNameExpl = "";
				if (!altName.equals(methodName)) altNameExpl = String.format(" (%s)", altName);
				source.addWarning(
					String.format("Not generating %s(): A method with that name already exists%s", methodName, altNameExpl));
			}
			return;
		default:
		case NOT_EXISTS:
			//continue scanning the other alt names.
		}
	}
	
	long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC);
	
	injectMethod(fieldNode.up(), createGetter(access, fieldNode, fieldNode.getTreeMaker(), source.get(), lazy, onMethod));
}