com.sun.tools.javac.tree.JCTree.JCModifiers Java Examples

The following examples show how to use com.sun.tools.javac.tree.JCTree.JCModifiers. 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: ImmutableTreeTranslator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected final MethodTree rewriteChildren(MethodTree tree) {
       ModifiersTree mods = (ModifiersTree)translate(tree.getModifiers());
ExpressionTree restype = (ExpressionTree)translateClassRef(tree.getReturnType());
List<? extends TypeParameterTree> typarams = translateStable(tree.getTypeParameters());
List<? extends VariableTree> params = translateStable(tree.getParameters());
List<? extends ExpressionTree> thrown = translate(tree.getThrows());
       ExpressionTree defaultValue = (ExpressionTree)translate(tree.getDefaultValue());
BlockTree body = (BlockTree)translate(tree.getBody());
       
if (restype!=tree.getReturnType() || !typarams.equals(tree.getTypeParameters()) || 
           !params.equals(tree.getParameters()) || !thrown.equals(tree.getThrows()) || 
           mods!=tree.getModifiers() || defaultValue!=tree.getDefaultValue() || 
           body!=tree.getBody()) {
           if ((((JCModifiers) mods).flags & Flags.GENERATEDCONSTR) != 0) {
               mods = make.Modifiers(((JCModifiers) mods).flags & ~Flags.GENERATEDCONSTR, mods.getAnnotations());
           }
           MethodTree n  = make.Method(mods, tree.getName().toString(), restype, typarams,
                                       params, thrown, body, defaultValue);
           
    copyCommentTo(tree,n);
           copyPosTo(tree,n);
    tree = n;
}
return tree;
   }
 
Example #2
Source File: TreeMaker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public ModifiersTree addModifiersModifier(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 #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: JavacJavaUtilListSetSingularizer.java    From EasyMPermission with MIT License 6 votes vote down vote up
void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
	List<JCTypeParameter> typeParams = List.nil();
	List<JCExpression> thrown = List.nil();
	
	JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
	ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
	statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source));
	JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "addAll");
	JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getPluralName())));
	statements.append(maker.Exec(invokeAdd));
	if (returnStatement != null) statements.append(returnStatement);
	JCBlock body = maker.Block(0, statements.toList());
	Name name = data.getPluralName();
	long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
	if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("addAll", name.toString()));
	JCExpression paramType = chainDots(builderType, "java", "util", "Collection");
	paramType = addTypeArgs(1, true, builderType, paramType, data.getTypeArgs(), source);
	JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null);
	JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null);
	injectMethod(builderType, method);
}
 
Example #5
Source File: JavacJavaUtilListSetSingularizer.java    From EasyMPermission with MIT License 6 votes vote down vote up
void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
	List<JCTypeParameter> typeParams = List.nil();
	List<JCExpression> thrown = List.nil();
	
	JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
	ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
	statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, false, source));
	JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), "add");
	JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, List.<JCExpression>of(maker.Ident(data.getSingularName())));
	statements.append(maker.Exec(invokeAdd));
	if (returnStatement != null) statements.append(returnStatement);
	JCBlock body = maker.Block(0, statements.toList());
	Name name = data.getSingularName();
	long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
	if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("add", name.toString()));
	JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
	JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null);
	JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), thrown, body, null);
	injectMethod(builderType, method);
}
 
Example #6
Source File: HandleEqualsAndHashCode.java    From EasyMPermission with MIT License 6 votes vote down vote up
public JCMethodDecl createCanEqual(JavacNode typeNode, JCTree source, List<JCAnnotation> onParam) {
	/* public boolean canEqual(final java.lang.Object other) {
	 *     return other instanceof Outer.Inner.MyType;
	 * }
	 */
	JavacTreeMaker maker = typeNode.getTreeMaker();
	
	JCModifiers mods = maker.Modifiers(Flags.PROTECTED, List.<JCAnnotation>nil());
	JCExpression returnType = maker.TypeIdent(CTC_BOOLEAN);
	Name canEqualName = typeNode.toName("canEqual");
	JCExpression objectType = genJavaLangTypeRef(typeNode, "Object");
	Name otherName = typeNode.toName("other");
	long flags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, typeNode.getContext());
	List<JCVariableDecl> params = List.of(maker.VarDef(maker.Modifiers(flags, onParam), otherName, objectType, null));
	
	JCBlock body = maker.Block(0, List.<JCStatement>of(
			maker.Return(maker.TypeTest(maker.Ident(otherName), createTypeReference(typeNode)))));
	
	return recursiveSetGeneratedBy(maker.MethodDef(mods, canEqualName, returnType, List.<JCTypeParameter>nil(), params, List.<JCExpression>nil(), body, null), source, typeNode.getContext());
}
 
Example #7
Source File: HandleBuilder.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void generateBuilderFields(JavacNode builderType, java.util.List<BuilderFieldData> builderFields, JCTree source) {
	int len = builderFields.size();
	java.util.List<JavacNode> existing = new ArrayList<JavacNode>();
	for (JavacNode child : builderType.down()) {
		if (child.getKind() == Kind.FIELD) existing.add(child);
	}
	
	top:
	for (int i = len - 1; i >= 0; i--) {
		BuilderFieldData bfd = builderFields.get(i);
		if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) {
			bfd.createdFields.addAll(bfd.singularData.getSingularizer().generateFields(bfd.singularData, builderType, source));
		} else {
			for (JavacNode exists : existing) {
				Name n = ((JCVariableDecl) exists.get()).name;
				if (n.equals(bfd.name)) {
					bfd.createdFields.add(exists);
					continue top;
				}
			}
			JavacTreeMaker maker = builderType.getTreeMaker();
			JCModifiers mods = maker.Modifiers(Flags.PRIVATE);
			JCVariableDecl newField = maker.VarDef(mods, bfd.name, cloneType(maker, bfd.type, source, builderType.getContext()), null);
			bfd.createdFields.add(injectFieldAndMarkGenerated(builderType, newField));
		}
	}
}
 
Example #8
Source File: T6397044.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private void check(CharSequence name, ModifiersTree modifiers) {
    long sysflags = ((JCModifiers) modifiers).flags;
    System.err.println(name + ": " + modifiers.getFlags() + " | " + Flags.toString(sysflags));
    if (name.toString().startsWith("x_")) {
        String expected = "[" + name.toString().substring(2) + "]";
        String found = modifiers.getFlags().toString();
        if (!found.equals(expected))
            throw new AssertionError("expected: " + expected + "; found: " + found);
    }
}
 
Example #9
Source File: JavacGuavaSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
protected void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
	List<JCTypeParameter> typeParams = List.nil();
	List<JCExpression> thrown = List.nil();
	boolean mapMode = isMap();
	
	JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
	ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
	statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, mapMode, source));
	JCExpression thisDotFieldDotAddAll = chainDots(builderType, "this", data.getPluralName().toString(), mapMode ? "putAll" : "addAll");
	JCExpression invokeAddAll = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAddAll, List.<JCExpression>of(maker.Ident(data.getPluralName())));
	statements.append(maker.Exec(invokeAddAll));
	if (returnStatement != null) statements.append(returnStatement);
	JCBlock body = maker.Block(0, statements.toList());
	Name methodName = data.getPluralName();
	long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
	if (!fluent) methodName = builderType.toName(HandlerUtil.buildAccessorName(mapMode ? "putAll" : "addAll", methodName.toString()));
	JCExpression paramType;
	if (mapMode) {
		paramType = chainDots(builderType, "java", "util", "Map");
	} else {
		paramType = genJavaLangTypeRef(builderType, "Iterable");
	}
	paramType = addTypeArgs(mapMode ? 2 : 1, true, builderType, paramType, data.getTypeArgs(), source);
	JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null);
	JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, List.of(param), thrown, body, null);
	injectMethod(builderType, method);
}
 
Example #10
Source File: T6397044.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void check(CharSequence name, ModifiersTree modifiers) {
    long sysflags = ((JCModifiers) modifiers).flags;
    System.err.println(name + ": " + modifiers.getFlags() + " | " + Flags.toString(sysflags));
    if (name.toString().startsWith("x_")) {
        String expected = "[" + name.toString().substring(2) + "]";
        String found = modifiers.getFlags().toString();
        if (!found.equals(expected))
            throw new AssertionError("expected: " + expected + "; found: " + found);
    }
}
 
Example #11
Source File: JavacGuavaSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
void generateSingularMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
	List<JCTypeParameter> typeParams = List.nil();
	List<JCExpression> thrown = List.nil();
	boolean mapMode = isMap();
	
	Name keyName = !mapMode ? data.getSingularName() : builderType.toName(data.getSingularName() + "$key");
	Name valueName = !mapMode ? null : builderType.toName(data.getSingularName() + "$value");
	
	JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
	ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
	statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, mapMode, source));
	JCExpression thisDotFieldDotAdd = chainDots(builderType, "this", data.getPluralName().toString(), mapMode ? "put" : "add");
	List<JCExpression> invokeAddExpr;
	if (mapMode) {
		invokeAddExpr = List.<JCExpression>of(maker.Ident(keyName), maker.Ident(valueName));
	} else {
		invokeAddExpr = List.<JCExpression>of(maker.Ident(keyName));
	}
	JCExpression invokeAdd = maker.Apply(List.<JCExpression>nil(), thisDotFieldDotAdd, invokeAddExpr);
	statements.append(maker.Exec(invokeAdd));
	if (returnStatement != null) statements.append(returnStatement);
	JCBlock body = maker.Block(0, statements.toList());
	Name methodName = data.getSingularName();
	long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
	if (!fluent) methodName = builderType.toName(HandlerUtil.buildAccessorName(mapMode ? "put" : "add", methodName.toString()));
	List<JCVariableDecl> params;
	if (mapMode) {
		JCExpression keyType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
		JCExpression valueType = cloneParamType(1, maker, data.getTypeArgs(), builderType, source);
		JCVariableDecl paramKey = maker.VarDef(maker.Modifiers(paramFlags), keyName, keyType, null);
		JCVariableDecl paramValue = maker.VarDef(maker.Modifiers(paramFlags), valueName, valueType, null);
		params = List.of(paramKey, paramValue);
	} else {
		JCExpression paramType = cloneParamType(0, maker, data.getTypeArgs(), builderType, source);
		params = List.of(maker.VarDef(maker.Modifiers(paramFlags), data.getSingularName(), paramType, null));
	}
	JCMethodDecl method = maker.MethodDef(mods, methodName, returnType, typeParams, params, thrown, body, null);
	injectMethod(builderType, method);
}
 
Example #12
Source File: T6397044.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void check(CharSequence name, ModifiersTree modifiers) {
    long sysflags = ((JCModifiers) modifiers).flags;
    System.err.println(name + ": " + modifiers.getFlags() + " | " + Flags.toString(sysflags));
    if (name.toString().startsWith("x_")) {
        String expected = "[" + name.toString().substring(2) + "]";
        String found = modifiers.getFlags().toString();
        if (!found.equals(expected))
            throw new AssertionError("expected: " + expected + "; found: " + found);
    }
}
 
Example #13
Source File: JavacJavaUtilMapSingularizer.java    From EasyMPermission with MIT License 5 votes vote down vote up
private void generatePluralMethod(JavacTreeMaker maker, JCExpression returnType, JCStatement returnStatement, SingularData data, JavacNode builderType, JCTree source, boolean fluent) {
	List<JCTypeParameter> typeParams = List.nil();
	List<JCExpression> jceBlank = List.nil();
	JCModifiers mods = maker.Modifiers(Flags.PUBLIC);
	ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>();
	statements.append(createConstructBuilderVarIfNeeded(maker, data, builderType, true, source));
	long paramFlags = JavacHandlerUtil.addFinalIfNeeded(Flags.PARAMETER, builderType.getContext());
	long baseFlags = JavacHandlerUtil.addFinalIfNeeded(0, builderType.getContext());
	Name entryName = builderType.toName("$lombokEntry");
	
	JCExpression forEachType = chainDots(builderType, "java", "util", "Map", "Entry");
	forEachType = addTypeArgs(2, true, builderType, forEachType, data.getTypeArgs(), source);
	JCExpression keyArg = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(entryName), builderType.toName("getKey")), List.<JCExpression>nil());
	JCExpression valueArg = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(entryName), builderType.toName("getValue")), List.<JCExpression>nil());
	JCExpression addKey = maker.Apply(List.<JCExpression>nil(), chainDots(builderType, "this", data.getPluralName() + "$key", "add"), List.of(keyArg));
	JCExpression addValue = maker.Apply(List.<JCExpression>nil(), chainDots(builderType, "this", data.getPluralName() + "$value", "add"), List.of(valueArg));
	JCBlock forEachBody = maker.Block(0, List.<JCStatement>of(maker.Exec(addKey), maker.Exec(addValue)));
	JCExpression entrySetInvocation = maker.Apply(jceBlank, maker.Select(maker.Ident(data.getPluralName()), builderType.toName("entrySet")), jceBlank);
	JCStatement forEach = maker.ForeachLoop(maker.VarDef(maker.Modifiers(baseFlags), entryName, forEachType, null), entrySetInvocation, forEachBody);
	statements.append(forEach);
	
	if (returnStatement != null) statements.append(returnStatement);
	JCBlock body = maker.Block(0, statements.toList());
	Name name = data.getPluralName();
	if (!fluent) name = builderType.toName(HandlerUtil.buildAccessorName("putAll", name.toString()));
	JCExpression paramType = chainDots(builderType, "java", "util", "Map");
	paramType = addTypeArgs(2, true, builderType, paramType, data.getTypeArgs(), source);
	JCVariableDecl param = maker.VarDef(maker.Modifiers(paramFlags), data.getPluralName(), paramType, null);
	JCMethodDecl method = maker.MethodDef(mods, name, returnType, typeParams, List.of(param), jceBlank, body, null);
	injectMethod(builderType, method);
}
 
Example #14
Source File: T6397044.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void check(CharSequence name, ModifiersTree modifiers) {
    long sysflags = ((JCModifiers) modifiers).flags;
    System.err.println(name + ": " + modifiers.getFlags() + " | " + Flags.toString(sysflags));
    if (name.toString().startsWith("x_")) {
        String expected = "[" + name.toString().substring(2) + "]";
        String found = modifiers.getFlags().toString();
        if (!found.equals(expected))
            throw new AssertionError("expected: " + expected + "; found: " + found);
    }
}
 
Example #15
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
public static void addGenerated(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) {
	if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateGenerated()) return;
	
	if (!Boolean.FALSE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_GENERATED_ANNOTATIONS))) {
		addAnnotation(mods, node, pos, source, context, "javax.annotation.Generated", node.getTreeMaker().Literal("lombok"));
	}
}
 
Example #16
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 5 votes vote down vote up
public static void addSuppressWarningsAll(JCModifiers mods, JavacNode node, int pos, JCTree source, Context context) {
	if (!LombokOptionsFactory.getDelombokOptions(context).getFormatPreferences().generateSuppressWarnings()) return;
	addAnnotation(mods, node, pos, source, context, "java.lang.SuppressWarnings", node.getTreeMaker().Literal("all"));
	
	if (Boolean.TRUE.equals(node.getAst().readConfiguration(ConfigurationKeys.ADD_FINDBUGS_SUPPRESSWARNINGS_ANNOTATIONS))) {
		JavacTreeMaker maker = node.getTreeMaker();
		JCExpression arg = maker.Assign(maker.Ident(node.toName("justification")), maker.Literal("generated code"));
		addAnnotation(mods, node, pos, source, context, "edu.umd.cs.findbugs.annotations.SuppressFBWarnings", arg);
	}
}
 
Example #17
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void visitModifiers(JCModifiers mods) {
	try {
		printAnnotations(mods.annotations);
		printFlags(mods.flags);
	} catch (IOException e) {
		throw new UncheckedIOException(e);
	}
}
 
Example #18
Source File: HandleConstructor.java    From EasyMPermission with MIT License 5 votes vote down vote up
public static void addConstructorProperties(JCModifiers mods, JavacNode node, List<JavacNode> fields) {
	if (fields.isEmpty()) return;
	JavacTreeMaker maker = node.getTreeMaker();
	JCExpression constructorPropertiesType = chainDots(node, "java", "beans", "ConstructorProperties");
	ListBuffer<JCExpression> fieldNames = new ListBuffer<JCExpression>();
	for (JavacNode field : fields) {
		Name fieldName = removePrefixFromField(field);
		fieldNames.append(maker.Literal(fieldName.toString()));
	}
	JCExpression fieldNamesArray = maker.NewArray(null, List.<JCExpression>nil(), fieldNames.toList());
	JCAnnotation annotation = maker.Annotation(constructorPropertiesType, List.of(fieldNamesArray));
	mods.annotations = mods.annotations.append(annotation);
}
 
Example #19
Source File: HandleValue.java    From EasyMPermission with MIT License 5 votes vote down vote up
@Override public void handle(AnnotationValues<Value> annotation, JCAnnotation ast, JavacNode annotationNode) {
	@SuppressWarnings("deprecation")
	Class<? extends Annotation> oldExperimentalValue = lombok.experimental.Value.class;
	
	handleFlagUsage(annotationNode, ConfigurationKeys.VALUE_FLAG_USAGE, "@Value");
	
	deleteAnnotationIfNeccessary(annotationNode, Value.class, oldExperimentalValue);
	JavacNode typeNode = annotationNode.up();
	boolean notAClass = !isClass(typeNode);
	
	if (notAClass) {
		annotationNode.addError("@Value is only supported on a class.");
		return;
	}
	
	String staticConstructorName = annotation.getInstance().staticConstructor();
	
	if (!hasAnnotationAndDeleteIfNeccessary(NonFinal.class, typeNode)) {
		JCModifiers jcm = ((JCClassDecl) typeNode.get()).mods;
		if ((jcm.flags & Flags.FINAL) == 0) {
			jcm.flags |= Flags.FINAL;
			typeNode.rebuild();
		}
	}
	new HandleFieldDefaults().generateFieldDefaultsForType(typeNode, annotationNode, AccessLevel.PRIVATE, true, true);
	
	// TODO move this to the end OR move it to the top in eclipse.
	new HandleConstructor().generateAllArgsConstructor(typeNode, AccessLevel.PUBLIC, staticConstructorName, SkipIfConstructorExists.YES, annotationNode);
	new HandleGetter().generateGetterForType(typeNode, annotationNode, AccessLevel.PUBLIC, true);
	new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
	new HandleToString().generateToStringForType(typeNode, annotationNode);
}
 
Example #20
Source File: HandleUtilityClass.java    From EasyMPermission with MIT License 5 votes vote down vote up
private void createPrivateDefaultConstructor(JavacNode typeNode) {
	JavacTreeMaker maker = typeNode.getTreeMaker();
	JCModifiers mods = maker.Modifiers(Flags.PRIVATE, List.<JCAnnotation>nil());
	
	Name name = typeNode.toName("<init>");
	JCBlock block = maker.Block(0L, createThrowStatement(typeNode, maker));
	JCMethodDecl methodDef = maker.MethodDef(mods, name, null, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), block, null);
	JCMethodDecl constructor = recursiveSetGeneratedBy(methodDef, typeNode.get(), typeNode.getContext());
	JavacHandlerUtil.injectMethod(typeNode, constructor);
}
 
Example #21
Source File: NBTreeMaker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected IndexedClassDecl(JCModifiers mods,
                           Name name,
                           List<JCTypeParameter> typarams,
                           JCExpression extending,
                           List<JCExpression> implementing,
                           List<JCTree> defs,
                           ClassSymbol sym) {
    super(mods, name, typarams, extending, implementing, defs, sym);
    this.index = -1;
}
 
Example #22
Source File: T6397044.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void check(CharSequence name, ModifiersTree modifiers) {
    long sysflags = ((JCModifiers) modifiers).flags;
    System.err.println(name + ": " + modifiers.getFlags() + " | " + Flags.toString(sysflags));
    if (name.toString().startsWith("x_")) {
        String expected = "[" + name.toString().substring(2) + "]";
        String found = modifiers.getFlags().toString();
        if (!found.equals(expected))
            throw new AssertionError("expected: " + expected + "; found: " + found);
    }
}
 
Example #23
Source File: T6397044.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private void check(CharSequence name, ModifiersTree modifiers) {
    long sysflags = ((JCModifiers) modifiers).flags;
    System.err.println(name + ": " + modifiers.getFlags() + " | " + Flags.toString(sysflags));
    if (name.toString().startsWith("x_")) {
        String expected = "[" + name.toString().substring(2) + "]";
        String found = modifiers.getFlags().toString();
        if (!found.equals(expected))
            throw new AssertionError("expected: " + expected + "; found: " + found);
    }
}
 
Example #24
Source File: T6397044.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void check(CharSequence name, ModifiersTree modifiers) {
    long sysflags = ((JCModifiers) modifiers).flags;
    System.err.println(name + ": " + modifiers.getFlags() + " | " + Flags.toString(sysflags));
    if (name.toString().startsWith("x_")) {
        String expected = "[" + name.toString().substring(2) + "]";
        String found = modifiers.getFlags().toString();
        if (!found.equals(expected))
            throw new AssertionError("expected: " + expected + "; found: " + found);
    }
}
 
Example #25
Source File: JackpotTrees.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static JCVariableDecl createVariableWildcard(Context ctx, Name name) {
    TreeMaker make = TreeMaker.instance(ctx);
    JCIdent jcIdent = make.Ident(name);

    JCErroneous err = new JCErroneous(List.<JCTree>nil()) {};

    err.type = Symtab.instance(ctx).errType;

    JCVariableDecl var;
    
    try {
        var = createInstance(ctx,
                             JCVariableDecl.class,
                             name,
                             jcIdent,
                             new Class<?>[] {JCModifiers.class, Name.class, JCExpression.class, JCExpression.class, VarSymbol.class},
                             new Object[] {new FakeModifiers(), name, err, null, null});
    } catch (IllegalStateException ex) {
        try {
            var = createInstance(ctx,
                                 JCVariableDecl.class,
                                 name,
                                 jcIdent,
                                 new Class<?>[] {JCModifiers.class, Name.class, JCExpression.class, JCExpression.class, VarSymbol.class, List.class},
                                 new Object[] {new FakeModifiers(), name, err, null, null, List.nil()});
        } catch (IllegalStateException ex2) {
            throw ex;
        }
    }

    var.sym = new VarSymbol(0, name, var.vartype.type, Symtab.instance(ctx).errSymbol);
    var.type = var.vartype.type;
    return var;
}
 
Example #26
Source File: NBTreeMaker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public JCClassDecl ClassDef(JCModifiers mods, Name name, List<JCTypeParameter> typarams, JCExpression extending, List<JCExpression> implementing, List<JCTree> defs) {
    try {
        IndexedClassDecl result = new IndexedClassDecl(mods, name, typarams, extending, implementing, defs, null);

        result.pos = pos;

        return result;
    } catch (NoSuchMethodError err) {
        return super.ClassDef(mods, name, typarams, extending, implementing, defs);
    }
}
 
Example #27
Source File: TreeFinder.java    From annotation-tools with MIT License 5 votes vote down vote up
/**
 * Returns the start position of the method's name.  In particular,
 * works properly for constructors, for which the name field in the
 * AST is always "<init>" instead of the name from the source.
 *
 * @param node AST node of method declaration
 * @return position of method name (from {@link JCMethodDecl#sym}) in source
 */
private int findMethodName(JCMethodDecl node) {
  String sym = node.sym.toString();
  String name = sym.substring(0, sym.indexOf('('));
  JCModifiers mods = node.getModifiers();
  JCBlock body = node.body;
  if ((mods.flags & Flags.GENERATEDCONSTR) != 0) { return Position.NOPOS; }
  int nodeStart = node.getStartPosition();
  int nodeEnd = node.getEndPosition(tree.endPositions);
  int nodeLength = nodeEnd - nodeStart;
  int modsLength = mods.getEndPosition(tree.endPositions)
      - mods.getStartPosition();  // can't trust string length!
  int bodyLength = body == null ? 1
      : body.getEndPosition(tree.endPositions) - body.getStartPosition();
  int start = nodeStart + modsLength;
  int end = nodeStart + nodeLength - bodyLength;
  int angle = name.lastIndexOf('>');  // check for type params
  if (angle >= 0) { name = name.substring(angle + 1); }

  try {
    CharSequence s = tree.getSourceFile().getCharContent(true);
    String regex = "\\b" + Pattern.quote(name) + "\\b";  // sufficient?
    Pattern pat = Pattern.compile(regex, Pattern.MULTILINE);
    Matcher mat = pat.matcher(s).region(start, end);
    return mat.find() ? mat.start() : Position.NOPOS;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #28
Source File: NBParserFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected JCClassDecl classDeclaration(JCModifiers mods, Comment dc) {
    if (cancelService != null) {
        cancelService.abortIfCanceled();
    }
    return super.classDeclaration(mods, dc);
}
 
Example #29
Source File: NBParserFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected JCClassDecl interfaceDeclaration(JCModifiers mods, Comment dc) {
    if (cancelService != null) {
        cancelService.abortIfCanceled();
    }
    return super.interfaceDeclaration(mods, dc);
}
 
Example #30
Source File: NBParserFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected JCClassDecl enumDeclaration(JCModifiers mods, Comment dc) {
    if (cancelService != null) {
        cancelService.abortIfCanceled();
    }
    return super.enumDeclaration(mods, dc);
}