Gradle 在 Eclipse 上,菱形运算符无法识别?
Gradle on Eclipse, diamond operator not recognized?
我有一个要迁移到 Gradle 的代码,当我 运行 它在 Eclipse 上时(没有 Gradle 它 运行 很好),但是当我使用 Eclipse 插件或尝试 运行 它通过命令行我有钻石运算符的问题。
例如,对于这个 class:
import java.util.SortedSet;
/**
* Holds inventory of currency, calculates balance, makes change.
* Immutable.
*/
public final class Bank {
/**
* Inventory of currencies for the bank.
*/
private final Inventory<Currency> inventory;
public Inventory<Currency> getInventory() {
return inventory;
}
/**
* Calculate the balance (in cents) of the currently held currencies.
*
* @return the current balance (in cents)
*/
public long calculateBalance() {
long balance = 0L;
for (Currency currency : Currency.values()) {
balance += currency.getCents() * inventory.quantity(currency);
}
return balance;
}
/**
* Create a bank made up of currencies from this bank that add up as close
* as possible to the change amount requested. Exact change is not
* guaranteed. It's a good idea to check that the calculated balance is
* the same as the amount you expect.
*
* @param amount the amount of change needed
* @return a bank of change made from as much currency that could be used
*/
public Bank makeChange(final long amount) {
long change = amount;
Bank changeBank = new Bank();
final SortedSet<Currency> descending = Currency.descending();
for (Currency largest : descending) {
int quantity = inventory.quantity(largest);
final long cents = largest.getCents();
while (quantity > 0 && change >= cents) {
change = change - cents;
quantity = quantity - 1;
changeBank = changeBank.deposit(largest);
}
}
return changeBank;
}
/**
* Bank constructor. Fills out a new Bank with 0 amounts for each currency.
*/
public Bank()
{
this.inventory = new Inventory <> (Currency.values());
}
/**
* Private Bank constructor. Used for returning new, modified instances.
*
* @param inventory the new inventory
*/
private Bank(final Inventory<Currency> inventory) {
this.inventory = new Inventory<>(Currency.values()).add(inventory);
}
/**
* Deposit the currencies from the incoming Bank into this Bank.
*
* @param bank incoming Bank
* @return new Bank with this Bank's currencies, incoming Bank's currencies
*/
public Bank deposit(final Bank bank) {
if (bank == null) {
return this;
}
return new Bank(inventory.add(bank.getInventory()));
}
/**
* Deposit the currency incoming into this Bank.
*
* @param currency incoming currency
* @return new Bank with this Bank's currencies, incoming currency
*/
public Bank deposit(final Currency currency) {
if (currency == null) {
return this;
}
return new Bank(inventory.add(currency));
}
/**
* Withdraws the currencies from the passed in Bank from this Bank. If
* there aren't enough of any particular currency, set the quantity to 0.
*
* @param bank the Bank of currencies to withdraw
* @return a Bank with the currencies of the passed Bank withdrawn
*/
public Bank withdraw(final Bank bank) {
if (bank == null) {
return this;
}
return new Bank(inventory.subtract(bank.getInventory()));
}
}
我遇到类型错误的非法启动:
illegal start of type this.inventory <> (Currency.values());
^
我知道从 JDK7 开始就可以使用菱形运算符对泛型进行类型推断,所以我很困惑为什么会出现这个错误。你能帮我修一下吗?
提前致谢
除非您指定要使用的 Java 版本,否则 Gradle 将使用当前使用的 JVM 版本编译您的代码。如果您看到有关菱形运算符的错误消息,则可能意味着您在命令行上使用的 Java 版本早于 1.7。您需要升级到更高版本(或者如果安装了多个版本,请激活另一个版本)。
通过将这些行添加到您的 build.gradle
文件来明确指定版本几乎总是一个好主意:
sourceCompatibility = 1.7
targetCompatibility = 1.7
参考:https://docs.gradle.org/current/userguide/java_plugin.html
我有一个要迁移到 Gradle 的代码,当我 运行 它在 Eclipse 上时(没有 Gradle 它 运行 很好),但是当我使用 Eclipse 插件或尝试 运行 它通过命令行我有钻石运算符的问题。
例如,对于这个 class:
import java.util.SortedSet;
/**
* Holds inventory of currency, calculates balance, makes change.
* Immutable.
*/
public final class Bank {
/**
* Inventory of currencies for the bank.
*/
private final Inventory<Currency> inventory;
public Inventory<Currency> getInventory() {
return inventory;
}
/**
* Calculate the balance (in cents) of the currently held currencies.
*
* @return the current balance (in cents)
*/
public long calculateBalance() {
long balance = 0L;
for (Currency currency : Currency.values()) {
balance += currency.getCents() * inventory.quantity(currency);
}
return balance;
}
/**
* Create a bank made up of currencies from this bank that add up as close
* as possible to the change amount requested. Exact change is not
* guaranteed. It's a good idea to check that the calculated balance is
* the same as the amount you expect.
*
* @param amount the amount of change needed
* @return a bank of change made from as much currency that could be used
*/
public Bank makeChange(final long amount) {
long change = amount;
Bank changeBank = new Bank();
final SortedSet<Currency> descending = Currency.descending();
for (Currency largest : descending) {
int quantity = inventory.quantity(largest);
final long cents = largest.getCents();
while (quantity > 0 && change >= cents) {
change = change - cents;
quantity = quantity - 1;
changeBank = changeBank.deposit(largest);
}
}
return changeBank;
}
/**
* Bank constructor. Fills out a new Bank with 0 amounts for each currency.
*/
public Bank()
{
this.inventory = new Inventory <> (Currency.values());
}
/**
* Private Bank constructor. Used for returning new, modified instances.
*
* @param inventory the new inventory
*/
private Bank(final Inventory<Currency> inventory) {
this.inventory = new Inventory<>(Currency.values()).add(inventory);
}
/**
* Deposit the currencies from the incoming Bank into this Bank.
*
* @param bank incoming Bank
* @return new Bank with this Bank's currencies, incoming Bank's currencies
*/
public Bank deposit(final Bank bank) {
if (bank == null) {
return this;
}
return new Bank(inventory.add(bank.getInventory()));
}
/**
* Deposit the currency incoming into this Bank.
*
* @param currency incoming currency
* @return new Bank with this Bank's currencies, incoming currency
*/
public Bank deposit(final Currency currency) {
if (currency == null) {
return this;
}
return new Bank(inventory.add(currency));
}
/**
* Withdraws the currencies from the passed in Bank from this Bank. If
* there aren't enough of any particular currency, set the quantity to 0.
*
* @param bank the Bank of currencies to withdraw
* @return a Bank with the currencies of the passed Bank withdrawn
*/
public Bank withdraw(final Bank bank) {
if (bank == null) {
return this;
}
return new Bank(inventory.subtract(bank.getInventory()));
}
}
我遇到类型错误的非法启动:
illegal start of type this.inventory <> (Currency.values());
^
我知道从 JDK7 开始就可以使用菱形运算符对泛型进行类型推断,所以我很困惑为什么会出现这个错误。你能帮我修一下吗?
提前致谢
除非您指定要使用的 Java 版本,否则 Gradle 将使用当前使用的 JVM 版本编译您的代码。如果您看到有关菱形运算符的错误消息,则可能意味着您在命令行上使用的 Java 版本早于 1.7。您需要升级到更高版本(或者如果安装了多个版本,请激活另一个版本)。
通过将这些行添加到您的 build.gradle
文件来明确指定版本几乎总是一个好主意:
sourceCompatibility = 1.7
targetCompatibility = 1.7
参考:https://docs.gradle.org/current/userguide/java_plugin.html