com.sun.tools.javac.tree.JCTree.JCBlock Java Examples
The following examples show how to use
com.sun.tools.javac.tree.JCTree.JCBlock.
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: CompilationUnitBuilder.java From j2cl with Apache License 2.0 | 7 votes |
private Block convertLambdaBody(JCTree lambdaBody, TypeDescriptor returnTypeDescriptor) { Block body; if (lambdaBody.getKind() == Kind.BLOCK) { body = convertBlock((JCBlock) lambdaBody); } else { checkArgument(lambdaBody instanceof JCExpression); Expression lambdaMethodBody = convertExpression((JCExpression) lambdaBody); Statement statement = AstUtils.createReturnOrExpressionStatement( getSourcePosition(lambdaBody), lambdaMethodBody, returnTypeDescriptor); body = Block.newBuilder() .setSourcePosition(getSourcePosition(lambdaBody)) .setStatements(statement) .build(); } return body; }
Example #2
Source File: JavacAST.java From EasyMPermission with MIT License | 6 votes |
private JavacNode buildType(JCClassDecl type) { if (setAndGetAsHandled(type)) return null; List<JavacNode> childNodes = new ArrayList<JavacNode>(); for (JCAnnotation annotation : type.mods.annotations) addIfNotNull(childNodes, buildAnnotation(annotation, false)); for (JCTree def : type.defs) { /* A def can be: * JCClassDecl for inner types * JCMethodDecl for constructors and methods * JCVariableDecl for fields * JCBlock for (static) initializers */ if (def instanceof JCMethodDecl) addIfNotNull(childNodes, buildMethod((JCMethodDecl)def)); else if (def instanceof JCClassDecl) addIfNotNull(childNodes, buildType((JCClassDecl)def)); else if (def instanceof JCVariableDecl) addIfNotNull(childNodes, buildField((JCVariableDecl)def)); else if (def instanceof JCBlock) addIfNotNull(childNodes, buildInitializer((JCBlock)def)); } return putInMap(new JavacNode(this, type, childNodes, Kind.TYPE)); }
Example #3
Source File: Analyzer.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Analyze an AST node; this involves collecting a list of all the nodes that needs rewriting, * and speculatively type-check the rewritten code to compare results against previously attributed code. */ void analyze(JCStatement statement, Env<AttrContext> env) { AnalysisContext context = new AnalysisContext(); StatementScanner statementScanner = new StatementScanner(context); statementScanner.scan(statement); if (!context.treesToAnalyzer.isEmpty()) { //add a block to hoist potential dangling variable declarations JCBlock fakeBlock = make.Block(SYNTHETIC, List.of(statement)); TreeMapper treeMapper = new TreeMapper(context); //TODO: to further refine the analysis, try all rewriting combinations deferredAttr.attribSpeculative(fakeBlock, env, attr.statInfo, treeMapper, t -> new AnalyzeDeferredDiagHandler(context), argumentAttr.withLocalCacheContext()); context.treeMap.entrySet().forEach(e -> { context.treesToAnalyzer.get(e.getKey()) .process(e.getKey(), e.getValue(), context.errors.nonEmpty()); }); } }
Example #4
Source File: HandleEqualsAndHashCode.java From EasyMPermission with MIT License | 6 votes |
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 #5
Source File: JavacJavaUtilListSetSingularizer.java From EasyMPermission with MIT License | 6 votes |
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: JavacJavaUtilListSetSingularizer.java From EasyMPermission with MIT License | 6 votes |
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 #7
Source File: Corraller.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private JCBlock resolutionExceptionBlock() { if (resolutionExceptionBlock == null) { JCExpression expClass = null; // Split the exception class name at dots for (String id : SPIResolutionException.class.getName().split("\\.")) { Name nm = names.fromString(id); if (expClass == null) { expClass = make.Ident(nm); } else { expClass = make.Select(expClass, nm); } } JCNewClass exp = make.NewClass(null, null, expClass, List.of(make.Literal(keyIndex)), null); resolutionExceptionBlock = make.Block(0L, List.of(make.Throw(exp))); } return resolutionExceptionBlock; }
Example #8
Source File: HandleBuilder.java From EasyMPermission with MIT License | 6 votes |
private JCMethodDecl generateCleanMethod(java.util.List<BuilderFieldData> builderFields, JavacNode type, JCTree source) { JavacTreeMaker maker = type.getTreeMaker(); ListBuffer<JCStatement> statements = new ListBuffer<JCStatement>(); for (BuilderFieldData bfd : builderFields) { if (bfd.singularData != null && bfd.singularData.getSingularizer() != null) { bfd.singularData.getSingularizer().appendCleaningCode(bfd.singularData, type, source, statements); } } statements.append(maker.Exec(maker.Assign(maker.Select(maker.Ident(type.toName("this")), type.toName("$lombokUnclean")), maker.Literal(CTC_BOOLEAN, false)))); JCBlock body = maker.Block(0, statements.toList()); return maker.MethodDef(maker.Modifiers(Flags.PUBLIC), type.toName("$lombokClean"), maker.Type(Javac.createVoidType(maker, CTC_VOID)), List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null); /* * if (shouldReturnThis) { methodType = cloneSelfType(field); } if (methodType == null) { //WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6. methodType = treeMaker.Type(Javac.createVoidType(treeMaker, CTC_VOID)); shouldReturnThis = false; } */ }
Example #9
Source File: PrettyCommentsPrinter.java From EasyMPermission with MIT License | 6 votes |
public void visitLabelled(JCLabeledStatement tree) { try { print(tree.label + ":"); if (isEmptyStat(tree.body) || tree.body instanceof JCSkip) { print(" ;"); } else if (tree.body instanceof JCBlock) { print(" "); printStat(tree.body); } else { println(); align(); printStat(tree.body); } } catch (IOException e) { throw new UncheckedIOException(e); } }
Example #10
Source File: JavacAST.java From EasyMPermission with MIT License | 6 votes |
/** {@inheritDoc} */ @Override protected JavacNode buildTree(JCTree node, Kind kind) { switch (kind) { case COMPILATION_UNIT: return buildCompilationUnit((JCCompilationUnit) node); case TYPE: return buildType((JCClassDecl) node); case FIELD: return buildField((JCVariableDecl) node); case INITIALIZER: return buildInitializer((JCBlock) node); case METHOD: return buildMethod((JCMethodDecl) node); case ARGUMENT: return buildLocalVar((JCVariableDecl) node, kind); case LOCAL: return buildLocalVar((JCVariableDecl) node, kind); case STATEMENT: return buildStatementOrExpression(node); case ANNOTATION: return buildAnnotation((JCAnnotation) node, false); default: throw new AssertionError("Did not expect: " + kind); } }
Example #11
Source File: HandleSneakyThrows.java From EasyMPermission with MIT License | 6 votes |
public JCStatement buildTryCatchBlock(JavacNode node, List<JCStatement> contents, String exception, JCTree source) { JavacTreeMaker maker = node.getTreeMaker(); Context context = node.getContext(); JCBlock tryBlock = setGeneratedBy(maker.Block(0, contents), source, context); JCExpression varType = chainDots(node, exception.split("\\.")); JCVariableDecl catchParam = maker.VarDef(maker.Modifiers(Flags.FINAL | Flags.PARAMETER), node.toName("$ex"), varType, null); JCExpression lombokLombokSneakyThrowNameRef = chainDots(node, "lombok", "Lombok", "sneakyThrow"); JCBlock catchBody = maker.Block(0, List.<JCStatement>of(maker.Throw(maker.Apply( List.<JCExpression>nil(), lombokLombokSneakyThrowNameRef, List.<JCExpression>of(maker.Ident(node.toName("$ex"))))))); JCTry tryStatement = maker.Try(tryBlock, List.of(recursiveSetGeneratedBy(maker.Catch(catchParam, catchBody), source, context)), null); if (JavacHandlerUtil.inNetbeansEditor(node)) { //set span (start and end position) of the try statement and the main block //this allows NetBeans to dive into the statement correctly: JCCompilationUnit top = (JCCompilationUnit) node.top().get(); int startPos = contents.head.pos; int endPos = Javac.getEndPosition(contents.last().pos(), top); tryBlock.pos = startPos; tryStatement.pos = startPos; Javac.storeEnd(tryBlock, endPos, top); Javac.storeEnd(tryStatement, endPos, top); } return setGeneratedBy(tryStatement, source, context); }
Example #12
Source File: Analyzer.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override JCLambda map (JCNewClass oldTree, JCNewClass newTree){ JCMethodDecl md = (JCMethodDecl)decls(newTree.def).head; List<JCVariableDecl> params = md.params; JCBlock body = md.body; return make.Lambda(params, body); }
Example #13
Source File: TypeAnnotations.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
@Override public void visitBlock(JCBlock tree) { // Do not descend into top-level blocks when only interested // in the signature. if (!sigOnly) { scan(tree.stats); } }
Example #14
Source File: TypeAnnotations.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Override public void visitBlock(JCBlock tree) { // Do not descend into top-level blocks when only interested // in the signature. if (!sigOnly) { scan(tree.stats); } }
Example #15
Source File: Lower.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
private JCStatement makeTwrCloseStatement(Symbol primaryException, JCExpression resource) { // primaryException.addSuppressed(catchException); VarSymbol catchException = new VarSymbol(SYNTHETIC, make.paramName(2), syms.throwableType, currentMethodSym); JCStatement addSuppressionStatement = make.Exec(makeCall(make.Ident(primaryException), names.addSuppressed, List.of(make.Ident(catchException)))); // try { resource.close(); } catch (e) { primaryException.addSuppressed(e); } JCBlock tryBlock = make.Block(0L, List.of(makeResourceCloseInvocation(resource))); JCVariableDecl catchExceptionDecl = make.VarDef(catchException, null); JCBlock catchBlock = make.Block(0L, List.of(addSuppressionStatement)); List<JCCatch> catchClauses = List.of(make.Catch(catchExceptionDecl, catchBlock)); JCTry tryTree = make.Try(tryBlock, catchClauses, null); tryTree.finallyCanCompleteNormally = true; // if (primaryException != null) {try...} else resourceClose; JCIf closeIfStatement = make.If(makeNonNullCheck(make.Ident(primaryException)), tryTree, makeResourceCloseInvocation(resource)); return closeIfStatement; }
Example #16
Source File: JavacGuavaSingularizer.java From EasyMPermission with MIT License | 5 votes |
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 #17
Source File: JavacGuavaSingularizer.java From EasyMPermission with MIT License | 5 votes |
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 #18
Source File: DeferredAttr.java From lua-for-android with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Performs speculative attribution of a lambda body and returns the speculative lambda tree, * in the absence of a target-type. Since {@link Attr#visitLambda(JCLambda)} cannot type-check * lambda bodies w/o a suitable target-type, this routine 'unrolls' the lambda by turning it * into a regular block, speculatively type-checks the block and then puts back the pieces. */ JCLambda attribSpeculativeLambda(JCLambda that, Env<AttrContext> env, ResultInfo resultInfo) { ListBuffer<JCStatement> stats = new ListBuffer<>(); stats.addAll(that.params); if (that.getBodyKind() == JCLambda.BodyKind.EXPRESSION) { stats.add(make.Return((JCExpression)that.body)); } else { stats.add((JCBlock)that.body); } JCBlock lambdaBlock = make.Block(0, stats.toList()); Env<AttrContext> localEnv = attr.lambdaEnv(that, env); try { localEnv.info.returnResult = resultInfo; JCBlock speculativeTree = (JCBlock)attribSpeculative(lambdaBlock, localEnv, resultInfo); List<JCVariableDecl> args = StreamSupport.stream(speculativeTree.getStatements()) .filter(s -> s.hasTag(Tag.VARDEF)) .map(t -> (JCVariableDecl)t) .collect(List.collector()); JCTree lambdaBody = speculativeTree.getStatements().last(); if (lambdaBody.hasTag(Tag.RETURN)) { lambdaBody = ((JCReturn)lambdaBody).expr; } JCLambda speculativeLambda = make.Lambda(args, lambdaBody); attr.preFlow(speculativeLambda); flow.analyzeLambda(env, speculativeLambda, make, false); return speculativeLambda; } finally { localEnv.info.scope.leave(); } }
Example #19
Source File: JavacASTVisitor.java From EasyMPermission with MIT License | 5 votes |
@Override public void visitInitializer(JavacNode node, JCBlock initializer) { print("<%s INITIALIZER>", initializer.isStatic() ? "static" : "instance"); indent++; if (printContent) { print("%s", initializer); disablePrinting++; } }
Example #20
Source File: CompilationUnitBuilder.java From j2cl with Apache License 2.0 | 5 votes |
private Block convertBlock(JCBlock block) { return Block.newBuilder() .setSourcePosition(getSourcePosition(block)) .setStatements( block.getStatements().stream() .map(this::convertStatement) .filter(Predicates.notNull()) .collect(toImmutableList())) .build(); }
Example #21
Source File: TypeAnnotations.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
@Override public void visitBlock(JCBlock tree) { // Do not descend into top-level blocks when only interested // in the signature. if (!sigOnly) { scan(tree.stats); } }
Example #22
Source File: TypeAnnotations.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public void visitBlock(JCBlock tree) { // Do not descend into top-level blocks when only interested // in the signature. if (!sigOnly) { scan(tree.stats); } }
Example #23
Source File: JavacNode.java From EasyMPermission with MIT License | 5 votes |
/** {@inheritDoc} */ @Override protected boolean calculateIsStructurallySignificant(JCTree parent) { if (node instanceof JCClassDecl) return true; if (node instanceof JCMethodDecl) return true; if (node instanceof JCVariableDecl) return true; if (node instanceof JCCompilationUnit) return true; //Static and instance initializers if (node instanceof JCBlock) return parent instanceof JCClassDecl; return false; }
Example #24
Source File: Corraller.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public void visitBlock(JCBlock tree) { // Top-level executable blocks (usually method bodies) are corralled super.visitBlock((tree.flags & STATIC) != 0 ? tree : resolutionExceptionBlock()); }
Example #25
Source File: InitBlockScanner.java From annotation-tools with MIT License | 5 votes |
@Override public Void visitBlock(BlockTree node, Boolean isStatic) { // TODO: is isStatic only used for static initializer blocks? if (!done && isStatic == node.isStatic() && ((JCBlock) node).endpos >= 0) { index++; } if (tree == node) { done = true; } return super.visitBlock(node, isStatic); }
Example #26
Source File: PrettyCommentsPrinter.java From EasyMPermission with MIT License | 5 votes |
public void visitBlock(JCBlock tree) { try { consumeComments(tree.pos); printFlags(tree.flags); printBlock(tree.stats, tree); } catch (IOException e) { throw new UncheckedIOException(e); } }
Example #27
Source File: TypeAnnotations.java From hottub with GNU General Public License v2.0 | 5 votes |
@Override public void visitBlock(JCBlock tree) { // Do not descend into top-level blocks when only interested // in the signature. if (!sigOnly) { scan(tree.stats); } }
Example #28
Source File: HandleBuilder.java From EasyMPermission with MIT License | 5 votes |
public JCMethodDecl generateBuilderMethod(String builderMethodName, String builderClassName, JavacNode type, List<JCTypeParameter> typeParams) { JavacTreeMaker maker = type.getTreeMaker(); ListBuffer<JCExpression> typeArgs = new ListBuffer<JCExpression>(); for (JCTypeParameter typeParam : typeParams) { typeArgs.append(maker.Ident(typeParam.name)); } JCExpression call = maker.NewClass(null, List.<JCExpression>nil(), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), List.<JCExpression>nil(), null); JCStatement statement = maker.Return(call); JCBlock body = maker.Block(0, List.<JCStatement>of(statement)); return maker.MethodDef(maker.Modifiers(Flags.STATIC | Flags.PUBLIC), type.toName(builderMethodName), namePlusTypeParamsToTypeReference(maker, type.toName(builderClassName), typeParams), copyTypeParams(maker, typeParams), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null); }
Example #29
Source File: TypeAnnotations.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Override public void visitBlock(JCBlock tree) { // Do not descend into top-level blocks when only interested // in the signature. if (!sigOnly) { scan(tree.stats); } }
Example #30
Source File: JavacHandlerUtil.java From EasyMPermission with MIT License | 5 votes |
/** * Generates a new statement that checks if the given variable is null, and if so, throws a specified exception with the * variable name as message. * * @param exName The name of the exception to throw; normally {@code java.lang.NullPointerException}. */ public static JCStatement generateNullCheck(JavacTreeMaker maker, JavacNode variable, JavacNode source) { NullCheckExceptionType exceptionType = source.getAst().readConfiguration(ConfigurationKeys.NON_NULL_EXCEPTION_TYPE); if (exceptionType == null) exceptionType = NullCheckExceptionType.NULL_POINTER_EXCEPTION; JCVariableDecl varDecl = (JCVariableDecl) variable.get(); if (isPrimitive(varDecl.vartype)) return null; Name fieldName = varDecl.name; JCExpression exType = genTypeRef(variable, exceptionType.getExceptionType()); JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), exType, List.<JCExpression>of(maker.Literal(exceptionType.toExceptionMessage(fieldName.toString()))), null); JCStatement throwStatement = maker.Throw(exception); JCBlock throwBlock = maker.Block(0, List.of(throwStatement)); return maker.If(maker.Binary(CTC_EQUAL, maker.Ident(fieldName), maker.Literal(CTC_BOT, null)), throwBlock, null); }